Skip to content

Commit

Permalink
fix(javascript): account for null response in request response pair
Browse files Browse the repository at this point in the history
  • Loading branch information
ppoffice committed Mar 10, 2022
1 parent f41cbae commit abe9567
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nexus-lab/iot-service-blockchain",
"version": "0.0.4",
"version": "0.0.5",
"description": "Secure Decentralized IoT Service Platform using Consortium Blockchain",
"main": "sdk/javascript/index.js",
"repository": "git://github.com/nexus-lab/iot-service-blockchain.git",
Expand Down
135 changes: 117 additions & 18 deletions sdk/javascript/ServiceRequestResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ServiceResponse from './ServiceResponse';
import moment from './moment';

test('pair.toObject()', () => {
const pair = new ServiceRequestResponse(
let pair = new ServiceRequestResponse(
new ServiceRequest(
'ffbc9005-c62a-4563-a8f7-b32bba27d707',
moment('2021-12-12T17:34:00-05:00'),
Expand All @@ -20,7 +20,7 @@ test('pair.toObject()', () => {
'["a","b","c"]',
),
);
const obj = {
let obj: { [key: string]: any } = {
request: {
id: 'ffbc9005-c62a-4563-a8f7-b32bba27d707',
time: moment('2021-12-12T17:34:00-05:00'),
Expand All @@ -44,10 +44,40 @@ test('pair.toObject()', () => {
};

expect(pair.toObject()).toEqual(obj);

pair = new ServiceRequestResponse(
new ServiceRequest(
'ffbc9005-c62a-4563-a8f7-b32bba27d707',
moment('2021-12-12T17:34:00-05:00'),
new Service('service1', 'device1', 'org1'),
'GET',
['1', '2', '3'],
),
null,
);
obj = {
request: {
id: 'ffbc9005-c62a-4563-a8f7-b32bba27d707',
time: moment('2021-12-12T17:34:00-05:00'),
service: {
name: 'service1',
deviceId: 'device1',
organizationId: 'org1',
version: 0,
description: '',
lastUpdateTime: moment(0),
},
method: 'GET',
arguments: ['1', '2', '3'],
},
response: null,
};

expect(pair.toObject()).toEqual(obj);
});

test('pair.serialize()', () => {
const pair = new ServiceRequestResponse(
let pair = new ServiceRequestResponse(
new ServiceRequest(
'ffbc9005-c62a-4563-a8f7-b32bba27d707',
moment('2021-12-12T17:34:00-05:00'),
Expand All @@ -62,19 +92,36 @@ test('pair.serialize()', () => {
'["a","b","c"]',
),
);

const serialized =
let serialized =
'{"request":{"id":"ffbc9005-c62a-4563-a8f7-b32bba27d707","time":"2021-12-12T17:34:00.000-05:00",' +
'"service":{"name":"service1","deviceId":"device1","organizationId":"org1","version":0,' +
'"description":"","lastUpdateTime":"1969-12-31T19:00:00.000-05:00"},"method":"GET",' +
'"arguments":["1","2","3"]},"response":{"requestId":"ffbc9005-c62a-4563-a8f7-b32bba27d707",' +
'"time":"2021-12-12T17:34:00.000-05:00","statusCode":0,"returnValue":"[\\"a\\",\\"b\\",\\"c\\"]"}}';

expect(pair.serialize()).toEqual(serialized);

pair = new ServiceRequestResponse(
new ServiceRequest(
'ffbc9005-c62a-4563-a8f7-b32bba27d707',
moment('2021-12-12T17:34:00-05:00'),
new Service('service1', 'device1', 'org1'),
'GET',
['1', '2', '3'],
),
null,
);
serialized =
'{"request":{"id":"ffbc9005-c62a-4563-a8f7-b32bba27d707","time":"2021-12-12T17:34:00.000-05:00",' +
'"service":{"name":"service1","deviceId":"device1","organizationId":"org1","version":0,' +
'"description":"","lastUpdateTime":"1969-12-31T19:00:00.000-05:00"},"method":"GET",' +
'"arguments":["1","2","3"]},"response":null}';

expect(pair.serialize()).toEqual(serialized);
});

test('ServiceRequestResponse.fromObject()', () => {
const obj = {
let obj: { [key: string]: any } = {
request: {
id: 'ffbc9005-c62a-4563-a8f7-b32bba27d707',
time: moment('2021-12-12T17:34:00-05:00'),
Expand All @@ -94,22 +141,47 @@ test('ServiceRequestResponse.fromObject()', () => {
},
};

const pair = ServiceRequestResponse.fromObject(obj);
let pair = ServiceRequestResponse.fromObject(obj);
expect(pair.request.id).toEqual(obj.request.id);
expect(pair.request.time).toEqual(obj.request.time);
expect(pair.request.method).toEqual(obj.request.method);
expect(pair.request.args).toEqual(obj.request.arguments);
expect(pair.request.service.organizationId).toEqual(obj.request.service.organizationId);
expect(pair.request.service.deviceId).toEqual(obj.request.service.deviceId);
expect(pair.request.service.name).toEqual(obj.request.service.name);
expect(pair.response.requestId).toEqual(obj.response.requestId);
expect(pair.response.time).toEqual(obj.response.time);
expect(pair.response.returnValue).toEqual(obj.response.returnValue);
expect(pair.response.statusCode).toEqual(obj.response.statusCode);
expect(pair.response?.requestId).toEqual(obj.response.requestId);
expect(pair.response?.time).toEqual(obj.response.time);
expect(pair.response?.returnValue).toEqual(obj.response.returnValue);
expect(pair.response?.statusCode).toEqual(obj.response.statusCode);

obj = {
request: {
id: 'ffbc9005-c62a-4563-a8f7-b32bba27d707',
time: moment('2021-12-12T17:34:00-05:00'),
service: {
name: 'service1',
deviceId: 'device1',
organizationId: 'org1',
},
method: 'GET',
arguments: ['1', '2', '3'],
},
response: null,
};

pair = ServiceRequestResponse.fromObject(obj);
expect(pair.request.id).toEqual(obj.request.id);
expect(pair.request.time).toEqual(obj.request.time);
expect(pair.request.method).toEqual(obj.request.method);
expect(pair.request.args).toEqual(obj.request.arguments);
expect(pair.request.service.organizationId).toEqual(obj.request.service.organizationId);
expect(pair.request.service.deviceId).toEqual(obj.request.service.deviceId);
expect(pair.request.service.name).toEqual(obj.request.service.name);
expect(pair.response).toEqual(obj.response);
});

test('ServiceRequestResponse.deserialize()', () => {
const expected = new ServiceRequestResponse(
let expected = new ServiceRequestResponse(
new ServiceRequest(
'ffbc9005-c62a-4563-a8f7-b32bba27d707',
moment('2021-12-12T17:34:00-05:00'),
Expand All @@ -125,25 +197,52 @@ test('ServiceRequestResponse.deserialize()', () => {
),
);

const serialized =
let serialized =
'{"request":{"id":"ffbc9005-c62a-4563-a8f7-b32bba27d707","time":"2021-12-12T17:34:00-05:00",' +
'"service":{"name":"service1","deviceId":"device1","organizationId":"org1","version":0,' +
'"description":"","lastUpdateTime":"1969-12-31T19:00:00-05:00"},"method":"GET",' +
'"arguments":["1","2","3"]},"response":{"requestId":"ffbc9005-c62a-4563-a8f7-b32bba27d707",' +
'"time":"2021-12-12T17:34:00-05:00","statusCode":0,"returnValue":"[\\"a\\",\\"b\\",\\"c\\"]"}}';

const actual = ServiceRequestResponse.deserialize(serialized);
let actual = ServiceRequestResponse.deserialize(serialized);
expect(actual.request.id).toEqual(expected.request.id);
expect(actual.request.time).toEqual(expected.request.time);
expect(actual.request.method).toEqual(expected.request.method);
expect(actual.request.args).toEqual(expected.request.args);
expect(actual.request.service.organizationId).toEqual(expected.request.service.organizationId);
expect(actual.request.service.deviceId).toEqual(expected.request.service.deviceId);
expect(actual.request.service.name).toEqual(expected.request.service.name);
expect(actual.response?.requestId).toEqual(expected.response?.requestId);
expect(actual.response?.time).toEqual(expected.response?.time);
expect(actual.response?.returnValue).toEqual(expected.response?.returnValue);
expect(actual.response?.statusCode).toEqual(expected.response?.statusCode);

expected = new ServiceRequestResponse(
new ServiceRequest(
'ffbc9005-c62a-4563-a8f7-b32bba27d707',
moment('2021-12-12T17:34:00-05:00'),
new Service('service1', 'device1', 'org1'),
'GET',
['1', '2', '3'],
),
null,
);

serialized =
'{"request":{"id":"ffbc9005-c62a-4563-a8f7-b32bba27d707","time":"2021-12-12T17:34:00-05:00",' +
'"service":{"name":"service1","deviceId":"device1","organizationId":"org1","version":0,' +
'"description":"","lastUpdateTime":"1969-12-31T19:00:00-05:00"},"method":"GET",' +
'"arguments":["1","2","3"]},"response":null}';

actual = ServiceRequestResponse.deserialize(serialized);
expect(actual.request.id).toEqual(expected.request.id);
expect(actual.request.time).toEqual(expected.request.time);
expect(actual.request.method).toEqual(expected.request.method);
expect(actual.request.args).toEqual(expected.request.args);
expect(actual.request.service.organizationId).toEqual(expected.request.service.organizationId);
expect(actual.request.service.deviceId).toEqual(expected.request.service.deviceId);
expect(actual.request.service.name).toEqual(expected.request.service.name);
expect(actual.response.requestId).toEqual(expected.response.requestId);
expect(actual.response.time).toEqual(expected.response.time);
expect(actual.response.returnValue).toEqual(expected.response.returnValue);
expect(actual.response.statusCode).toEqual(expected.response.statusCode);
expect(actual.response).toEqual(expected.response);

expect(() => ServiceResponse.deserialize('\x00')).toThrow();
});
6 changes: 3 additions & 3 deletions sdk/javascript/ServiceRequestResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class ServiceRequestResponse {
* @param request IoT service request
* @param response IoT service response
*/
constructor(public request: ServiceRequest, public response: ServiceResponse) {}
constructor(public request: ServiceRequest, public response: ServiceResponse | null) {}

/**
* Transform current service request/response pair to a plain object
Expand All @@ -19,7 +19,7 @@ export default class ServiceRequestResponse {
toObject(): { [key: string]: any } {
return {
request: this.request.toObject(),
response: this.response.toObject(),
response: this.response?.toObject() ?? null,
};
}

Expand All @@ -41,7 +41,7 @@ export default class ServiceRequestResponse {
static fromObject(obj: { [key: string]: any }): ServiceRequestResponse {
return new ServiceRequestResponse(
ServiceRequest.fromObject(obj.request),
ServiceResponse.fromObject(obj.response),
obj.response ? ServiceResponse.fromObject(obj.response) : null,
);
}

Expand Down
11 changes: 7 additions & 4 deletions tests/e2e/javascript/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,14 @@ async function checkAndRemoveRequests(isb: Sdk) {

const pair = before[0];
await isb.getServiceBroker().get(pair.request.id);
if (pair.request.id !== pair.response.requestId) {
fatal(`request and response ID mismatch, ${pair.request.id} != ${pair.response.requestId}`);
}

if (pair.response?.requestId) {
if (pair.request.id !== pair.response?.requestId) {
fatal(`request and response ID mismatch, ${pair.request.id} != ${pair.response?.requestId}`);
}

await isb.getServiceBroker().remove(pair.request.id);
await isb.getServiceBroker().remove(pair.request.id);
}

const after = await isb
.getServiceBroker()
Expand Down

0 comments on commit abe9567

Please sign in to comment.