Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Weirich <matthias.weirich@selectcode.de>
  • Loading branch information
vavido committed Jan 11, 2021
1 parent 8faced6 commit e14f281
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 61 deletions.
2 changes: 1 addition & 1 deletion javascript/lib/api/src/client/handles/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {
id: this.thingId,
path: 'features',
requestOptions: options,
payload: features.toObject()
payload: Features.toObject(features)
});
}

Expand Down
6 changes: 3 additions & 3 deletions javascript/lib/api/src/client/handles/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export class DefaultPoliciesHandle implements PoliciesHandle {
id: policyId,
path: 'entries',
requestOptions: options,
payload: entries.toObject()
payload: Entries.toObject(entries)
});
}

Expand Down Expand Up @@ -416,7 +416,7 @@ export class DefaultPoliciesHandle implements PoliciesHandle {
id: policyId,
path: `entries/${label}/subjects`,
requestOptions: options,
payload: subjects.toObject()
payload: Subjects.toObject(subjects)
});
}

Expand Down Expand Up @@ -456,7 +456,7 @@ export class DefaultPoliciesHandle implements PoliciesHandle {
id: policyId,
path: `entries/${label}/resources`,
requestOptions: options,
payload: resources.toObject()
payload: Resources.toObject(resources)
});
}

Expand Down
2 changes: 1 addition & 1 deletion javascript/lib/api/src/client/handles/things.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class DefaultThingsHandle implements WebSocketThingsHandle, HttpThingsHan
id: thingId,
path: 'acl',
requestOptions: options,
payload: acl.toObject()
payload: Acl.toObject(acl)
});
}

Expand Down
12 changes: 6 additions & 6 deletions javascript/lib/api/src/model/policies.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ export class Entries extends IndexedEntityModel<Entry> {
* @param o - The object to parse.
* @returns The Entries
*/
public static fromObject(o: any): Entries | undefined {
public static fromObject(o: any): Entries {
if (o === undefined) {
return o;
}
return this.fromPlainObject(o, Entry.fromObject);
return IndexedEntityModel.fromPlainObject(o, Entry.fromObject) || {};
}

}
Expand Down Expand Up @@ -136,11 +136,11 @@ export class Subjects extends IndexedEntityModel<Subject> {
* @param o - The object to parse.
* @returns The Subjects
*/
public static fromObject(o: any): Subjects | undefined {
public static fromObject(o: any): Subjects {
if (o === undefined) {
return o;
}
return IndexedEntityModel.fromPlainObject(o, Subject.fromObject, key => key);
return IndexedEntityModel.fromPlainObject(o, Subject.fromObject, key => key) || {};
}
}

Expand All @@ -155,11 +155,11 @@ export class Resources extends IndexedEntityModel<Resource> {
* @param o - The object to parse.
* @returns The Resources
*/
public static fromObject(o: any): Resources | undefined {
public static fromObject(o: any): Resources {
if (o === undefined) {
return o;
}
return IndexedEntityModel.fromPlainObject(o, Resource.fromObject);
return IndexedEntityModel.fromPlainObject(o, Resource.fromObject) || {};
}
}

Expand Down
8 changes: 4 additions & 4 deletions javascript/lib/api/src/model/things.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ export class Features extends IndexedEntityModel<Feature> {
* @param o - The object to parse.
* @returns The Features
*/
public static fromObject(o: any): Features | undefined {
public static fromObject(o: any): Features {
if (o === undefined) {
return o;
}
return IndexedEntityModel.fromPlainObject<Feature>(o, Feature.fromObject);
return IndexedEntityModel.fromPlainObject<Feature>(o, Feature.fromObject) || {};
}
}

Expand Down Expand Up @@ -189,11 +189,11 @@ export class Acl extends IndexedEntityModel<AclEntry> {
* @param o - The object to parse.
* @returns The Acl
*/
public static fromObject(o: any): Acl | undefined {
public static fromObject(o: any): Acl {
if (o === undefined) {
return o;
}
return IndexedEntityModel.fromPlainObject<AclEntry>(o, AclEntry.fromObject);
return IndexedEntityModel.fromPlainObject<AclEntry>(o, AclEntry.fromObject) || {};
}
}

Expand Down
7 changes: 4 additions & 3 deletions javascript/lib/api/tests/client/http/features.http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { FeaturesHandle } from '../../../src/client/handles/features.interfaces';
import { PutResponse } from '../../../src/model/response';
import { HttpHelper as H } from './http.helper';
import { Features } from '../../../src/model/things.model';

describe('Http Features Handle', () => {
const baseRequest = `things/${H.thing.thingId}/features`;
Expand All @@ -24,7 +25,7 @@ describe('Http Features Handle', () => {
it('gets Features', () => {
return H.test({
toTest: () => handle.getFeatures(),
testBody: H.features.toObject(),
testBody: Features.toObject(H.features),
expected: H.features,
request: baseRequest,
method: 'get',
Expand Down Expand Up @@ -79,12 +80,12 @@ describe('Http Features Handle', () => {
it('updates Features', () => {
return H.test({
toTest: () => handle.putFeatures(H.features),
testBody: H.features.toObject(),
testBody: Features.toObject(H.features),
expected: new PutResponse(H.features, 201, undefined),
request: baseRequest,
method: 'put',
status: 201,
payload: H.features.toJson()
payload: Features.toJson(H.features)
});
});

Expand Down
32 changes: 16 additions & 16 deletions javascript/lib/api/tests/client/http/policies.http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

/* tslint:disable:no-big-function */
import {
AccessRight, DittoSubjectIssuer,
AccessRight,
Entries,
Entry,
Policy,
Resource,
Resources,
Subject, SubjectId,
Subjects,
SubjectType
Subject,
SubjectId,
Subjects
} from '../../../src/model/policies.model';
import { PutResponse } from '../../../src/model/response';
import { HttpHelper as H } from './http.helper';
Expand All @@ -32,15 +32,15 @@ describe('Http Policies Handle', () => {
const resourcePath = 'aResource';
const aResource = new Resource(resourcePath, [AccessRight.Read], [AccessRight.Write]);
const anotherResource = new Resource('anotherResource', [AccessRight.Write], [AccessRight.Write]);
const resources = new Resources({ aResource, anotherResource });
const resources = { aResource, anotherResource };
const subjectId = 'nginx:aSubject';
const aSubject = new Subject(SubjectId.fromString(subjectId), 'my default nginx user');
const anotherSubject = new Subject(SubjectId.fromString('anotherSubject'), 'my other nginx user');
const subjects = new Subjects({ [aSubject.id]: aSubject, [anotherSubject.id]: anotherSubject });
const subjects = { [aSubject.id]: aSubject, [anotherSubject.id]: anotherSubject };
const label = 'anEntry';
const anEntry = new Entry(label, subjects, resources);
const anotherEntry = new Entry('anotherEntry', subjects, resources);
const entries = new Entries({ anEntry, anotherEntry });
const entries = { anEntry, anotherEntry };
const policyId = 'Testspace:Testpolicy';
const policy = new Policy(policyId, entries);
const baseRequest = `policies/${policy.id}`;
Expand All @@ -60,7 +60,7 @@ describe('Http Policies Handle', () => {
it('gets Entries', () => {
return H.test({
toTest: () => handle.getEntries(policy.id),
testBody: entries.toObject(),
testBody: Entries.toObject(entries),
expected: entries,
request: `${baseRequest}/entries`,
method: 'get',
Expand All @@ -82,7 +82,7 @@ describe('Http Policies Handle', () => {
it('gets Subjects', () => {
return H.test({
toTest: () => handle.getSubjects(policy.id, label),
testBody: subjects.toObject(),
testBody: Subjects.toObject(subjects),
expected: subjects,
request: `${baseRequest}/entries/${label}/subjects`,
method: 'get',
Expand All @@ -104,7 +104,7 @@ describe('Http Policies Handle', () => {
it('gets Resources', () => {
return H.test({
toTest: () => handle.getResources(policy.id, label),
testBody: resources.toObject(),
testBody: Resources.toObject(resources),
expected: resources,
request: `${baseRequest}/entries/${label}/resources`,
method: 'get',
Expand Down Expand Up @@ -138,12 +138,12 @@ describe('Http Policies Handle', () => {
it('updates Entries', () => {
return H.test({
toTest: () => handle.putEntries(policy.id, entries),
testBody: entries.toObject(),
testBody: Entries.toObject(entries),
expected: new PutResponse(entries, 201, undefined),
request: `${baseRequest}/entries`,
method: 'put',
status: 201,
payload: entries.toJson()
payload: Entries.toJson(entries)
});
});

Expand All @@ -162,12 +162,12 @@ describe('Http Policies Handle', () => {
it('updates Subjects', () => {
return H.test({
toTest: () => handle.putSubjects(policy.id, label, subjects),
testBody: subjects.toObject(),
testBody: Subjects.toObject(subjects),
expected: new PutResponse(subjects, 201, undefined),
request: `${baseRequest}/entries/${label}/subjects`,
method: 'put',
status: 201,
payload: subjects.toJson()
payload: Subjects.toJson(subjects)
});
});

Expand All @@ -186,12 +186,12 @@ describe('Http Policies Handle', () => {
it('updates Resources', () => {
return H.test({
toTest: () => handle.putResources(policy.id, label, resources),
testBody: resources.toObject(),
testBody: Resources.toObject(resources),
expected: new PutResponse(resources, 201, undefined),
request: `${baseRequest}/entries/${label}/resources`,
method: 'put',
status: 201,
payload: resources.toJson()
payload: Resources.toJson(resources)
});
});

Expand Down
12 changes: 8 additions & 4 deletions javascript/lib/api/tests/client/http/things.http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
import { HttpThingsHandleV1, HttpThingsHandleV2 } from '../../../src/client/handles/things.interfaces';
import { PutResponse } from '../../../src/model/response';
import { Acl, AclEntry } from '../../../src/model/things.model';
import { DefaultFieldsOptions, DefaultGetThingsOptions, DefaultMatchOptions } from '../../../src/options/request.options';
import {
DefaultFieldsOptions,
DefaultGetThingsOptions,
DefaultMatchOptions
} from '../../../src/options/request.options';
import { HttpHelper as H } from './http.helper';

describe('Http Things Handle', () => {
Expand All @@ -27,7 +31,7 @@ describe('Http Things Handle', () => {
const authorizationSubject = 'Id';
const anAclEntry = new AclEntry(authorizationSubject, true, true, true);
const anotherAclEntry = new AclEntry('Test', false, false, false);
const acl = new Acl({ [anAclEntry.id]: anAclEntry, [anotherAclEntry.id]: anotherAclEntry });
const acl = { [anAclEntry.id]: anAclEntry, [anotherAclEntry.id]: anotherAclEntry };

it('sends options and gets a Thing', () => {
const options = DefaultFieldsOptions.getInstance().withFields('A', 'B').ifMatch('C').ifNoneMatch('D');
Expand Down Expand Up @@ -105,7 +109,7 @@ describe('Http Things Handle', () => {
it('gets an Acl', () => {
return H.test({
toTest: () => handleV1.getAcl(H.thing.thingId),
testBody: acl.toObject(),
testBody: Acl.toObject(acl),
expected: acl,
request: `${baseRequest}/acl`,
method: 'get',
Expand Down Expand Up @@ -195,7 +199,7 @@ describe('Http Things Handle', () => {
request: `${baseRequest}/acl`,
method: 'put',
status: 204,
payload: acl.toJson(),
payload: Acl.toJson(acl),
api: 1
});
});
Expand Down
6 changes: 3 additions & 3 deletions javascript/lib/api/tests/client/test.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* SPDX-License-Identifier: EPL-2.0
*/

import { Feature, Features, Thing } from '../../src/model/things.model';
import { Feature, Thing } from '../../src/model/things.model';
import { AuthProvider, DittoHeaders, DittoURL } from '../../src/auth/auth-provider';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 0;
Expand Down Expand Up @@ -49,10 +49,10 @@ export class Helper {
public static readonly properties = { A21: 'A3', A31: Helper.property };
public static readonly feature = new Feature('F1', Helper.definition, Helper.properties);
public static readonly anotherFeature = new Feature('F2', ['def2'], { A22: 'A3', A32: 'A4' });
public static readonly features = new Features({
public static readonly features = {
[Helper.feature.id]: Helper.feature,
[Helper.anotherFeature.id]: Helper.anotherFeature
});
};


public static testError(method: () => Promise<any>): Promise<any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import { ProtocolResponseValue } from '../../../src/client/request-factory/websocket-request-handler';
import { EventsHelper as H } from './events.helper';
import { Features } from '../../../src/model/things.model';


describe('WebSocket Commands Handle', () => {
Expand Down Expand Up @@ -90,7 +91,7 @@ describe('WebSocket Commands Handle', () => {
topic,
action,
path: '/features',
value: H.features.toObject()
value: Features.toObject(H.features)
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import { ProtocolResponseValue } from '../../../src/client/request-factory/websocket-request-handler';
import { EventsHelper as H } from './events.helper';
import { Features } from '../../../src/model/things.model';


describe('WebSocket Events Handle', () => {
Expand Down Expand Up @@ -90,7 +91,7 @@ describe('WebSocket Events Handle', () => {
topic,
action,
path: '/features',
value: H.features.toObject()
value: Features.toObject(H.features)
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

import { WebSocketHelper as H } from './websocket.helper';
import { Features } from '../../../src/model/things.model';

// tslint:disable-next-line:no-big-function
describe('WebSocket Features Handle', () => {
Expand All @@ -25,7 +26,7 @@ describe('WebSocket Features Handle', () => {
topic: `${baseTopic}/retrieve`,
path: '/features',
status: 200,
responseBody: H.features.toObject(),
responseBody: Features.toObject(H.features),
expected: H.features
});
});
Expand Down Expand Up @@ -80,7 +81,7 @@ describe('WebSocket Features Handle', () => {
topic: `${baseTopic}/modify`,
path: '/features',
status: 204,
requestBody: H.features.toObject()
requestBody: Features.toObject(H.features)
});
});

Expand Down
Loading

0 comments on commit e14f281

Please sign in to comment.