fix(simulator): fix empty array detection helper semantic
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 10 Feb 2023 17:19:06 +0000 (18:19 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 10 Feb 2023 17:19:06 +0000 (18:19 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationUtils.ts
src/charging-station/SharedLRUCache.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts
src/charging-station/ocpp/OCPPServiceUtils.ts
src/charging-station/ui-server/ui-services/AbstractUIService.ts
src/utils/Utils.ts
test/utils/UtilsTest.ts

index afa56c37ceca8a799df90e34bf0a5cb8f306e3fc..62f04891150a4e4a9da67cbf0f92e083d10787ce 100644 (file)
@@ -241,7 +241,7 @@ export class Bootstrap {
       this.numberOfChargingStationTemplates = 0;
       this.numberOfChargingStations = 0;
       const stationTemplateUrls = Configuration.getStationTemplateUrls();
-      if (!Utils.isEmptyArray(stationTemplateUrls)) {
+      if (Utils.isNotEmptyArray(stationTemplateUrls)) {
         this.numberOfChargingStationTemplates = stationTemplateUrls.length;
         stationTemplateUrls.forEach((stationTemplateUrl) => {
           this.numberOfChargingStations += stationTemplateUrl.numberOfStations ?? 0;
index 65ea08736287d65bb7137bfa236402b8a1c801a7..a425d865651ead0c168a714b163705255f5b3434 100644 (file)
@@ -170,7 +170,7 @@ export default class ChargingStation {
   };
 
   public hasAuthorizedTags(): boolean {
-    return !Utils.isEmptyArray(
+    return Utils.isNotEmptyArray(
       this.authorizedTagsCache.getAuthorizedTags(
         ChargingStationUtils.getAuthorizationFile(this.stationInfo)
       )
@@ -713,7 +713,7 @@ export default class ChargingStation {
         this.getAutomaticTransactionGeneratorConfigurationFromTemplate(),
       this
     );
-    if (!Utils.isEmptyArray(connectorIds)) {
+    if (Utils.isNotEmptyArray(connectorIds)) {
       for (const connectorId of connectorIds) {
         this.automaticTransactionGenerator?.startConnector(connectorId);
       }
@@ -724,7 +724,7 @@ export default class ChargingStation {
   }
 
   public stopAutomaticTransactionGenerator(connectorIds?: number[]): void {
-    if (!Utils.isEmptyArray(connectorIds)) {
+    if (Utils.isNotEmptyArray(connectorIds)) {
       for (const connectorId of connectorIds) {
         this.automaticTransactionGenerator?.stopConnector(connectorId);
       }
@@ -865,7 +865,7 @@ export default class ChargingStation {
     );
     stationInfo.ocppVersion = stationTemplate?.ocppVersion ?? OCPPVersion.VERSION_16;
     ChargingStationUtils.createSerialNumber(stationTemplate, stationInfo);
-    if (!Utils.isEmptyArray(stationTemplate?.power)) {
+    if (Utils.isNotEmptyArray(stationTemplate?.power)) {
       stationTemplate.power = stationTemplate.power as number[];
       const powerArrayRandomIndex = Math.floor(Utils.secureRandom() * stationTemplate.power.length);
       stationInfo.maximumPower =
@@ -1928,7 +1928,7 @@ export default class ChargingStation {
 
   private getConfiguredSupervisionUrl(): URL {
     const supervisionUrls = this.stationInfo?.supervisionUrls ?? Configuration.getSupervisionUrls();
-    if (!Utils.isEmptyArray(supervisionUrls)) {
+    if (Utils.isNotEmptyArray(supervisionUrls)) {
       switch (Configuration.getSupervisionUrlDistribution()) {
         case SupervisionUrlDistribution.ROUND_ROBIN:
           // FIXME
index 2d1adb5d9bebdb274607e7351dd29967352d6db6..54e4df72d8f0105e050f005c5b062dc7ef31a817 100644 (file)
@@ -112,7 +112,7 @@ export class ChargingStationUtils {
 
   public static getConfiguredNumberOfConnectors(stationTemplate: ChargingStationTemplate): number {
     let configuredMaxConnectors: number;
-    if (Utils.isEmptyArray(stationTemplate.numberOfConnectors) === false) {
+    if (Utils.isNotEmptyArray(stationTemplate.numberOfConnectors) === true) {
       const numberOfConnectors = stationTemplate.numberOfConnectors as number[];
       configuredMaxConnectors =
         numberOfConnectors[Math.floor(Utils.secureRandom() * numberOfConnectors.length)];
@@ -334,7 +334,7 @@ export class ChargingStationUtils {
           .chargingProfiles.sort((a, b) => b.stackLevel - a.stackLevel)
       );
     }
-    if (!Utils.isEmptyArray(chargingProfiles)) {
+    if (Utils.isNotEmptyArray(chargingProfiles)) {
       const result = ChargingStationUtils.getLimitFromChargingProfiles(
         chargingProfiles,
         chargingStation.logPrefix()
index 97ed4cd1806e9ac45d5a1fd28b52cd1acdc63494..3e2e0de0f32b5319e43e18b9f95b788fc7c6a7bf 100644 (file)
@@ -113,7 +113,7 @@ export default class SharedLRUCache {
       Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) === false &&
       Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) === false &&
       Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) === false &&
-      Utils.isEmptyArray(chargingStationConfiguration?.configurationKey) === false &&
+      Utils.isNotEmptyArray(chargingStationConfiguration?.configurationKey) === true &&
       Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) === false &&
       Utils.isNotEmptyString(chargingStationConfiguration?.configurationHash) === true
     );
index 039c77641c243b36ded0abbfc5e65e57c79ed09e..7fcb12fb0b79353d7b61ae02b6e6f21dcb867b60 100644 (file)
@@ -602,7 +602,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     const connectorStatus = chargingStation.getConnectorStatus(commandPayload.connectorId);
     if (
       !Utils.isNullOrUndefined(commandPayload.connectorId) &&
-      !Utils.isEmptyArray(connectorStatus?.chargingProfiles)
+      Utils.isNotEmptyArray(connectorStatus?.chargingProfiles)
     ) {
       connectorStatus.chargingProfiles = [];
       logger.debug(
@@ -616,7 +616,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       let clearedCP = false;
       for (const connectorId of chargingStation.connectors.keys()) {
         if (
-          !Utils.isEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)
+          Utils.isNotEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)
         ) {
           chargingStation
             .getConnectorStatus(connectorId)
index d0d46c25d50df40ae9fb3f1f4bed5d9fbe196d5d..eceb21488f6e4e320a19d9d1df52bd6c43b98c31 100644 (file)
@@ -769,7 +769,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
       chargingStation.getConnectorStatus(connectorId).chargingProfiles = [];
     }
     let cpReplaced = false;
-    if (!Utils.isEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)) {
+    if (Utils.isNotEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)) {
       chargingStation
         .getConnectorStatus(connectorId)
         ?.chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => {
index 178e4be7fd8057de90284c8362bea157d3c4c562..3fced606ba29e6b003aa1b16c0bafdcb4a3f533e 100644 (file)
@@ -218,7 +218,7 @@ export class OCPPServiceUtils {
       chargingStation.getConnectorStatus(connectorId)?.MeterValues;
     for (
       let index = 0;
-      Utils.isEmptyArray(sampledValueTemplates) === false && index < sampledValueTemplates.length;
+      Utils.isNotEmptyArray(sampledValueTemplates) === true && index < sampledValueTemplates.length;
       index++
     ) {
       if (
index f8cebbf03f134645dfc040207c7c9ec97c1af81b..62249549c268b11b383dd151f91d23429a70fb4b 100644 (file)
@@ -154,7 +154,7 @@ export default abstract class AbstractUIService {
     procedureName: BroadcastChannelProcedureName,
     payload: BroadcastChannelRequestPayload
   ): void {
-    if (!Utils.isEmptyArray(payload.hashIds)) {
+    if (Utils.isNotEmptyArray(payload.hashIds)) {
       payload.hashIds = payload.hashIds
         .map((hashId) => {
           if (this.uiServer.chargingStations.has(hashId) === true) {
@@ -169,7 +169,7 @@ export default abstract class AbstractUIService {
         })
         .filter((hashId) => hashId !== undefined);
     }
-    const expectedNumberOfResponses = !Utils.isEmptyArray(payload.hashIds)
+    const expectedNumberOfResponses = Utils.isNotEmptyArray(payload.hashIds)
       ? payload.hashIds.length
       : this.uiServer.chargingStations.size;
     this.uiServiceWorkerBroadcastChannel.sendRequest([uuid, procedureName, payload]);
index 1f17fe8735dce9bb1b794075aefd318da22e06a9..fa1c7b6b610fd5fa6a2043381cd44adecd88b7a6 100644 (file)
@@ -214,13 +214,17 @@ export default class Utils {
   }
 
   public static isEmptyArray(object: unknown | unknown[]): boolean {
-    if (!Array.isArray(object)) {
+    if (Array.isArray(object) && object.length === 0) {
       return true;
     }
-    if (object.length > 0) {
-      return false;
+    return false;
+  }
+
+  public static isNotEmptyArray(object: unknown | unknown[]): boolean {
+    if (Array.isArray(object) && object.length > 0) {
+      return true;
     }
-    return true;
+    return false;
   }
 
   public static isEmptyObject(obj: object): boolean {
index 423d61f335dda19aebf894978eb047ea2697c9cd..836e41b93bea826baaa3a67ddd7c868aaf074bcd 100644 (file)
@@ -229,16 +229,33 @@ describe('Utils test suite', () => {
   it('Verify isEmptyArray()', () => {
     expect(Utils.isEmptyArray([])).toBe(true);
     expect(Utils.isEmptyArray([1, 2])).toBe(false);
-    expect(Utils.isEmptyArray(undefined)).toBe(true);
-    expect(Utils.isEmptyArray(null)).toBe(true);
-    expect(Utils.isEmptyArray('')).toBe(true);
-    expect(Utils.isEmptyArray('test')).toBe(true);
-    expect(Utils.isEmptyArray(0)).toBe(true);
-    expect(Utils.isEmptyArray({})).toBe(true);
-    expect(Utils.isEmptyArray(new Map())).toBe(true);
-    expect(Utils.isEmptyArray(new Set())).toBe(true);
-    expect(Utils.isEmptyArray(new WeakMap())).toBe(true);
-    expect(Utils.isEmptyArray(new WeakSet())).toBe(true);
+    expect(Utils.isEmptyArray(['1', '2'])).toBe(false);
+    expect(Utils.isEmptyArray(undefined)).toBe(false);
+    expect(Utils.isEmptyArray(null)).toBe(false);
+    expect(Utils.isEmptyArray('')).toBe(false);
+    expect(Utils.isEmptyArray('test')).toBe(false);
+    expect(Utils.isEmptyArray(0)).toBe(false);
+    expect(Utils.isEmptyArray({})).toBe(false);
+    expect(Utils.isEmptyArray(new Map())).toBe(false);
+    expect(Utils.isEmptyArray(new Set())).toBe(false);
+    expect(Utils.isEmptyArray(new WeakMap())).toBe(false);
+    expect(Utils.isEmptyArray(new WeakSet())).toBe(false);
+  });
+
+  it('Verify isNotEmptyArray()', () => {
+    expect(Utils.isNotEmptyArray([])).toBe(false);
+    expect(Utils.isNotEmptyArray([1, 2])).toBe(true);
+    expect(Utils.isNotEmptyArray(['1', '2'])).toBe(true);
+    expect(Utils.isNotEmptyArray(undefined)).toBe(false);
+    expect(Utils.isNotEmptyArray(null)).toBe(false);
+    expect(Utils.isNotEmptyArray('')).toBe(false);
+    expect(Utils.isNotEmptyArray('test')).toBe(false);
+    expect(Utils.isNotEmptyArray(0)).toBe(false);
+    expect(Utils.isNotEmptyArray({})).toBe(false);
+    expect(Utils.isNotEmptyArray(new Map())).toBe(false);
+    expect(Utils.isNotEmptyArray(new Set())).toBe(false);
+    expect(Utils.isNotEmptyArray(new WeakMap())).toBe(false);
+    expect(Utils.isNotEmptyArray(new WeakSet())).toBe(false);
   });
 
   it('Verify isEmptyObject()', () => {