Skip to content

Commit

Permalink
SMHE-2164: Add unit test for SetSpecificAttributeValueCommandExecutor
Browse files Browse the repository at this point in the history
Signed-off-by: stefanermens <[email protected]>
  • Loading branch information
stefanermens committed Feb 17, 2025
1 parent 1b123ec commit c34dd78
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

package org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.misc;

import java.util.List;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.openmuc.jdlms.AccessResultCode;
import org.openmuc.jdlms.AttributeAddress;
import org.openmuc.jdlms.ObisCode;
import org.openmuc.jdlms.SetParameter;
import org.openmuc.jdlms.datatypes.DataObject;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.AbstractCommandExecutor;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.DlmsHelper;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.JdlmsObjectToStringUtil;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager;
import org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException;
import org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException;
import org.opensmartgridplatform.dlms.exceptions.ObjectConfigException;
import org.opensmartgridplatform.dlms.objectconfig.Attribute;
Expand All @@ -33,14 +33,11 @@
public class SetSpecificAttributeValueCommandExecutor
extends AbstractCommandExecutor<SetSpecificAttributeValueRequestDto, Void> {

private final DlmsHelper dlmsHelper;
private final ObjectConfigService objectConfigService;

public SetSpecificAttributeValueCommandExecutor(
final DlmsHelper dlmsHelper, final ObjectConfigService objectConfigService) {
public SetSpecificAttributeValueCommandExecutor(final ObjectConfigService objectConfigService) {
super(SetSpecificAttributeValueRequestDto.class);
this.objectConfigService = objectConfigService;
this.dlmsHelper = dlmsHelper;
}

@Override
Expand Down Expand Up @@ -93,19 +90,24 @@ public Void execute(
requestData.getAttribute());
final SetParameter setParameter = new SetParameter(attributeAddress, data);

conn.getDlmsMessageListener()
.setDescription(
"Setting value in "
+ requestData.getObjectType()
+ ", set attribute: "
+ JdlmsObjectToStringUtil.describeAttributes(attributeAddress));

log.debug(
"Set specific attribute value, class id: {}, obis code: {}, attribute id: {}, value: {}",
cosemObject.getClassId(),
cosemObject.getObis(),
requestData.getAttribute(),
requestData.getIntValue());

final List<AccessResultCode> resultCodes =
this.dlmsHelper.setWithList(conn, device, List.of(setParameter));

if (!resultCodes.stream().allMatch(code -> code.equals(AccessResultCode.SUCCESS))) {
log.debug("Result of Set specific value is {}", resultCodes);
throw new ProtocolAdapterException("Set specific value resulted in: " + resultCodes);
try {
conn.getConnection().set(setParameter);
} catch (final IOException e) {
throw new ConnectionException(e);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.misc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openmuc.jdlms.AccessResultCode;
import org.openmuc.jdlms.AttributeAddress;
import org.openmuc.jdlms.DlmsConnection;
import org.openmuc.jdlms.SetParameter;
import org.openmuc.jdlms.datatypes.DataObject;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.Protocol;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager;
import org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsMessageListener;
import org.opensmartgridplatform.dlms.exceptions.ObjectConfigException;
import org.opensmartgridplatform.dlms.services.ObjectConfigService;
import org.opensmartgridplatform.dto.valueobjects.smartmetering.SetSpecificAttributeValueRequestDto;
import org.opensmartgridplatform.shared.infra.jms.MessageMetadata;

@ExtendWith(MockitoExtension.class)
class SetSpecificAttributeValueCommandExecutorTest {

private SetSpecificAttributeValueCommandExecutor executor;

@Mock private DlmsMessageListener dlmsMessageListener;

@Mock private DlmsConnectionManager connectionManager;

@Mock private DlmsConnection dlmsConnection;

@Captor private ArgumentCaptor<SetParameter> setParameterArgumentCaptor;

private static final String WATCHDOG_OBJECT_TYPE = "WATCHDOG_TIMER";
private static final String WATCHDOG_TIMER_OBIS = "0.1.94.31.2.255";
private static final int CLASS_ID = 1;
private static final int ATTRIBUTE_ID = 2;
private static final int VALUE = 25;

@BeforeEach
public void setUp() throws IOException, ObjectConfigException {
final ObjectConfigService objectConfigService = new ObjectConfigService();
this.executor = new SetSpecificAttributeValueCommandExecutor(objectConfigService);
}

@ParameterizedTest
@EnumSource(
value = Protocol.class,
names = {"DSMR_2_2", "OTHER_PROTOCOL"},
mode = EnumSource.Mode.EXCLUDE)
void testExecute(final Protocol protocol) throws Exception {

final DlmsDevice testDevice = new DlmsDevice();
testDevice.setProtocol(protocol);
testDevice.setWithListMax(1);

final SetSpecificAttributeValueRequestDto requestData =
new SetSpecificAttributeValueRequestDto(WATCHDOG_OBJECT_TYPE, ATTRIBUTE_ID, VALUE);

final AttributeAddress attributeAddress =
new AttributeAddress(CLASS_ID, this.WATCHDOG_TIMER_OBIS, ATTRIBUTE_ID, null);
final SetParameter expectedSetParameter =
new SetParameter(attributeAddress, DataObject.newUInteger16Data(VALUE));

when(this.connectionManager.getDlmsMessageListener()).thenReturn(this.dlmsMessageListener);
when(this.connectionManager.getConnection()).thenReturn(this.dlmsConnection);
when(this.dlmsConnection.set(this.setParameterArgumentCaptor.capture()))
.thenReturn(AccessResultCode.SUCCESS);

// CALL
this.executor.execute(
this.connectionManager, testDevice, requestData, mock(MessageMetadata.class));

// VERIFY
assertThat(this.setParameterArgumentCaptor.getValue())
.usingRecursiveComparison()
.isEqualTo(expectedSetParameter);
}

@Test
void testExecuteNoObject() {

final DlmsDevice testDevice = new DlmsDevice();
testDevice.setProtocol(Protocol.DSMR_2_2);

final SetSpecificAttributeValueRequestDto requestData =
new SetSpecificAttributeValueRequestDto(WATCHDOG_OBJECT_TYPE, ATTRIBUTE_ID, VALUE);

// CALL
assertThrows(
IllegalArgumentException.class,
() -> {
this.executor.execute(
this.connectionManager, testDevice, requestData, mock(MessageMetadata.class));
});
}
}

0 comments on commit c34dd78

Please sign in to comment.