Skip to content

Commit

Permalink
#66 add missing tests for PUT resources and fix interface of ThingsHa…
Browse files Browse the repository at this point in the history
…ndle#putAttribute and HttpThingsHandleV1#putAcl
  • Loading branch information
ffendt committed Feb 5, 2021
1 parent b21fd70 commit bf9c525
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
4 changes: 2 additions & 2 deletions javascript/lib/api/src/client/handles/things.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface ThingsHandle {
* @param options - Options to use for the request.
* @returns A Promise for the response
*/
putAttribute(thingId: string, attributePath: string, attributeValue: any, options?: MatchOptions): Promise<GenericResponse>;
putAttribute(thingId: string, attributePath: string, attributeValue: any, options?: MatchOptions): Promise<PutResponse<any>>;

/**
* Deletes a Thing.
Expand Down Expand Up @@ -163,7 +163,7 @@ export interface HttpThingsHandleV1 extends HttpThingsHandle {
* @param options - Options to use for the request.
* @returns A Promise for the response
*/
putAcl(thingId: string, acl: Acl, options?: MatchOptions): Promise<GenericResponse>;
putAcl(thingId: string, acl: Acl, options?: MatchOptions): Promise<PutResponse<Acl>>;

/**
* Updates an AclEntry of a Thing.
Expand Down
10 changes: 6 additions & 4 deletions javascript/lib/api/src/client/handles/things.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ export class DefaultThingsHandle implements WebSocketThingsHandle, HttpThingsHan
}

public putAttribute(thingId: string, attributePath: string,
attributeValue: any, options?: MatchOptions): Promise<GenericResponse> {
return this.requestFactory.fetchRequest({
attributeValue: any, options?: MatchOptions): Promise<PutResponse<any>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => o,
id: thingId,
path: `attributes/${attributePath}`,
requestOptions: options,
Expand All @@ -195,9 +196,10 @@ export class DefaultThingsHandle implements WebSocketThingsHandle, HttpThingsHan
});
}

public putAcl(thingId: string, acl: Acl, options?: MatchOptions): Promise<GenericResponse> {
return this.requestFactory.fetchRequest({
public putAcl(thingId: string, acl: Acl, options?: MatchOptions): Promise<PutResponse<Acl>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => Acl.fromObject(o),
id: thingId,
path: 'acl',
requestOptions: options,
Expand Down
2 changes: 1 addition & 1 deletion javascript/lib/api/src/model/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class PutResponse<T> implements GenericResponse {
}

public wasCreated(): boolean {
return this.body !== undefined;
return this.body !== undefined && this.body !== null;
}

public wasUpdated(): boolean {
Expand Down
56 changes: 52 additions & 4 deletions javascript/lib/api/tests/client/http/features.http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Http Features Handle', () => {
});
});

it('updates Features', () => {
it('creates Features', () => {
return H.test({
toTest: () => handle.putFeatures(H.features),
testBody: Features.toObject(H.features),
Expand All @@ -89,6 +89,18 @@ describe('Http Features Handle', () => {
});
});

it('updates Features', () => {
return H.test({
toTest: () => handle.putFeatures(H.features),
testBody: Features.toObject(H.features),
expected: new PutResponse(null, 204, undefined),
request: baseRequest,
method: 'put',
status: 204,
payload: Features.toJson(H.features)
});
});

it('creates a Feature', () => {
return H.test({
toTest: () => handle.putFeature(H.feature),
Expand All @@ -113,7 +125,7 @@ describe('Http Features Handle', () => {
});
});

it('updates a Definition', () => {
it('creates a Definition', () => {
return H.test({
toTest: () => handle.putDefinition(H.feature.id, H.definition),
testBody: H.definition,
Expand All @@ -125,7 +137,19 @@ describe('Http Features Handle', () => {
});
});

it('updates Properties', () => {
it('updates a Definition', () => {
return H.test({
toTest: () => handle.putDefinition(H.feature.id, H.definition),
testBody: H.definition,
expected: new PutResponse(null, 204, undefined),
request: `${baseRequest}/${H.feature.id}/definition`,
method: 'put',
status: 204,
payload: JSON.stringify(H.definition)
});
});

it('creates Properties', () => {
return H.test({
toTest: () => handle.putProperties(H.feature.id, H.properties),
testBody: H.properties,
Expand All @@ -137,7 +161,19 @@ describe('Http Features Handle', () => {
});
});

it('updates a Property', () => {
it('updates Properties', () => {
return H.test({
toTest: () => handle.putProperties(H.feature.id, H.properties),
testBody: H.properties,
expected: new PutResponse(null, 204, undefined),
request: `${baseRequest}/${H.feature.id}/properties`,
method: 'put',
status: 204,
payload: JSON.stringify(H.properties)
});
});

it('creates a Property', () => {
return H.test({
toTest: () => handle.putProperty(H.feature.id, H.propertyPath, H.property),
testBody: H.property,
Expand All @@ -149,6 +185,18 @@ describe('Http Features Handle', () => {
});
});

it('updates a Property', () => {
return H.test({
toTest: () => handle.putProperty(H.feature.id, H.propertyPath, H.property),
testBody: H.property,
expected: new PutResponse(null, 204, undefined),
request: `${baseRequest}/${H.feature.id}/properties/${H.propertyPath}`,
method: 'put',
status: 204,
payload: JSON.stringify(H.property)
});
});

it('deletes Features', () => {
return H.test({
toTest: () => handle.deleteFeatures(),
Expand Down
15 changes: 14 additions & 1 deletion javascript/lib/api/tests/client/http/things.http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,31 @@ describe('Http Things Handle', () => {
return H.test({
toTest: () => handleV2.putAttribute(H.thing.thingId, H.attributePath, H.attribute),
testBody: H.attribute,
expected: { status: 201, headers: undefined, body: H.attribute },
expected: new PutResponse(H.attribute, 201, undefined),
request: `${baseRequest}/attributes/${H.attributePath}`,
method: 'put',
status: 201,
payload: JSON.stringify(H.attribute)
});
});

it('updates an Attribute', () => {
return H.test({
toTest: () => handleV2.putAttribute(H.thing.thingId, H.attributePath, H.attribute),
testBody: H.attribute,
expected: new PutResponse(null, 204, undefined),
request: `${baseRequest}/attributes/${H.attributePath}`,
method: 'put',
status: 204,
payload: JSON.stringify(H.attribute)
});
});

it('updates an Acl', () => {
return H.test({
toTest: () => handleV1.putAcl(H.thing.thingId, acl),
request: `${baseRequest}/acl`,
expected: new PutResponse(null, 204, undefined),
method: 'put',
status: 204,
payload: Acl.toJson(acl),
Expand Down

0 comments on commit bf9c525

Please sign in to comment.