Skip to content

Commit

Permalink
Added unit test to validate the case where there are no subscription yet
Browse files Browse the repository at this point in the history
  • Loading branch information
cpoder committed Jun 6, 2024
1 parent a1969d7 commit 47e7c98
Show file tree
Hide file tree
Showing 7 changed files with 2,528 additions and 6,755 deletions.
4 changes: 4 additions & 0 deletions nodejs/c8y-codec-interface/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node"
}
9,134 changes: 2,410 additions & 6,724 deletions nodejs/c8y-codec-interface/package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions nodejs/c8y-codec-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "jest --detectOpenHandles",
"build": "tsc",
"watch": "concurrently --kill-others npm:watch-*",
"watch-typescript": "tsc -w",
Expand All @@ -17,8 +17,9 @@
"devDependencies": {
"@types/express": "^4.17.11",
"@types/node": "^18.11.9",
"@types/jest": "^29.5.7",
"concurrently": "^6.0.0",
"jest": "^26.6.3",
"jest": "^29.7.0",
"nodemon": "^2.0.7",
"ts-node": "^9.1.1",
"reflect-metadata": "^0.1.13",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { FetchClient, IFetchResponse } from "@c8y/client";
import { MicroserviceSubscriptionService } from "./MicroserviceSubscriptionService";

describe("Test microservice subscription service", () => {
test("Test getting no users", async () => {
process.env.C8Y_BASEURL = "https://fake";
jest.spyOn(FetchClient.prototype, "fetch").mockImplementation(async () => {
let response: IFetchResponse = {
status: 200,
headers: new Headers(),
arrayBuffer: null,
json: async () => {
return {};
},
text: null,
ok: true,
redirected: true,
statusText: "",
type: null,
url: "",
body: null,
clone: null,
bodyUsed: true,
blob: null,
formData: null,
};
return response;
});
let service = new MicroserviceSubscriptionService(true);
await service.getUsers();
expect(service.getClients().size).toBe(0);
});
test("Test getting one user", async () => {
process.env.C8Y_BASEURL = "https://fake";
jest.spyOn(FetchClient.prototype, "fetch").mockImplementation(async () => {
let response: IFetchResponse = {
status: 200,
headers: new Headers(),
arrayBuffer: null,
json: async () => {
return {
users: [
{
name: "user",
password: "mypassword",
tenant: "fake-tenant",
},
],
};
},
text: null,
ok: true,
redirected: true,
statusText: "",
type: null,
url: "",
body: null,
clone: null,
bodyUsed: true,
blob: null,
formData: null,
};
return response;
});
let service = new MicroserviceSubscriptionService(true);
await service.getUsers();
expect(service.getClients().size).toBe(1);
expect(service.getClients().get("fake-tenant")).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class MicroserviceSubscriptionService extends EventEmitter {
protected clients: Map<string, Client> = new Map<string, Client>();
protected logger = Logger.getLogger("MicroserviceSubscriptionService");

constructor() {
constructor(testmode?) {
super();
this.client = new FetchClient(
new BasicAuth({
Expand All @@ -23,40 +23,42 @@ export class MicroserviceSubscriptionService extends EventEmitter {
}),
this.baseUrl
);
cron.schedule("*/10 * * * * *", () => {
if (testmode) {
this.getUsers();
});
} else {
cron.schedule("*/10 * * * * *", () => {
this.getUsers();
});
}
}

protected async getUsers() {
this.client
.fetch("/application/currentApplication/subscriptions")
.then(async (result) => {
let allUsers = await result.json();
let newClients: Map<string, Client> = new Map<string, Client>();
if (allUsers?.users) {
allUsers.users.forEach((user) => {
if (!Array.from(this.clients.keys()).includes(user.tenant)) {
const auth = new BasicAuth({
user: user.name,
password: user.password,
tenant: user.tenant,
});
let client: Client = new Client(auth, this.baseUrl);
newClients.set(user.tenant, client);

this.emit("newMicroserviceSubscription", client);
} else {
newClients.set(user.tenant, this.clients.get(user.tenant));
}
async getUsers() {
try {
let result = await this.client.fetch(
"/application/currentApplication/subscriptions"
);
let allUsers = await result.json();
let newClients: Map<string, Client> = new Map<string, Client>();
allUsers?.users?.forEach((user) => {
if (!Array.from(this.clients.keys()).includes(user.tenant)) {
const auth = new BasicAuth({
user: user.name,
password: user.password,
tenant: user.tenant,
});
let client: Client = new Client(auth, this.baseUrl);
newClients.set(user.tenant, client);

this.emit("newMicroserviceSubscription", client);
} else {
newClients.set(user.tenant, this.clients.get(user.tenant));
}
this.clients = newClients;
})
.catch((e) => {
console.log(e);
this.logger.error(e);
});
this.clients = newClients;
} catch (e) {
console.log(e);
this.logger.error(e);
}
}

getClients(): Map<string, Client> {
Expand Down
1 change: 1 addition & 0 deletions nodejs/c8y-codec-interface/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dom"
],
"types": [
"jest",
"reflect-metadata",
"node"
],
Expand Down
9 changes: 9 additions & 0 deletions nodejs/c8y-codec-interface/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": ["jest"],
"esModuleInterop": true
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}

0 comments on commit 47e7c98

Please sign in to comment.