Skip to content

Commit

Permalink
test: add some tests for charging station helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit committed Jul 26, 2024
1 parent a95db78 commit df59920
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 24 deletions.
16 changes: 10 additions & 6 deletions src/charging-station/AutomaticTransactionGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`)
Expand All @@ -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(
Expand Down
46 changes: 28 additions & 18 deletions src/charging-station/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
114 changes: 114 additions & 0 deletions tests/charging-station/Helpers.test.ts
Original file line number Diff line number Diff line change
@@ -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<number, EvseStatus>()
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`
)
)
})
})

0 comments on commit df59920

Please sign in to comment.