Skip to content

Commit

Permalink
fix(tests): fix all tests vs DHL sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Baulig committed Apr 30, 2024
1 parent 51ee979 commit ea92940
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 65 deletions.
4 changes: 2 additions & 2 deletions cfg/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"INVALID": "fulfillmentInvalid",
"SUBMITTED": "fulfillmentSubmitted",
"IN_TRANSFER": "fulfillmentInTransfer",
"FULFILLED": "fulfillmentFulfilled",
"COMPLETED": "fulfillmentCompleted",
"WITHDRAWN": "fulfillmentWithdrawn",
"CANCELLED": "fulfillmentCancelled",
"CREATE_INVOICES": "createInvoices"
Expand All @@ -101,7 +101,7 @@
"fulfillmentInTransfer": {
"messageObject": "io.restorecommerce.fulfillment.Fulfillment"
},
"fulfillmentFulfilled": {
"fulfillmentCompleted": {
"messageObject": "io.restorecommerce.fulfillment.Fulfillment"
},
"fulfillmentFailed": {
Expand Down
124 changes: 68 additions & 56 deletions src/stubs/dhl_soap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@ type ClientMap = { [id: string]: soap.Client };

interface Origin
{
country: string;
countryISOCode: string;
'cis:country': string;
'cis:countryISOCode': string;
}

interface Communication
{
phone?: string;
email?: string;
contactPerson?: string;
'cis:phone'?: string;
'cis:email'?: string;
'cis:contactPerson'?: string;
}

interface Shipper
{
Name: {
name1: string;
name2?: string;
name3?: string;
'cis:name1': string;
'cis:name2'?: string;
'cis:name3'?: string;
};
Address: {
streetName: string;
streetNumber: string;
zip: string;
city: string;
Origin: Origin;
'cis:streetName': string;
'cis:streetNumber': string;
'cis:zip': string;
'cis:city': string;
'cis:Origin': Origin;
};
Communication: Communication;
}
Expand All @@ -58,11 +58,11 @@ interface Receiver
Address: {
name2?: string;
name3?: string;
streetName: string;
streetNumber: string;
zip: string;
city: string;
Origin: Origin;
'cis:streetName': string;
'cis:streetNumber': string;
'cis:zip': string;
'cis:city': string;
'cis:Origin': Origin;
};
Communication: Communication;
}
Expand Down Expand Up @@ -421,8 +421,8 @@ class DHLSoap extends Stub {
}

return fulfillments.map((fulfillment, i) => {
const dhl_state = response?.CreationFulfillmentState?.find((state: any) => state.sequenceNumber === (i + 1).toString());
const code = dhl_state?.LabelData.Status.statusCode;
const dhl_state = response?.CreationState?.find((state: any) => state.sequenceNumber === (i + 1).toString());
const code = dhl_state?.LabelData?.Status?.statusCode;
const state = code === 0 ? FulfillmentState.SUBMITTED : FulfillmentState.INVALID;
const status = this.DHLCode2StatusCode(
code,
Expand Down Expand Up @@ -466,44 +466,44 @@ class DHLSoap extends Stub {
Shipment: {
Shipper: {
Name: {
name1: packaging.sender.address.residential_address?.family_name ?? packaging.sender.address.business_address?.name,
name2: packaging.sender.address.residential_address?.given_name,
name3: packaging.sender.address.residential_address?.mid_name,
'cis:name1': packaging.sender.address.residential_address?.family_name ?? packaging.sender.address.business_address?.name,
'cis:name2': packaging.sender.address.residential_address?.given_name,
'cis:name3': packaging.sender.address.residential_address?.mid_name,
},
Address: {
streetName: packaging.sender.address?.street,
streetNumber: packaging.sender.address?.building_number,
zip: packaging.sender.address?.postcode,
city: packaging.sender.address?.region,
Origin: {
country: request.sender_country?.name,
countryISOCode: request.sender_country?.country_code
'cis:streetName': packaging.sender.address?.street,
'cis:streetNumber': packaging.sender.address?.building_number,
'cis:zip': packaging.sender.address?.postcode,
'cis:city': packaging.sender.address?.region,
'cis:Origin': {
'cis:country': request.sender_country?.name,
'cis:countryISOCode': request.sender_country?.country_code
}
},
Communication: {
contactPerson: packaging.sender?.contact?.name,
email: packaging.sender?.contact.email,
phone: packaging.sender?.contact.phone,
'cis:contactPerson': packaging.sender?.contact?.name,
'cis:email': packaging.sender?.contact.email,
'cis:phone': packaging.sender?.contact.phone,
}
},
Receiver: {
name1: packaging.recipient.address.residential_address?.family_name ?? packaging.recipient.address.business_address?.name,
Address: {
name2: packaging.recipient.address.residential_address?.given_name,
name3: packaging.recipient.address.residential_address?.mid_name,
streetName: packaging.recipient.address?.street,
streetNumber: packaging.recipient.address?.building_number,
zip: packaging.recipient.address?.postcode,
city: packaging.recipient.address?.region,
Origin: {
country: request.recipient_country?.name,
countryISOCode: request.recipient_country?.country_code
'cis:streetName': packaging.recipient.address?.street,
'cis:streetNumber': packaging.recipient.address?.building_number,
'cis:zip': packaging.recipient.address?.postcode,
'cis:city': packaging.recipient.address?.region,
'cis:Origin': {
'cis:country': request.recipient_country?.name,
'cis:countryISOCode': request.recipient_country?.country_code
},
},
Communication: {
contactPerson: packaging.recipient.contact?.name,
email: packaging.recipient.contact?.email,
phone: packaging.recipient.contact?.phone,
'cis:contactPerson': packaging.recipient.contact?.name,
'cis:email': packaging.recipient.contact?.email,
'cis:phone': packaging.recipient.contact?.phone,
}
},
ShipmentDetails: {
Expand Down Expand Up @@ -589,13 +589,18 @@ class DHLSoap extends Stub {
if (fulfillments.length === 0) return [];
const dhl_order_request = this.AggregatedFulfillmentRequests2DHLShipmentOrderRequest(fulfillments);
const client = await this.registerSoapClient();
return new Promise<FlatAggregatedFulfillment[]>((resolve, reject): void => {
return await new Promise<FlatAggregatedFulfillment[]>((resolve, reject): void => {
const timer = setTimeout(reject, 30000, this.operation_status_codes.TIMEOUT);
client.GVAPI_2_0_de.GKVAPISOAP11port0.createShipmentOrder(
dhl_order_request,
(error: any, result: any): any => {
clearTimeout(timer);
resolve(this.DHLShipmentLabels2FulfillmentResponses(fulfillments, result, error));
try {
resolve(this.DHLShipmentLabels2FulfillmentResponses(fulfillments, result, error));
}
catch (e: any) {
reject(e);
}
}
);
});
Expand Down Expand Up @@ -627,16 +632,16 @@ class DHLSoap extends Stub {
const params = new URLSearchParams();
params.append('xml', xml);
const payload = {
method: 'post',
method: 'get',
headers: {
Host: 'cig.dhl.de',
Authorization: auth,
Connection: 'Keep-Alive',
},
body: params,
// body: params,
};

return await fetch(client.endpoint, payload).then(
return await fetch(`${client.endpoint}?${params}`, payload).then(
response => DHLTracking2FulfillmentTracking(item, response),
err => {
this.logger?.error(`${this.constructor.name}: ${err}`);
Expand Down Expand Up @@ -666,20 +671,27 @@ class DHLSoap extends Stub {
const dhl_cancel_request = this.AggregatedFulfillment2DHLShipmentCancelRequest(fulfillments);
const client = await this.registerSoapClient();
return await new Promise<FlatAggregatedFulfillment[]>((resolve, reject: (v: FlatAggregatedFulfillment[]) => void): void => {
const timer = setTimeout(reject, 30000, this.operation_status_codes.TIMEOUT);
client.GVAPI_2_0_de.GKVAPISOAP11port0.deleteShipmentOrder(dhl_cancel_request,
(err: any, result: any): any => {
if (err) {
if (result?.html) {
this.logger?.error(`${this.constructor.name}: ${result.html.head.title}`);
reject(DHLShipmentCancelResponse2AggregatedFulfillment(fulfillment_map, null, result.html.head.title));
clearTimeout(timer);
try {
if (err) {
if (result?.html) {
this.logger?.error(`${this.constructor.name}: ${result.html.head.title}`);
reject(DHLShipmentCancelResponse2AggregatedFulfillment(fulfillment_map, null, result.html.head.title));
}
else {
this.logger?.error(`${this.constructor.name}: ${err}`);
reject(DHLShipmentCancelResponse2AggregatedFulfillment(fulfillment_map, null, err));
}
}
else {
this.logger?.error(`${this.constructor.name}: ${err}`);
reject(DHLShipmentCancelResponse2AggregatedFulfillment(fulfillment_map, null, err));
resolve(DHLShipmentCancelResponse2AggregatedFulfillment(fulfillment_map, result));
}
}
else {
resolve(DHLShipmentCancelResponse2AggregatedFulfillment(fulfillment_map, result));
catch (e: any) {
reject(e);
}
});
});
Expand Down
37 changes: 30 additions & 7 deletions test/fulfillment-srv-dhl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ describe('Testing Fulfillment Service Cluster:', () => {
FulfillmentServiceDefinition,
createChannel(cfg.get('client:fulfillment:address'))
) as Client<FulfillmentServiceDefinition>;

await Promise.allSettled([
courier_client?.delete({
collection: true,
subject: {
id: 'superadmin',
token: 'superadmin',
},
}),
product_client?.delete({
collection: true,
subject: {
id: 'superadmin',
token: 'superadmin',
},
}),
fulfillment_client?.delete({
collection: true,
subject: {
id: 'superadmin',
token: 'superadmin',
},
}),
]);
});

after(async function() {
Expand Down Expand Up @@ -191,7 +215,7 @@ describe('Testing Fulfillment Service Cluster:', () => {
describe('The Fulfillment Service:', () => {
const fulfillmentCreatedSemaphore = new Semaphore(0);
const fulfillmentSubmittedSemaphore = new Semaphore(0);
const fulfillmentFulfilledSemaphore = new Semaphore(0);
const fulfillmentCompletedSemaphore = new Semaphore(0);
const fulfillmentWithdrawnSemaphore = new Semaphore(0);
const fulfillmentCancelledSemaphore = new Semaphore(0);

Expand All @@ -205,9 +229,9 @@ describe('Testing Fulfillment Service Cluster:', () => {
fulfillmentSubmittedSemaphore.release(1);
};

const onFulfillmentFulfilled = (msg: Fulfillment, context?:any): void => {
const onFulfillmentCompleted = (msg: Fulfillment, context?:any): void => {
should.equal(msg?.fulfillment_state, FulfillmentState.COMPLETED);
fulfillmentFulfilledSemaphore.release(1);
fulfillmentCompletedSemaphore.release(1);
};

const onFulfillmentWithdrawn = (msg: Fulfillment, context?:any): void => {
Expand All @@ -225,7 +249,7 @@ describe('Testing Fulfillment Service Cluster:', () => {
await Promise.all([
topics.on('fulfillmentCreated', onFulfillmentCreated),
topics.on('fulfillmentSubmitted', onFulfillmentSubmitted),
topics.on('fulfillmentFulfilled', onFulfillmentFulfilled),
topics.on('fulfillmentCompleted', onFulfillmentCompleted),
topics.on('fulfillmentWithdrawn', onFulfillmentWithdrawn),
topics.on('fulfillmentCancelled', onFulfillmentCancelled),
]);
Expand All @@ -236,7 +260,7 @@ describe('Testing Fulfillment Service Cluster:', () => {
await Promise.all([
topics.removeListener('fulfillmentCreated', onFulfillmentCreated),
topics.removeListener('fulfillmentSubmitted', onFulfillmentSubmitted),
topics.removeListener('fulfillmentFulfilled', onFulfillmentFulfilled),
topics.removeListener('fulfillmentCompleted', onFulfillmentCompleted),
topics.removeListener('fulfillmentWithdrawn', onFulfillmentWithdrawn),
topics.removeListener('fulfillmentCancelled', onFulfillmentCancelled),
]);
Expand Down Expand Up @@ -271,7 +295,6 @@ describe('Testing Fulfillment Service Cluster:', () => {
it(`should submit fulfillment by valid samples: ${sample_name}`, async function() {
this.timeout(60000);
const response = await fulfillment_client.submit(sample);
console.log(JSON.stringify(response, null, 2));
should.equal(
response?.operationStatus?.code, 200,
'response.operationStatus.code expected to be 200',
Expand Down Expand Up @@ -316,7 +339,7 @@ describe('Testing Fulfillment Service Cluster:', () => {

it(`should have received fulfillment tracking event for ${sample_name}`, async function() {
this.timeout(5000);
await fulfillmentFulfilledSemaphore.acquire(1);
await fulfillmentCompletedSemaphore.acquire(1);
});
}

Expand Down

0 comments on commit ea92940

Please sign in to comment.