From: Jérôme Benoit Date: Fri, 26 Jul 2024 15:39:58 +0000 (+0200) Subject: test: add some tests for charging station helpers X-Git-Tag: ocpp-server@v1.5.1~13 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=df5992062c1e0975e1e159f7fa7b81bcc263bd27;p=e-mobility-charging-stations-simulator.git test: add some tests for charging station helpers Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 02419444..8f442f46 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -202,10 +202,12 @@ export class AutomaticTransactionGenerator { } const wait = secondsToMilliseconds( randomInt( - this.chargingStation.getAutomaticTransactionGeneratorConfiguration() - ?.minDelayBetweenTwoTransactions, - this.chargingStation.getAutomaticTransactionGeneratorConfiguration() - ?.maxDelayBetweenTwoTransactions + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.chargingStation.getAutomaticTransactionGeneratorConfiguration()! + .minDelayBetweenTwoTransactions, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.chargingStation.getAutomaticTransactionGeneratorConfiguration()! + .maxDelayBetweenTwoTransactions ) ) logger.info(`${this.logPrefix(connectorId)} waiting for ${formatDurationMilliSeconds(wait)}`) @@ -224,8 +226,10 @@ export class AutomaticTransactionGenerator { // Wait until end of transaction const waitTrxEnd = secondsToMilliseconds( randomInt( - this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.minDuration, - this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.maxDuration + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.minDuration, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.maxDuration ) ) logger.info( diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index e8ef83c4..9b2d8370 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -176,46 +176,56 @@ export const getHashId = (index: number, stationTemplate: ChargingStationTemplat } export const validateStationInfo = (chargingStation: ChargingStation): void => { - if (isEmpty(chargingStation.stationInfo)) { + if (chargingStation.stationInfo == null || isEmpty(chargingStation.stationInfo)) { throw new BaseError('Missing charging station information') } - if (isEmpty(chargingStation.stationInfo?.chargingStationId?.trim())) { + if ( + chargingStation.stationInfo.chargingStationId == null || + isEmpty(chargingStation.stationInfo.chargingStationId.trim()) + ) { throw new BaseError('Missing chargingStationId in stationInfo properties') } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const chargingStationId: string = chargingStation.stationInfo!.chargingStationId! - if (isEmpty(chargingStation.stationInfo?.hashId.trim())) { + const chargingStationId = chargingStation.stationInfo.chargingStationId + if ( + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + chargingStation.stationInfo.hashId == null || + isEmpty(chargingStation.stationInfo.hashId.trim()) + ) { throw new BaseError(`${chargingStationId}: Missing hashId in stationInfo properties`) } - if (isEmpty(chargingStation.stationInfo?.templateIndex)) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (chargingStation.stationInfo.templateIndex == null) { throw new BaseError(`${chargingStationId}: Missing templateIndex in stationInfo properties`) } - if (isEmpty(chargingStation.stationInfo?.templateName.trim())) { + if (chargingStation.stationInfo.templateIndex <= 0) { + throw new BaseError( + `${chargingStationId}: Invalid templateIndex value in stationInfo properties` + ) + } + if ( + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + chargingStation.stationInfo.templateName == null || + isEmpty(chargingStation.stationInfo.templateName.trim()) + ) { throw new BaseError(`${chargingStationId}: Missing templateName in stationInfo properties`) } - if (isEmpty(chargingStation.stationInfo?.maximumPower)) { + if (chargingStation.stationInfo.maximumPower == null) { throw new BaseError(`${chargingStationId}: Missing maximumPower in stationInfo properties`) } - if ( - chargingStation.stationInfo?.maximumPower != null && - chargingStation.stationInfo.maximumPower <= 0 - ) { + if (chargingStation.stationInfo.maximumPower <= 0) { throw new RangeError( `${chargingStationId}: Invalid maximumPower value in stationInfo properties` ) } - if (isEmpty(chargingStation.stationInfo?.maximumAmperage)) { + if (chargingStation.stationInfo.maximumAmperage == null) { throw new BaseError(`${chargingStationId}: Missing maximumAmperage in stationInfo properties`) } - if ( - chargingStation.stationInfo?.maximumAmperage != null && - chargingStation.stationInfo.maximumAmperage <= 0 - ) { + if (chargingStation.stationInfo.maximumAmperage <= 0) { throw new RangeError( `${chargingStationId}: Invalid maximumAmperage value in stationInfo properties` ) } - switch (chargingStation.stationInfo?.ocppVersion) { + switch (chargingStation.stationInfo.ocppVersion) { case OCPPVersion.VERSION_20: case OCPPVersion.VERSION_201: if (chargingStation.evses.size === 0) { diff --git a/tests/charging-station/Helpers.test.ts b/tests/charging-station/Helpers.test.ts new file mode 100644 index 00000000..2a3c799c --- /dev/null +++ b/tests/charging-station/Helpers.test.ts @@ -0,0 +1,114 @@ +import { describe, it } from 'node:test' + +import { expect } from 'expect' + +import { + getChargingStationId, + getHashId, + validateStationInfo, +} from '../../src/charging-station/Helpers.js' +import type { ChargingStation } from '../../src/charging-station/index.js' +import { BaseError } from '../../src/exception/index.js' +import { + type ChargingStationInfo, + type ChargingStationTemplate, + type EvseStatus, + OCPPVersion, +} from '../../src/types/index.js' + +await describe('Helpers test suite', async () => { + const baseName = 'CS-TEST' + const chargingStationTemplate = { + baseName, + } as ChargingStationTemplate + const chargingStation = {} as ChargingStation + + await it('Verify getChargingStationId()', t => { + expect(getChargingStationId(1, chargingStationTemplate)).toBe(`${baseName}-00001`) + }) + + await it('Verify getHashId()', t => { + expect(getHashId(1, chargingStationTemplate)).toBe( + 'b4b1e8ec4fca79091d99ea9a7ea5901548010e6c0e98be9296f604b9d68734444dfdae73d7d406b6124b42815214d088' + ) + }) + + await it('Verify validateStationInfo()', t => { + expect(() => { + validateStationInfo(chargingStation) + }).toThrow(new BaseError('Missing charging station information')) + chargingStation.stationInfo = {} as ChargingStationInfo + expect(() => { + validateStationInfo(chargingStation) + }).toThrow(new BaseError('Missing charging station information')) + chargingStation.stationInfo.baseName = baseName + expect(() => { + validateStationInfo(chargingStation) + }).toThrow(new BaseError('Missing chargingStationId in stationInfo properties')) + chargingStation.stationInfo.chargingStationId = '' + expect(() => { + validateStationInfo(chargingStation) + }).toThrow(new BaseError('Missing chargingStationId in stationInfo properties')) + chargingStation.stationInfo.chargingStationId = getChargingStationId(1, chargingStationTemplate) + expect(() => { + validateStationInfo(chargingStation) + }).toThrow(new BaseError(`${baseName}-00001: Missing hashId in stationInfo properties`)) + chargingStation.stationInfo.hashId = '' + expect(() => { + validateStationInfo(chargingStation) + }).toThrow(new BaseError(`${baseName}-00001: Missing hashId in stationInfo properties`)) + chargingStation.stationInfo.hashId = getHashId(1, chargingStationTemplate) + expect(() => { + validateStationInfo(chargingStation) + }).toThrow(new BaseError(`${baseName}-00001: Missing templateIndex in stationInfo properties`)) + chargingStation.stationInfo.templateIndex = 0 + expect(() => { + validateStationInfo(chargingStation) + }).toThrow( + new BaseError(`${baseName}-00001: Invalid templateIndex value in stationInfo properties`) + ) + chargingStation.stationInfo.templateIndex = 1 + expect(() => { + validateStationInfo(chargingStation) + }).toThrow(new BaseError(`${baseName}-00001: Missing templateName in stationInfo properties`)) + chargingStation.stationInfo.templateName = '' + expect(() => { + validateStationInfo(chargingStation) + }).toThrow(new BaseError(`${baseName}-00001: Missing templateName in stationInfo properties`)) + chargingStation.stationInfo.templateName = 'test-template.json' + expect(() => { + validateStationInfo(chargingStation) + }).toThrow(new BaseError(`${baseName}-00001: Missing maximumPower in stationInfo properties`)) + chargingStation.stationInfo.maximumPower = 0 + expect(() => { + validateStationInfo(chargingStation) + }).toThrow( + new BaseError(`${baseName}-00001: Invalid maximumPower value in stationInfo properties`) + ) + chargingStation.stationInfo.maximumPower = 12000 + expect(() => { + validateStationInfo(chargingStation) + }).toThrow( + new BaseError(`${baseName}-00001: Missing maximumAmperage in stationInfo properties`) + ) + chargingStation.stationInfo.maximumAmperage = 0 + expect(() => { + validateStationInfo(chargingStation) + }).toThrow( + new BaseError(`${baseName}-00001: Invalid maximumAmperage value in stationInfo properties`) + ) + chargingStation.stationInfo.maximumAmperage = 16 + expect(() => { + validateStationInfo(chargingStation) + }).not.toThrow() + chargingStation.evses = new Map() + chargingStation.stationInfo.ocppVersion = OCPPVersion.VERSION_201 + expect(() => { + validateStationInfo(chargingStation) + }).toThrow( + new BaseError( + `${baseName}-00001: OCPP 2.0 or superior requires at least one EVSE defined in the charging station template/configuration` + ) + ) + }) +})