Skip to content

Commit

Permalink
SMHE-909: Solve Sonar warnings
Browse files Browse the repository at this point in the history
Signed-off-by: stefanermens <[email protected]>
  • Loading branch information
stefanermens committed May 24, 2024
1 parent 14d5d30 commit 1c9d731
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,15 @@ public int version() {
return this.version;
}

public Enum<?>[] getAttributeEnumValues() {
return this.attributeClass.getEnumConstants();
public AttributeClass[] getAttributeEnumValues() {
return (AttributeClass[]) this.attributeClass.getEnumConstants();
}

public AttributeType getAttributeType(final int attributeId) {
final Enum<?>[] attributesForClass = this.getAttributeEnumValues();
final AttributeClass[] attributesForClass = this.getAttributeEnumValues();

final Optional<AttributeClass> optionalAttributeClass =
Arrays.stream(attributesForClass)
.map(AttributeClass.class::cast)
.filter(attribute -> attribute.attributeId() == attributeId)
.findFirst();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,11 @@ private String decodeAttributeValue(
}

private String getAttributeDescription(final InterfaceClass interfaceClass, final int attrId) {
for (final Enum<?> attributeClass : interfaceClass.getAttributeEnumValues()) {
if (((AttributeClass) attributeClass).attributeId() == attrId) {
return ((AttributeClass) attributeClass).attributeName().toLowerCase();
final AttributeClass[] attributesForClasses = interfaceClass.getAttributeEnumValues();

for (final AttributeClass attributeClass : attributesForClasses) {
if (attributeClass.attributeId() == attrId) {
return attributeClass.attributeName().toLowerCase();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;
import org.openmuc.jdlms.datatypes.DataObject;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.DlmsHelper;
import org.opensmartgridplatform.dlms.interfaceclass.attribute.AttributeType;
import org.opensmartgridplatform.dlms.objectconfig.Attribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
Expand All @@ -26,15 +24,10 @@ public class ProfileDataDecoder {
// This service decodes attributes that have a special type in some DLMS classes as defined in
// an additional specification, e.g. (D)SMR.

private final DlmsHelper dlmsHelper;

private final Map<AttributeType, Function<DataObject, String>> decoderMap =
new EnumMap<>(AttributeType.class);

@Autowired
public ProfileDataDecoder(final DlmsHelper dlmsHelper) {
this.dlmsHelper = dlmsHelper;

public ProfileDataDecoder() {
this.decoderMap.put(SIGNATURE, this::decodeSignature);
this.decoderMap.put(DSMR_EQUIPMENT_IDENTIFIER, this::decodeDsmrEquipmentIdentifier);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import lombok.extern.slf4j.Slf4j;
import org.openmuc.jdlms.datatypes.DataObject;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.DlmsHelper;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.datadecoder.BasicDlmsDataDecoder;
import org.opensmartgridplatform.dto.valueobjects.smartmetering.CircuitSwitchedStatusDto;
import org.opensmartgridplatform.dto.valueobjects.smartmetering.ModemRegistrationStatusDto;
import org.opensmartgridplatform.dto.valueobjects.smartmetering.PacketSwitchedStatusDto;
Expand All @@ -24,13 +23,10 @@ public class DataExchangeClassesDecoder {
// book chapter "Interface classes for setting up data exchange via local ports and modems".

private final DlmsHelper dlmsHelper;
private final BasicDlmsDataDecoder basicDlmsDataDecoder;

@Autowired
public DataExchangeClassesDecoder(
final DlmsHelper dlmsHelper, final BasicDlmsDataDecoder basicDlmsDataDecoder) {
public DataExchangeClassesDecoder(final DlmsHelper dlmsHelper) {
this.dlmsHelper = dlmsHelper;
this.basicDlmsDataDecoder = basicDlmsDataDecoder;
}

// Class-id 47: GSM diagnostic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
import org.openmuc.jdlms.datatypes.DataObject;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.DlmsHelper;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.datadecoder.BasicDlmsDataDecoder;
import org.opensmartgridplatform.dlms.interfaceclass.InterfaceClass;
import org.opensmartgridplatform.dlms.interfaceclass.attribute.AttributeType;
Expand All @@ -39,20 +38,17 @@ public class DlmsClassDataDecoder {
// This service decodes attributes that have a special type in some DLMS classes, as described
// in the DLMS Blue book.

private final DlmsHelper dlmsHelper;
private final BasicDlmsDataDecoder basicDlmsDataDecoder;

private final Map<AttributeType, Function<DataObject, String>> map =
new EnumMap<>(AttributeType.class);

@Autowired
public DlmsClassDataDecoder(
final DlmsHelper dlmsHelper,
final BasicDlmsDataDecoder basicDlmsDataDecoder,
final DataExchangeClassesDecoder dataExchangeDecoder,
final MeasurementDataClassesDecoder measurementDataDecoder,
final TimeAndEventsClassesDecoder timeAndEventsClassesDecoder) {
this.dlmsHelper = dlmsHelper;
this.basicDlmsDataDecoder = basicDlmsDataDecoder;

this.map.put(ADJACENT_CELLS, dataExchangeDecoder::decodeAdjacentCells);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.openmuc.jdlms.datatypes.DataObject;
import org.openmuc.jdlms.datatypes.DataObject.Type;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.DlmsHelper;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.datadecoder.BasicDlmsDataDecoder;
import org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemObjectDefinitionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -22,13 +21,10 @@ public class MeasurementDataClassesDecoder {
// book chapter "Interface classes for parameters and measurement data".

private final DlmsHelper dlmsHelper;
private final BasicDlmsDataDecoder basicDlmsDataDecoder;

@Autowired
public MeasurementDataClassesDecoder(
final DlmsHelper dlmsHelper, final BasicDlmsDataDecoder basicDlmsDataDecoder) {
public MeasurementDataClassesDecoder(final DlmsHelper dlmsHelper) {
this.dlmsHelper = dlmsHelper;
this.basicDlmsDataDecoder = basicDlmsDataDecoder;
}

// Class-id 3, 4, 5: (Extended / Demand) Register
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class TimeAndEventsClassesDecoder {
// This class contains decoding functions for the attributes of the DLMS classes in DLMS Blue
// book chapter "Interface classes for time- and event bound control".

private static final String ARRAY_EXPECTED_BUT_FOUND = "ARRAY expected, but found ";
private static final String STRUCTURE_EXPECTED_BUT_FOUND = "STRUCTURE expected, but found ";

private final DlmsHelper dlmsHelper;
private final BasicDlmsDataDecoder basicDlmsDataDecoder;

Expand All @@ -43,7 +46,7 @@ public String decodeSpecialDays(final DataObject attributeData) {
}
return String.join("\n", specialDaysDecoded);
} else {
return "ARRAY expected, but found " + attributeData.getType().name();
return ARRAY_EXPECTED_BUT_FOUND + attributeData.getType().name();
}
} catch (final Exception e) {
return "decoding special days failed: " + e.getMessage();
Expand All @@ -60,7 +63,7 @@ private String decodeSpecialDay(final DataObject specialDay) {

return index + ". (id " + day_id + "): " + date;
} else {
return "STRUCTURE expected, but found " + specialDay.getType().name();
return STRUCTURE_EXPECTED_BUT_FOUND + specialDay.getType().name();
}
} catch (final Exception e) {
return "decoding special day failed: " + e.getMessage();
Expand All @@ -79,7 +82,7 @@ public String decodeDayProfiles(final DataObject attributeData) {
}
return String.join("\n", dayProfilesDecoded);
} else {
return "ARRAY expected, but found " + attributeData.getType().name();
return ARRAY_EXPECTED_BUT_FOUND + attributeData.getType().name();
}
} catch (final Exception e) {
return "decoding day profiles failed: " + e.getMessage();
Expand All @@ -94,7 +97,7 @@ private String decodeDayProfile(final DataObject dayProfile) {
final String schedule = this.decodeDaySchedule(fields.get(1));
return id + ": " + schedule;
} else {
return "STRUCTURE expected, but found " + dayProfile.getType().name();
return STRUCTURE_EXPECTED_BUT_FOUND + dayProfile.getType().name();
}
} catch (final Exception e) {
return "decoding day profile failed: " + e.getMessage();
Expand All @@ -112,7 +115,7 @@ private String decodeDaySchedule(final DataObject daySchedule) {
}
return String.join(", ", dayDayProfileActionsDecoded);
} else {
return "ARRAY expected, but found " + daySchedule.getType().name();
return ARRAY_EXPECTED_BUT_FOUND + daySchedule.getType().name();
}
} catch (final Exception e) {
return "decoding day profile actions failed: " + e.getMessage();
Expand All @@ -129,7 +132,7 @@ private String decodeDayProfileAction(final DataObject dayProfileAction) {
final int selector = this.dlmsHelper.readInteger(fields.get(2), "read selector");
return startTime + " " + logicalName + " selector " + selector;
} else {
return "STRUCTURE expected, but found " + dayProfileAction.getType().name();
return STRUCTURE_EXPECTED_BUT_FOUND + dayProfileAction.getType().name();
}
} catch (final Exception e) {
return "decoding day profile action failed: " + e.getMessage();
Expand All @@ -147,7 +150,7 @@ public String decodeWeekProfiles(final DataObject attributeData) {
}
return String.join("\n", weekProfilesDecoded);
} else {
return "ARRAY expected, but found " + attributeData.getType().name();
return ARRAY_EXPECTED_BUT_FOUND + attributeData.getType().name();
}
} catch (final Exception e) {
return "decoding week profiles failed: " + e.getMessage();
Expand Down Expand Up @@ -182,7 +185,7 @@ private String decodeWeekProfile(final DataObject weekProfile) {
+ ", Sunday "
+ sunday;
} else {
return "STRUCTURE expected, but found " + weekProfile.getType().name();
return STRUCTURE_EXPECTED_BUT_FOUND + weekProfile.getType().name();
}
} catch (final Exception e) {
return "decoding week profile failed: " + e.getMessage();
Expand All @@ -200,7 +203,7 @@ public String decodeSeasonProfiles(final DataObject attributeData) {
}
return String.join("\n", seasonProfilesDecoded);
} else {
return "ARRAY expected, but found " + attributeData.getType().name();
return ARRAY_EXPECTED_BUT_FOUND + attributeData.getType().name();
}
} catch (final Exception e) {
return "decoding season profiles failed: " + e.getMessage();
Expand All @@ -221,7 +224,7 @@ private String decodeSeasonProfile(final DataObject seasonProfile) {

return name + ": " + start + ", weekName: " + weekName;
} else {
return "STRUCTURE expected, but found " + seasonProfile.getType().name();
return STRUCTURE_EXPECTED_BUT_FOUND + seasonProfile.getType().name();
}
} catch (final Exception e) {
return "decoding season profile failed: " + e.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,18 @@ public void setUp() throws IOException, ObjectConfigException {
new ObjectConfigServiceHelper(objectConfigService);
final BasicDlmsDataDecoder basicDlmsDataDecoder = new BasicDlmsDataDecoder(dlmsHelper);
final DataExchangeClassesDecoder dataExchangeClassesDecoder =
new DataExchangeClassesDecoder(dlmsHelper, basicDlmsDataDecoder);
new DataExchangeClassesDecoder(dlmsHelper);
final MeasurementDataClassesDecoder measurementDataClassesDecoder =
new MeasurementDataClassesDecoder(dlmsHelper, basicDlmsDataDecoder);
new MeasurementDataClassesDecoder(dlmsHelper);
final TimeAndEventsClassesDecoder timeAndEventsClassesDecoder =
new TimeAndEventsClassesDecoder(dlmsHelper, basicDlmsDataDecoder);
final DlmsClassDataDecoder dlmsClassDataDecoder =
new DlmsClassDataDecoder(
dlmsHelper,
basicDlmsDataDecoder,
dataExchangeClassesDecoder,
measurementDataClassesDecoder,
timeAndEventsClassesDecoder);
final ProfileDataDecoder profileDataDecoder = new ProfileDataDecoder(dlmsHelper);
final ProfileDataDecoder profileDataDecoder = new ProfileDataDecoder();
final DataDecoder dataDecoder =
new DataDecoder(dlmsHelper, basicDlmsDataDecoder, dlmsClassDataDecoder, profileDataDecoder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,18 @@ class DataDecoderTest {
private final BasicDlmsDataDecoder basicDlmsDataDecoder =
new BasicDlmsDataDecoder(this.dlmsHelper);
private final DataExchangeClassesDecoder dataExchangeClassesDecoder =
new DataExchangeClassesDecoder(this.dlmsHelper, this.basicDlmsDataDecoder);
new DataExchangeClassesDecoder(this.dlmsHelper);
private final MeasurementDataClassesDecoder measurementDataClassesDecoder =
new MeasurementDataClassesDecoder(this.dlmsHelper, this.basicDlmsDataDecoder);
new MeasurementDataClassesDecoder(this.dlmsHelper);
private final TimeAndEventsClassesDecoder timeAndEventsClassesDecoder =
new TimeAndEventsClassesDecoder(this.dlmsHelper, this.basicDlmsDataDecoder);
private final DlmsClassDataDecoder dlmsClassDataDecoder =
new DlmsClassDataDecoder(
this.dlmsHelper,
this.basicDlmsDataDecoder,
this.dataExchangeClassesDecoder,
this.measurementDataClassesDecoder,
this.timeAndEventsClassesDecoder);
private final ProfileDataDecoder profileDataDecoder = new ProfileDataDecoder(this.dlmsHelper);
private final ProfileDataDecoder profileDataDecoder = new ProfileDataDecoder();
private final DataDecoder decoder =
new DataDecoder(
this.dlmsHelper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class ProfileDataDecoderTest {
private final DlmsHelper dlmsHelper = new DlmsHelper();
private final ProfileDataDecoder decoder = new ProfileDataDecoder(this.dlmsHelper);
private final ProfileDataDecoder decoder = new ProfileDataDecoder();

@Test
void testDecodeSignature() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DataExchangeClassesDecoderTest {
private final DlmsHelper dlmsHelper = new DlmsHelper();
private final BasicDlmsDataDecoder dlmsDataDecoder = new BasicDlmsDataDecoder(this.dlmsHelper);
private final DataExchangeClassesDecoder decoder =
new DataExchangeClassesDecoder(this.dlmsHelper, this.dlmsDataDecoder);
new DataExchangeClassesDecoder(this.dlmsHelper);

@Test
void testDecodeModemRegistrationStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ class DlmsClassDataDecoderTest {
private final DlmsHelper dlmsHelper = new DlmsHelper();
private final BasicDlmsDataDecoder dlmsDataDecoder = new BasicDlmsDataDecoder(this.dlmsHelper);
private final DataExchangeClassesDecoder dataExchangeClassesDecoder =
new DataExchangeClassesDecoder(this.dlmsHelper, this.dlmsDataDecoder);
new DataExchangeClassesDecoder(this.dlmsHelper);
private final MeasurementDataClassesDecoder measurementDataClassesDecoder =
new MeasurementDataClassesDecoder(this.dlmsHelper, this.dlmsDataDecoder);
new MeasurementDataClassesDecoder(this.dlmsHelper);
private final TimeAndEventsClassesDecoder timeAndEventsClassesDecoder =
new TimeAndEventsClassesDecoder(this.dlmsHelper, this.dlmsDataDecoder);
private final DlmsClassDataDecoder decoder =
new DlmsClassDataDecoder(
this.dlmsHelper,
this.dlmsDataDecoder,
this.dataExchangeClassesDecoder,
this.measurementDataClassesDecoder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MeasurementDataClassesDecoderTest {
private final DlmsHelper dlmsHelper = new DlmsHelper();
private final BasicDlmsDataDecoder dlmsDataDecoder = new BasicDlmsDataDecoder(this.dlmsHelper);
private final MeasurementDataClassesDecoder decoder =
new MeasurementDataClassesDecoder(this.dlmsHelper, this.dlmsDataDecoder);
new MeasurementDataClassesDecoder(this.dlmsHelper);

@Test
void testDecodeScalerUnit() {
Expand Down

0 comments on commit 1c9d731

Please sign in to comment.