To make a test of the REST service, the first thing to do is to make sure to run the SEBA software with the server option specified (-s), providing or not (optional) the port where the service will be defined.
$ git clone https://github.com/gsi-upm/soba
$ cd soba/projects/seba
$ python3 run.py -v -s
Then, once the software has run, following the previous case, selecting the start option of the interface in the browser. Once the simulation has started, run the 'apiTest.py' test script. If a port was defined in the execution, change the variable port of the file to the chosen value.
$ cd restClient
$ python3 apiTest.py
To execute the tests with greater control, it is recommended to download and use the jupyter notebook that is provided in the following address: Notebook
This notebook is also presented below.
In [3]:
from unittest import TestCase
import json, requests
from jsonschema import validate
import socket
import unittest
ipServer = socket.gethostbyname(socket.gethostname())
#Port defined when executing, 10000 default
port = '10000'
#Template of the URLs
URLBASE = "http://127.0.0.1:"+ port
URISOBA = "/api/soba/v1/occupants"
URISEBA = "/api/seba/v1/occupants"
URIFIRE = "/api/seba/v1/fire"
stringTemplate = {"type": "string"}
numberTemplate = {"type": "number"}
#Number of test for each pair (URI, Method)
N = 1
In [8]:
print(str('Testing {}').format('GET /api/soba/v1/occupants'))
template = {
"type": "object",
"properties": {
"occupants": {
"type": "array"
}
},
"required": ["occupants"]
}
for i in range(N):
url = URLBASE + URISOBA
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
for o in datajson["occupants"]:
validate(o, numberTemplate)
In [9]:
print(str('Testing {}').format('GET /api/soba/v1/occupants/movements'))
template = {
"type": "object",
"properties": {
"orientation": {
"type": "string"
},
"speed": {
"type": "number"
}
},
"required": ["orientation", "speed"]
}
template2 = {
"type": "object"
}
for i in range(N):
url = URLBASE + URISOBA + "/movements"
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template2)
for k, v in datajson.items():
validate(k, stringTemplate)
validate(int(k), numberTemplate)
validate(v, template)
In [10]:
print(str('Testing {}').format('GET /api/soba/v1/occupants/positions'))
template = {
"type": "object",
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
}
},
"required": ["x", "y"]
}
for i in range(N):
url = URLBASE + URISOBA + "/positions"
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
for k, v in datajson.items():
validate(k, stringTemplate)
validate(int(k), numberTemplate)
validate(v, template)
In [11]:
print(str('Testing {}').format('GET /api/soba/v1/occupants/states'))
for i in range(N):
url = URLBASE + URISOBA + "/states"
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
for k,v in datajson.items():
validate(v, stringTemplate)
validate(k, stringTemplate)
validate(int(k), numberTemplate)
In [12]:
print(str('Testing {}').format('GET /api/soba/v1/occupants/{id}'))
template = {
"type": "object",
"properties": {
"occupant":{
"type": "object",
"properties": {
"state":{
"type": "string"
},
"fov": {
"type": "array"
},
"unique_id":{
"type": "string"
},
"movement": {
"type": "object",
"properties": {
"orientation":{
"type": "string"
},
"speed":{
"type": "number"
},
},
"required": ["orientation", "speed"]
},
"position": {
"type": "object",
"properties": {
"x":{
"type": "number"
},
"y":{
"type": "number"
}
},
"required": ["x", "y"]
}
},
"required": ["state", "fov", "unique_id", "movement", "position"]
}
},
"required": ["occupant"]
}
template2 = {
"type": "object",
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
}
},
"required": ["x", "y"]
}
for i in range(N):
url = URLBASE + URISOBA + "/" + str(0)
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
validate(int(datajson['occupant']['unique_id']), numberTemplate)
print(template)
for p in datajson['occupant']['fov']:
validate(p, template2)
In [13]:
print(str('Testing {}').format('GET /api/soba/v1/occupants/{id}/movement'))
template = {
"type": "object",
"properties": {
"movement":{
"type": "object",
"properties": {
"orientation": {
"type": "string"
},
"speed": {
"type": "number"
}
},
"required": ["orientation", "speed"]
}
},
"required": ["movement"]
}
for i in range(N):
url = URLBASE + URISOBA + "/" + str(0) + "/movement"
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
In [14]:
print(str('Testing {}').format('GET /api/soba/v1/occupants/{id}/position'))
template = {
"type": "object",
"properties": {
"position":{
"type": "object",
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
}
},
"required": ["x", "y"]
}
},
"required": ["position"]
}
for i in range(N):
url = URLBASE + URISOBA + "/" + str(0) + "/position"
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
In [15]:
print(str('Testing {}').format('GET /api/soba/v1/occupants/{id}/state'))
template = {
"type": "object",
"properties":{
"state": {
"type": "string"
}
},
"required": ["state"]
}
for i in range(N):
url = URLBASE + URISOBA + "/" + str(0) + "/state"
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
In [21]:
print(str('Testing {}').format('GET /api/soba/v1/occupants/{id}/fov'))
template = {
"type": "object",
"properties": {
"fov": {
"type": "array"
}
},
"required": ["fov"]
}
template2 = {
"type": "object",
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
}
},
"required": ["x", "y"]
}
for i in range(N):
url = URLBASE + URISOBA + "/" + str(0) + "/fov"
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
for p in datajson['fov']:
validate(p, template2)
In [5]:
print(str('Testing {}').format('PUT /api/soba/v1/occupants/{id}'))
template = {
"type": "object",
"properties": {
"avatar":{
"type": "object",
"properties": {
"position":{
"type": "object",
"properties": {
"x": {
"type": "number",
},
"y": {
"type": "number"
}
},
"required": ["x", "y"]
},
"id":{
"type": "number"
}
},
"required": ["position", "id"]
}
},
"required": ["avatar"]
}
dataBody = {"x": 10, "y": 10}
for i in range(N):
url = URLBASE + URISOBA + "/" + str(0)
data = requests.put(url, json=dataBody, headers={'Content-Type': "application/json", 'Accept': "application/json"})
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
In [7]:
print(str('Testing {}').format('POST /api/soba/v1/occupants/{id}/position'))
template = {
"type": "object",
"properties": {
"avatar":{
"type": "object",
"properties": {
"position":{
"type": "object",
"properties": {
"x": {
"type": "number",
},
"y": {
"type": "number"
}
},
"required": ["x", "y"]
},
"id":{
"type": "number"
}
},
"required": ["position", "id"]
}
},
"required": ["avatar"]
}
dataBody = {"x": 5, "y": 7}
for i in range(N):
url = URLBASE + URISOBA + "/" + str(100000) + "/position"
data = requests.post(url, json=dataBody, headers={'Content-Type': "application/json", 'Accept': "application/json"})
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
In [16]:
print(str('Testing {}').format('GET /api/seba/v1/occupants/{id}/route/{route_id}'))
template = {
"type": "object",
"properties": {
"positions": {
"type": "array"
}
}
}
template2 = {
"type": "object",
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
}
},
"required": ["x", "y"]
}
for i in range(N):
url = URLBASE + URISEBA + "/" + str(100000) + "/route/1"
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
for m in datajson["positions"]:
validate(m, template2)
In [29]:
print(str('Testing {}').format('PUT /api/seba/v1/occupants/{id}'))
template = {
"type": "object",
"properties": {
"avatar": {
"type": "object",
"properties":{
"position":{
"type": "object",
"properties":{
"x": {
"type": "number"
},
"y": {
"type": "number"
}
},
"required": ["x", "y"]
},
"id": {
"type": "number"
}
},
"required": ["position", "id"]
}
},
"required": ["avatar"]
}
dataBody = {"x": 13, "y": 13}
for i in range(N):
url = URLBASE + URISEBA + "/" + str(1)
data = requests.put(url, json=dataBody, headers={'Content-Type': "application/json", 'Accept': "application/json"})
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
In [22]:
print(str('Testing {}').format('GET /api/seba/v1/occupants/{id}/fire'))
template = {
"type": "object",
"properties": {
"positions": {
"type": "array"
}
},
"required": ["positions"]
}
template2 = {
"type": "object",
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
}
},
"required": ["x", "y"]
}
for i in range(N):
url = URLBASE + URISEBA + "/" + str(2) + "/fire"
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
for m in datajson["positions"]:
validate(m, template2)
In [31]:
print(str('Testing {}').format('GET /api/seba/v1/fire'))
template = {
"type": "object",
"properties": {
"positions": {
"type": "array"
}
},
"required": ["positions"]
}
template2 = {
"type": "object",
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
}
},
"required": ["x", "y"]
}
for i in range(N):
url = URLBASE + URIFIRE
data = requests.get(url)
datajson = data.json()
print("Response: ", datajson)
validate(datajson, template)
for m in datajson["positions"]:
validate(m, template2)