|
| 1 | +/* |
| 2 | + * Copyright (C) 2022-2022 ConnectorIO Sp. z o.o. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | +package org.connectorio.addons.binding.ocpp.internal.server.adapter; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import eu.chargetime.ocpp.model.Confirmation; |
| 25 | +import eu.chargetime.ocpp.model.Request; |
| 26 | +import eu.chargetime.ocpp.model.core.BootNotificationRequest; |
| 27 | +import eu.chargetime.ocpp.model.core.ChangeConfigurationConfirmation; |
| 28 | +import eu.chargetime.ocpp.model.core.ChangeConfigurationRequest; |
| 29 | +import eu.chargetime.ocpp.model.core.ConfigurationStatus; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.UUID; |
| 33 | +import java.util.concurrent.CompletableFuture; |
| 34 | +import java.util.concurrent.CompletionStage; |
| 35 | +import org.connectorio.addons.binding.ocpp.internal.OcppSender; |
| 36 | +import org.connectorio.addons.binding.ocpp.internal.server.ChargerReference; |
| 37 | +import org.connectorio.addons.binding.ocpp.internal.server.OcppChargerSessionRegistry; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | + |
| 40 | +class MeterValuesConfigAdapterTest { |
| 41 | + |
| 42 | + @Test |
| 43 | + void stripsTheLastMeasurand() { |
| 44 | + assertThat(MeterValuesConfigAdapter.stripLast( |
| 45 | + "Energy.Active.Import.Register,Power.Active.Import,Current.Import,Voltage")) |
| 46 | + .isEqualTo("Energy.Active.Import.Register,Power.Active.Import,Current.Import"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void stripsDownToEmptyOnceOnlyOneMeasurandIsLeft() { |
| 51 | + assertThat(MeterValuesConfigAdapter.stripLast("Voltage")).isEmpty(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void handlesWhitespaceAroundCommas() { |
| 56 | + assertThat(MeterValuesConfigAdapter.stripLast("Voltage, Temperature, Current.Import")) |
| 57 | + .isEqualTo("Voltage,Temperature"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void handlesNullAndEmpty() { |
| 62 | + assertThat(MeterValuesConfigAdapter.stripLast(null)).isNull(); |
| 63 | + assertThat(MeterValuesConfigAdapter.stripLast("")).isEmpty(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Fake sender: Rejects a MeterValuesSampledData/AlignedData ChangeConfiguration while its value |
| 68 | + * still contains {@code rejectedMeasurand}, Accepts everything else. Stands in for a charger that |
| 69 | + * only supports a subset of the configured measurands, without hardcoding which subset. |
| 70 | + */ |
| 71 | + private static OcppSender rejectingSenderFor(String rejectedMeasurand, List<String> sentMeasurandValues) { |
| 72 | + return new OcppSender() { |
| 73 | + @Override |
| 74 | + @SuppressWarnings("unchecked") |
| 75 | + public <T extends Confirmation> CompletionStage<T> send(ChargerReference reference, Request request) { |
| 76 | + ChangeConfigurationRequest change = (ChangeConfigurationRequest) request; |
| 77 | + if ("MeterValuesSampledData".equals(change.getKey()) || "MeterValuesAlignedData".equals(change.getKey())) { |
| 78 | + sentMeasurandValues.add(change.getValue()); |
| 79 | + } |
| 80 | + ConfigurationStatus status = change.getValue() != null && change.getValue().contains(rejectedMeasurand) |
| 81 | + ? ConfigurationStatus.Rejected : ConfigurationStatus.Accepted; |
| 82 | + return (CompletionStage<T>) CompletableFuture.completedFuture(new ChangeConfigurationConfirmation(status)); |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void negotiatesDownOnRejectAndCachesTheAcceptedSetPerCharger() { |
| 89 | + ChargerReference charx = new ChargerReference("charx"); |
| 90 | + UUID session = UUID.randomUUID(); |
| 91 | + OcppChargerSessionRegistry registry = mock(OcppChargerSessionRegistry.class); |
| 92 | + when(registry.getCharger(session)).thenReturn(charx); |
| 93 | + |
| 94 | + List<String> sentMeasurandValues = new ArrayList<>(); |
| 95 | + MeterValuesConfigAdapter adapter = new MeterValuesConfigAdapter(registry, |
| 96 | + rejectingSenderFor("Temperature", sentMeasurandValues), 30, |
| 97 | + "Energy.Active.Import.Register,Temperature", 30); |
| 98 | + |
| 99 | + adapter.handleBootNotificationRequest(session, new BootNotificationRequest()); |
| 100 | + |
| 101 | + assertThat(adapter.acceptedMeasurands.get("charx")).isEqualTo("Energy.Active.Import.Register"); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void aReconnectStartsFromTheCachedAcceptedSetInsteadOfRenegotiating() { |
| 106 | + ChargerReference charx = new ChargerReference("charx"); |
| 107 | + UUID session = UUID.randomUUID(); |
| 108 | + OcppChargerSessionRegistry registry = mock(OcppChargerSessionRegistry.class); |
| 109 | + when(registry.getCharger(session)).thenReturn(charx); |
| 110 | + |
| 111 | + List<String> sentMeasurandValues = new ArrayList<>(); |
| 112 | + MeterValuesConfigAdapter adapter = new MeterValuesConfigAdapter(registry, |
| 113 | + rejectingSenderFor("Temperature", sentMeasurandValues), 30, |
| 114 | + "Energy.Active.Import.Register,Temperature", 30); |
| 115 | + |
| 116 | + adapter.handleBootNotificationRequest(session, new BootNotificationRequest()); // first boot: negotiates down |
| 117 | + adapter.handleBootNotificationRequest(session, new BootNotificationRequest()); // reconnect: reuses the cache |
| 118 | + |
| 119 | + // First boot tries the full configured list for both measurand keys, rejected both times, then |
| 120 | + // retries each down to the already-accepted set. The reconnect goes straight to that set for |
| 121 | + // both keys, first try, no further negotiation. |
| 122 | + assertThat(sentMeasurandValues).containsExactly( |
| 123 | + "Energy.Active.Import.Register,Temperature", "Energy.Active.Import.Register", |
| 124 | + "Energy.Active.Import.Register,Temperature", "Energy.Active.Import.Register", |
| 125 | + "Energy.Active.Import.Register", "Energy.Active.Import.Register"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void aChargerThatAcceptsTheFullListIsCachedTooSoLaterBootsSkipRenegotiation() { |
| 130 | + ChargerReference wallbox = new ChargerReference("wallbox"); |
| 131 | + UUID session = UUID.randomUUID(); |
| 132 | + OcppChargerSessionRegistry registry = mock(OcppChargerSessionRegistry.class); |
| 133 | + when(registry.getCharger(session)).thenReturn(wallbox); |
| 134 | + |
| 135 | + List<String> sentMeasurandValues = new ArrayList<>(); |
| 136 | + MeterValuesConfigAdapter adapter = new MeterValuesConfigAdapter(registry, |
| 137 | + rejectingSenderFor("Temperature", sentMeasurandValues), 30, |
| 138 | + "Energy.Active.Import.Register,Voltage", 30); |
| 139 | + |
| 140 | + adapter.handleBootNotificationRequest(session, new BootNotificationRequest()); |
| 141 | + |
| 142 | + assertThat(adapter.acceptedMeasurands.get("wallbox")).isEqualTo("Energy.Active.Import.Register,Voltage"); |
| 143 | + } |
| 144 | +} |
0 commit comments