Skip to content

Commit

Permalink
Merge pull request #43 from SolaceLabs/ep-apps4
Browse files Browse the repository at this point in the history
Ep apps4
  • Loading branch information
ricardojosegomezulmke authored Feb 27, 2023
2 parents 1941106 + cd808f8 commit 02bedf1
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 95 deletions.
12 changes: 12 additions & 0 deletions .changeset/three-bats-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@solace-labs/ep-sdk": minor
---

add managing custom attributes for application domains

- **EpSdkApplicationDomainsServiceClass**
- `setCustomAttributes()`
- `unsetCustomAttributes()`
- `removeAssociatedEntityTypeFromCustomAttributeDefinitions()`
- `listAll()`
- new optional parameter: `attributesQuery?: IEpSdkAttributesQuery;`
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solace-labs/solace-tools-typescript",
"version": "2.9.3",
"version": "2.9.4",
"private": true,
"workspaces": [
"packages/*"
Expand Down
103 changes: 101 additions & 2 deletions packages/ep-sdk/src/services/EpSdkApplicationDomainsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ApplicationDomainResponse,
ApplicationDomainsResponse,
ApplicationDomainsService,
CustomAttribute,
Pagination,
} from '@solace-labs/ep-openapi-node';
import {
Expand All @@ -15,14 +16,105 @@ import {
EpApiMaxPageSize
} from '../constants';
import { EpSdkServiceClass } from './EpSdkService';
import { EEpSdkCustomAttributeEntityTypes, IEpSdkAttributesQuery, TEpSdkCustomAttributeList } from '../types';
import EpSdkCustomAttributesQueryService from './EpSdkCustomAttributesQueryService';
import EpSdkCustomAttributesService from './EpSdkCustomAttributesService';
import EpSdkCustomAttributeDefinitionsService from './EpSdkCustomAttributeDefinitionsService';


/** @category Services */
export class EpSdkApplicationDomainsServiceClass extends EpSdkServiceClass {

public listAll = async({ pageSize = EpApiMaxPageSize, xContextId }:{
private async updateApplicationDomain({ xContextId, update }:{
xContextId?: string;
update: ApplicationDomain;
}): Promise<ApplicationDomain> {
const funcName = 'updateApplicationDomain';
const logName = `${EpSdkApplicationDomainsServiceClass.name}.${funcName}()`;
/* istanbul ignore next */
if(update.id === undefined) throw new EpSdkApiContentError(logName, this.constructor.name, 'update.id === undefined', {
update: update
});
const applicationDomainResponse: ApplicationDomainResponse = await ApplicationDomainsService.updateApplicationDomain({
xContextId,
id: update.id,
requestBody: update
});
/* istanbul ignore next */
if(applicationDomainResponse.data === undefined) throw new EpSdkApiContentError(logName, this.constructor.name, 'applicationDomainResponse.data === undefined', {
applicationDomainResponse: applicationDomainResponse
});
return applicationDomainResponse.data;
}

/**
* Sets the custom attributes in the list on the application domain.
* Creates attribute definitions / adds entity type 'applicationDomain' if it doesn't exist.
*/
public async setCustomAttributes({ xContextId, applicationDomainId, epSdkCustomAttributeList}:{
xContextId?: string;
applicationDomainId: string;
epSdkCustomAttributeList: TEpSdkCustomAttributeList;
}): Promise<ApplicationDomain> {
const applicationDomain: ApplicationDomain = await this.getById({ xContextId, applicationDomainId });
const customAttributes: Array<CustomAttribute> = await EpSdkCustomAttributesService.createCustomAttributesWithNew({
xContextId,
existingCustomAttributes: applicationDomain.customAttributes,
epSdkCustomAttributeList: epSdkCustomAttributeList,
epSdkCustomAttributeEntityType: EEpSdkCustomAttributeEntityTypes.APPLICATION_DOMAIN
});
return await this.updateApplicationDomain({
xContextId,
update: {
...applicationDomain,
customAttributes: customAttributes,
}
});
}

/**
* Unsets the custom attributes in the list on the application domain.
* Leaves attibute definitions as-is.
*/
public async unsetCustomAttributes({ xContextId, applicationDomainId, epSdkCustomAttributeList }:{
xContextId?: string;
applicationDomainId: string;
epSdkCustomAttributeList: TEpSdkCustomAttributeList;
}): Promise<ApplicationDomain> {
const applicationDomain: ApplicationDomain = await this.getById({ xContextId, applicationDomainId });
const customAttributes: Array<CustomAttribute> = await EpSdkCustomAttributesService.createCustomAttributesExcluding({
existingCustomAttributes: applicationDomain.customAttributes,
epSdkCustomAttributeList: epSdkCustomAttributeList,
});
return await this.updateApplicationDomain({
xContextId,
update: {
...applicationDomain,
customAttributes: customAttributes,
}
});
}

/**
* Removes the custom attribute association for application domains.
*/
public async removeAssociatedEntityTypeFromCustomAttributeDefinitions({ xContextId, customAttributeNames }: {
xContextId?: string;
customAttributeNames: Array<string>;
}): Promise<void> {
for(const customAttributeName of customAttributeNames) {
await EpSdkCustomAttributeDefinitionsService.removeAssociatedEntityTypeFromCustomAttributeDefinition({
xContextId,
attributeName: customAttributeName,
associatedEntityType: EEpSdkCustomAttributeEntityTypes.APPLICATION_DOMAIN,
});
}
}

public listAll = async({ pageSize = EpApiMaxPageSize, xContextId, attributesQuery }:{
xContextId?: string;
pageSize?: number; /** for testing */
attributesQuery?: IEpSdkAttributesQuery;
}): Promise<ApplicationDomainsResponse> => {
const funcName = 'listAll';
const logName = `${EpSdkApplicationDomainsServiceClass.name}.${funcName}()`;
Expand All @@ -38,7 +130,14 @@ export class EpSdkApplicationDomainsServiceClass extends EpSdkServiceClass {
});
if(applicationDomainsResponse.data === undefined || applicationDomainsResponse.data.length === 0) nextPage = undefined;
else {
applicationDomainList.push(...applicationDomainsResponse.data);
if(attributesQuery) {
for(const applicationDomain of applicationDomainsResponse.data) {
if(EpSdkCustomAttributesQueryService.resolve({
customAttributes: applicationDomain.customAttributes,
attributesQuery: attributesQuery,
})) applicationDomainList.push(applicationDomain);
}
} else applicationDomainList.push(...applicationDomainsResponse.data);
/* istanbul ignore next */
if(applicationDomainsResponse.meta === undefined) throw new EpSdkApiContentError(logName, this.constructor.name,'applicationDomainsResponse.meta === undefined', {
applicationDomainsResponse: applicationDomainsResponse
Expand Down
148 changes: 105 additions & 43 deletions packages/ep-sdk/test/specs/services/applicationDomainsService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import {
ApplicationDomainResponse,
ApplicationDomainsResponse,
ApplicationDomainsService,
CustomAttribute,
} from "@solace-labs/ep-openapi-node";
import { TestContext, TestUtils } from "@internal/tools/src";
import { TestLogger, TestConfig, TestHelpers } from "../../lib";
import {
EpSdkError,
EpSdkServiceError,
EpSdkApplicationDomainsService,
TEpSdkCustomAttribute,
IEpSdkAttributesQuery,
EEpSdkComparisonOps,
} from "../../../src";

const scriptName: string = path.basename(__filename);
Expand All @@ -26,6 +30,27 @@ let ApplicationDomainName: string;
let ApplicationDomainId: string | undefined;
let ApplicationDomainIdList: Array<string> = [];

const CorrectAttribute: TEpSdkCustomAttribute = {
name: `${TestSpecName}.APPS`,
value: "ep-developer-portal",
};
const AnotherAttribute: TEpSdkCustomAttribute = {
name: `${TestSpecName}.another`,
value: "another value",
};

const CorrectAttributesQuery: IEpSdkAttributesQuery = {
AND: {
queryList: [
{
attributeName: CorrectAttribute.name,
comparisonOp: EEpSdkComparisonOps.CONTAINS,
value: CorrectAttribute.value,
},
],
},
};

const recordApplicationDomainId = (applicationDomainId: string) => {
ApplicationDomainId = applicationDomainId;
ApplicationDomainIdList.push(applicationDomainId);
Expand Down Expand Up @@ -71,7 +96,7 @@ describe(`${scriptName}`, () => {
name: ApplicationDomainName,
},
});
recordApplicationDomainId(applicationDomainResponse.data.id);
recordApplicationDomainId(applicationDomainResponse.data.id);
// // DEBUG
// expect(false, `ApplicationDomainId=${ApplicationDomainId}`).to.be.true;
} catch (e) {
Expand All @@ -85,54 +110,91 @@ describe(`${scriptName}`, () => {
}
});

it(`${scriptName}: should list all application domains`, async () => {
it(`${scriptName}: should create custom attribute for the application domain`, async () => {
try {
const applicationDomainsResponse: ApplicationDomainsResponse =
await EpSdkApplicationDomainsService.listAll({ pageSize: 5 });
const message = `applicationDomainsResponse=\n${JSON.stringify(
applicationDomainsResponse,
null,
2
)}`;
expect(
applicationDomainsResponse.meta.pagination.count,
TestLogger.createApiTestFailMessage(message)
).to.be.greaterThanOrEqual(1);
expect(
applicationDomainsResponse.data,
TestLogger.createApiTestFailMessage(message)
).to.not.be.undefined;
expect(
applicationDomainsResponse.data.length,
TestLogger.createApiTestFailMessage(message)
).to.be.greaterThanOrEqual(1);
const found = applicationDomainsResponse.data.find((x) => {
return x.id === ApplicationDomainId;
const applicationDomain: ApplicationDomain = await EpSdkApplicationDomainsService.setCustomAttributes({
applicationDomainId: ApplicationDomainId,
epSdkCustomAttributeList: [
CorrectAttribute,
AnotherAttribute,
],
});
expect(found, TestLogger.createApiTestFailMessage(message)).to.not.be
.undefined;
// test it is set
expect(applicationDomain.customAttributes).to.not.be.undefined;
const found: CustomAttribute | undefined = applicationDomain.customAttributes.find( (customAttribute: CustomAttribute) => {
return ( customAttribute.customAttributeDefinitionName === CorrectAttribute.name );
});
expect(found, `applicationDomain=${JSON.stringify(applicationDomain, null, 2)}`).to.not.be.undefined;
// // DEBUG
// expect(false, `ApplicationDomainId=${ApplicationDomainId}`).to.be.true;
} catch (e) {
if (e instanceof ApiError) expect(false, TestLogger.createApiTestFailMessage("failed", e)).to.be.true;
expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)).to.be.true;
expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be.true;
}
});

it(`${scriptName}: should list all application domains`, async () => {
try {
const applicationDomainsResponse: ApplicationDomainsResponse = await EpSdkApplicationDomainsService.listAll({ pageSize: 5 });
const message = `applicationDomainsResponse=\n${JSON.stringify(applicationDomainsResponse, null, 2 )}`;
expect(applicationDomainsResponse.meta.pagination.count, TestLogger.createApiTestFailMessage(message)).to.be.greaterThanOrEqual(1);
expect(applicationDomainsResponse.data, TestLogger.createApiTestFailMessage(message)).to.not.be.undefined;
expect(applicationDomainsResponse.data.length, TestLogger.createApiTestFailMessage(message)).to.be.greaterThanOrEqual(1);
const found = applicationDomainsResponse.data.find((x) => { return x.id === ApplicationDomainId; });
expect(found, TestLogger.createApiTestFailMessage(message)).to.not.be.undefined;
const firstCount = applicationDomainsResponse.meta.pagination.count;
const secondApplicationDomainsResponse: ApplicationDomainsResponse =
await EpSdkApplicationDomainsService.listAll({});
expect(
secondApplicationDomainsResponse.meta.pagination.count,
TestLogger.createApiTestFailMessage(
`secondApplicationDomainsResponse=\n${JSON.stringify(
secondApplicationDomainsResponse,
null,
2
)}`
)
).to.equal(firstCount);
const secondApplicationDomainsResponse: ApplicationDomainsResponse = await EpSdkApplicationDomainsService.listAll({});
expect(secondApplicationDomainsResponse.meta.pagination.count, TestLogger.createApiTestFailMessage(`secondApplicationDomainsResponse=\n${JSON.stringify(secondApplicationDomainsResponse, null, 2 )}`)).to.equal(firstCount);
// // DEBUG
// expect(false, TestLogger.createApiTestFailMessage(message)).to.be.true;
} catch (e) {
if (e instanceof ApiError)
expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true;
expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e))
.to.be.true;
expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be
.true;
if (e instanceof ApiError) expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true;
expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)).to.be.true;
expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be.true;
}
});

it(`${scriptName}: should list all application domains filtering by custom attribute`, async () => {
try {
const applicationDomainsResponse: ApplicationDomainsResponse = await EpSdkApplicationDomainsService.listAll({ attributesQuery: CorrectAttributesQuery });
const message = `applicationDomainsResponse=\n${JSON.stringify(applicationDomainsResponse, null, 2 )}`;
expect(applicationDomainsResponse.meta.pagination.count, TestLogger.createApiTestFailMessage(message)).to.be.equal(1);
expect(applicationDomainsResponse.data, TestLogger.createApiTestFailMessage(message)).to.not.be.undefined;
expect(applicationDomainsResponse.data.length, TestLogger.createApiTestFailMessage(message)).to.be.equal(1);
const found = applicationDomainsResponse.data.find((x) => { return x.id === ApplicationDomainId; });
expect(found, TestLogger.createApiTestFailMessage(message)).to.not.be.undefined;
// // DEBUG
// expect(false, TestLogger.createApiTestFailMessage(message)).to.be.true;
} catch (e) {
if (e instanceof ApiError) expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true;
expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)).to.be.true;
expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be.true;
}
});

it(`${scriptName}: should unset custom attributes from application domain`, async () => {
try {
const applicationDomain: ApplicationDomain = await EpSdkApplicationDomainsService.unsetCustomAttributes({
applicationDomainId: ApplicationDomainId,
epSdkCustomAttributeList: [
CorrectAttribute,
AnotherAttribute,
],
});
// test no custom attributes left
expect(applicationDomain.customAttributes.length, 'wrong length').to.equal(0);
// remove association
await EpSdkApplicationDomainsService.removeAssociatedEntityTypeFromCustomAttributeDefinitions({
customAttributeNames: [
CorrectAttribute.name,
AnotherAttribute.name
]
});
} catch (e) {
if (e instanceof ApiError) expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true;
expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)).to.be.true;
expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be.true;
}
});

Expand Down
Loading

0 comments on commit 02bedf1

Please sign in to comment.