Skip to content
This repository has been archived by the owner on Mar 13, 2021. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrauskopf committed Sep 17, 2020
1 parent 382b8c9 commit fe782e3
Show file tree
Hide file tree
Showing 6 changed files with 596 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/core/test-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { RateShipmentWithAllServices } from './test-app/tests/rate-shipment-with
import { CancelShipment } from './test-app/tests/cancel-shipment';
import { SameDayPickup } from './test-app/tests/same-day-pickup';
import { NextDayPickup } from './test-app/tests/next-day-pickup';
import { CancelSameDayPickup } from './test-app/tests/cancel-same-day-pickup';

interface TesOptions {
debug?: boolean;
Expand Down Expand Up @@ -154,9 +155,9 @@ function registerTestSuiteModules(app: SdkApp): RegisteredTestSuiteModules {
CreateShipmentDomestic,
CreateShipmentMultiPackage,
CreateShipmentWithInsurance,
CreateShipmentWithInsurance,
CreateShipmentReturn
],
cancelPickups: [CancelSameDayPickup],
schedulePickup: [SameDayPickup, NextDayPickup],
rateShipment: [
RateShipment,
Expand Down
4 changes: 2 additions & 2 deletions src/core/test-app/runner/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { CancelShipmentConfigOptions } from './config/cancel-shipment';
import { CreateShipmentReturnConfigOptions } from './config/create-shipment-return';
import { SameDayPickupConfigOptions } from './config/same-day-pickup';
import { NextDayPickupConfigOptions } from './config/next-day-pickup';
import { CancelSameDayPickupConfigOptions } from './config/cancel-same-day-pickup';

export interface TestsConfig {
// cancelPickups?: (TestOptions & TestOptions) | [TestOptions];
cancelShipment?: CancelShipmentConfigOptions | [CancelShipmentConfigOptions];
// createManifest?: TestOptions | [TestOptions];
createShipment_domestic?:
Expand All @@ -28,7 +28,7 @@ export interface TestsConfig {
rateShipment_with_all_services?: RateShipmentWithAllServicesConfigOptions | [RateShipmentWithAllServicesConfigOptions];
schedulePickup_same_day?: SameDayPickupConfigOptions | [SameDayPickupConfigOptions];
schedulePickup_next_day?: NextDayPickupConfigOptions | [NextDayPickupConfigOptions];

cancelPickups_same_day?: CancelSameDayPickupConfigOptions | [CancelSameDayPickupConfigOptions];
// createShipment_multi_package?: TestOptions | [TestOptions];
// rateShipmentWithOneService?: RateShipmentConfigOptions | [RateShipmentConfigOptions];
// trackShipment?: TestOptions | [TestOptions];
Expand Down
35 changes: 35 additions & 0 deletions src/core/test-app/runner/config/cancel-same-day-pickup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
TimeRangePOJO,
ContactInfoPOJO,
AddressPOJO,
WeightPOJO,
DimensionsPOJO,
PickupCancellationReason
} from "@shipengine/connect-sdk";
import { BaseTestConfigOptions } from "./base-test-config-options";


export type PickupPackageConfig = {
packagingName: string;
dimensions?: DimensionsPOJO;
weight?: WeightPOJO;
};

export type PickupShipmentConfig = {
deliveryServiceName: string;
packages: PickupPackageConfig[];
};

export interface CancelSameDayPickupTestParams {
pickupServiceName: string;
deliveryServiceName: string;
address: AddressPOJO;
contact: ContactInfoPOJO;
timeWindow: TimeRangePOJO;
shipments: PickupShipmentConfig[];
cancellationReason: PickupCancellationReason;
}

export interface CancelSameDayPickupConfigOptions
extends CancelSameDayPickupTestParams,
BaseTestConfigOptions {}
33 changes: 30 additions & 3 deletions src/core/test-app/runner/validate-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable camelcase */
import Joi from "joi";
import Config from "./config";
import ono from "@jsdevtools/ono";
import {
Expand All @@ -9,10 +8,12 @@ import {
ContactInfo,
TimeRange,
Dimensions,
Address,
Address, Joi
} from "@shipengine/connect-sdk/lib/internal/common";
import { NewLabel } from "@shipengine/connect-sdk/lib/internal/carriers/documents/new-label";
import { _internal, MonetaryValue } from "@shipengine/connect-sdk/lib/internal/common";
import { PickupCancellationReason } from '@shipengine/connect-sdk';
import { ValidationOptions } from 'joi';

const joiOptions = {
abortEarly: false,
Expand Down Expand Up @@ -169,6 +170,26 @@ const SameDayPickupTestParamsSchema = Joi.object({
}
});

const CancelPickupsSamedayTestParamsSchema = Joi.object({
...baseTestParamValidations,
...{
pickupServiceName: Joi.string().optional(),
deliveryServiceName: Joi.string().optional(),
address: Address[_internal].schema.optional(),
contact: ContactInfo[_internal].schema.optional(),
timeWindow: TimeRange[_internal].schema.optional(),
shipments: Joi.object({
deliveryServiceName: Joi.string(),
packages: Joi.array().items(Joi.object({
packagingName: Joi.string(),
dimensions: Dimensions[_internal].schema.optional(),
weight: Weight[_internal].schema.optional()
}))
}),
cancellationReason: Joi.string().enum(PickupCancellationReason)
}
});

const NextDayPickupTestParamsSchema = Joi.object({
...baseTestParamValidations,
...{
Expand Down Expand Up @@ -239,6 +260,12 @@ const testsSchema = Joi.object({
then: Joi.array().items(RateShipmentWithAllServicesTestParamsSchema),
otherwise: RateShipmentWithAllServicesTestParamsSchema,
}),

cancelPickups_same_day: Joi.alternatives().conditional(Joi.array(), {
then: Joi.array().items(CancelPickupsSamedayTestParamsSchema),
otherwise: CancelPickupsSamedayTestParamsSchema,
}),

schedulePickup_same_day: Joi.alternatives().conditional(Joi.array(), {
then: Joi.array().items(SameDayPickupTestParamsSchema),
otherwise: SameDayPickupTestParamsSchema,
Expand All @@ -265,7 +292,7 @@ const schema = Joi.object().keys({
});

const validateConfig = (config: Config): Config => {
const { error, value } = schema.validate(config, joiOptions as Joi.ValidationOptions);
const { error, value } = schema.validate(config, joiOptions as ValidationOptions);

if (error) {
throw ono(error, { code: "ERR_CONNECT_CONFIG_SCHEMA" });
Expand Down
211 changes: 211 additions & 0 deletions src/core/test-app/tests/cancel-same-day-pickup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import { DeliveryService, PickupService, LengthUnit, WeightUnit, PickupCancellationReason } from "@shipengine/connect-sdk";
import { CarrierApp, PickupRequestPOJO, PickupShipmentPOJO, PickupPackagePOJO, PickupCancellationPOJO } from "@shipengine/connect-sdk/lib/internal";
import Suite from "../runner/suite";
import { initializeTimeStamps } from "../../utils/time-stamps";
import { CancelSameDayPickupTestParams, CancelSameDayPickupConfigOptions } from "../runner/config/cancel-same-day-pickup";
import reduceDefaultsWithConfig from "../utils/reduce-defaults-with-config";
import objectToTestTitle from "../utils/object-to-test-title";
import useShipmentAddresses from '../utils/use-shipment-addresses';
import findDeliveryServiceByName from '../utils/find-delivery-service-by-name';
import findPickupServiceByName from '../utils/find-pickup-service-by-name';
import { findDomesticDeliveryService } from '../utils/find-domestic-delivery-service';
import Test from '../runner/test';
import { buildAddress } from '../factories/address';
import findPackagingByName from '../utils/find-packaging-by-name';
import { v4 } from "uuid";
import { expect } from "chai";

interface TestArgs {
title: string;
methodArgs: PickupRequestPOJO;
config: unknown;
testParams: CancelSameDayPickupTestParams;
}

export class CancelSameDayPickup extends Suite {
title = "cancelPickups_same_day";

private deliveryService: DeliveryService | undefined;

private pickupService: PickupService | undefined;

private setDeliveryService(
config: CancelSameDayPickupConfigOptions,
): void {
const carrierApp = this.app as CarrierApp;

if (config.deliveryServiceName) {
this.deliveryService = findDeliveryServiceByName(
config.deliveryServiceName,
carrierApp,
);
} else {
try {
this.deliveryService = findDomesticDeliveryService(carrierApp);
} catch {
this.deliveryService = undefined;
}
}
}

private setPickupService(config: CancelSameDayPickupConfigOptions): void {
const carrierApp = this.app as CarrierApp;

if (config.pickupServiceName) {
this.pickupService = findPickupServiceByName(config.pickupServiceName, carrierApp);
}

else if (carrierApp.pickupServices.length > 0) {
this.pickupService = carrierApp.pickupServices[0];
}
}

buildTestArg(config: CancelSameDayPickupConfigOptions): TestArgs | undefined {
const carrierApp = this.app as CarrierApp;
this.setPickupService(config);
this.setDeliveryService(config);

if (!this.deliveryService || !this.pickupService) return undefined;
const [shipFrom, shipTo] = useShipmentAddresses(this.deliveryService);

if (!shipTo || !shipFrom) return undefined;

const address = buildAddress(`${shipFrom.country}-from`);

const { todayEarly, todayEvening } = initializeTimeStamps();

const defaults: CancelSameDayPickupTestParams = {
pickupServiceName: this.pickupService.name,
deliveryServiceName: this.deliveryService.name,
address,
contact: { name: "John Doe" },
timeWindow: {
startDateTime: todayEarly,
endDateTime: todayEvening
},
shipments: [{
deliveryServiceName: this.deliveryService.name,
packages: [
{
packagingName: this.deliveryService.packaging[0].name,
dimensions: {
length: 12,
width: 12,
height: 12,
unit: LengthUnit.Inches
},
weight: {
unit: WeightUnit.Pounds,
value: 5.0
}
}
]
}],
cancellationReason: PickupCancellationReason.NotReady
};

const testParams = reduceDefaultsWithConfig<
CancelSameDayPickupTestParams
>(defaults, config);

const shipments: PickupShipmentPOJO[] = testParams.shipments.map((shipmentParams) => {
const shipment: PickupShipmentPOJO = {
deliveryService: findDeliveryServiceByName(shipmentParams.deliveryServiceName, carrierApp),
packages: shipmentParams.packages.map((pkgParams) => {
const pkg: PickupPackagePOJO = {
packaging: findPackagingByName(pkgParams.packagingName, carrierApp),
dimensions: pkgParams.dimensions,
weight: pkgParams.weight
};

return pkg;
})
}

return shipment;
});

const rateCriteriaPOJO: PickupRequestPOJO = {
pickupService: findPickupServiceByName(testParams.pickupServiceName, carrierApp),
address: testParams.address,
timeWindow: testParams.timeWindow,
contact: testParams.contact,
shipments
};

const title = config.expectedErrorMessage
? `it raises an error when scheduling a pickup with ${objectToTestTitle(
testParams,
)}`
: `it schedules a pickup with ${objectToTestTitle(
testParams,
)}`;

return {
title,
methodArgs: rateCriteriaPOJO,
config,
testParams
};
}

buildTestArgs(): Array<TestArgs | undefined> {
if (Array.isArray(this.config)) {
return this.config.map((config: CancelSameDayPickupConfigOptions) => {
return this.buildTestArg(config);
});
}

const config = this.config as CancelSameDayPickupConfigOptions;
return [this.buildTestArg(config)];
}

tests(): Test[] {
const testArgs = this.buildTestArgs().filter(args => args !== undefined) as TestArgs[];

if (testArgs.length === 0) {
return [];
}
return testArgs.map((testArg) => {
return this.test(
testArg.title,
testArg.methodArgs,
testArg.config,
async () => {
const carrierApp = this.app as CarrierApp;

const transaction = await this.transaction(testArg.config);

// This should never actually throw because we handle this case up stream.
if (!carrierApp.schedulePickup) {
throw new Error("schedulePickup is not implemented");
}

// This should never actually throw because we handle this case up stream.
if (!carrierApp.cancelPickups) {
throw new Error("cancelPickups is not implemented");
}

await carrierApp.schedulePickup(transaction, testArg.methodArgs);

const cancellationID = v4();

const pickupCancellation: PickupCancellationPOJO[] = [{
cancellationID,
id: v4(),
pickupService: testArg.methodArgs.pickupService,
reason: testArg.testParams.cancellationReason,
address: testArg.methodArgs.address,
contact: testArg.methodArgs.contact,
timeWindows: [testArg.methodArgs.timeWindow],
shipments: testArg.methodArgs.shipments,
}]

const pickupCancelOutcome = await carrierApp.cancelPickups(transaction, pickupCancellation);
const customMsg = `The pickupCancellationOutcome cancellationID does not match the cancellationID that was included in the PickupCancellation parameter: ${cancellationID}`;
expect(pickupCancelOutcome[0].cancellationID).to.equal(cancellationID, customMsg);
}
);
});
}
}
Loading

0 comments on commit fe782e3

Please sign in to comment.