Skip to content

Commit

Permalink
[WIP] Create interface for Services and replace static methods with i…
Browse files Browse the repository at this point in the history
…nstance methods closes #110

Signed-off-by: Samir Romdhani <[email protected]>
  • Loading branch information
samirromdhani committed Aug 22, 2023
1 parent 985b66e commit e38130a
Show file tree
Hide file tree
Showing 21 changed files with 1,309 additions and 1,121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.lfenergy.compas.sct.commons.dto.SubNetworkDTO;
import org.lfenergy.compas.sct.commons.dto.SubNetworkTypeDTO;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.scl.SclService;
import org.lfenergy.compas.sct.commons.scl.SubstationService;
import org.lfenergy.compas.sct.commons.service.impl.SclService;
import org.lfenergy.compas.sct.commons.service.impl.SubstationService;

import java.util.*;

Expand All @@ -26,6 +26,9 @@
*/
public class SclAutomationService {

SclService sclService = new SclService();
SubstationService substationService = new SubstationService();

/**
* Possible Subnetwork and ConnectedAP names which should be used in generated SCD in order a have global coherence
* Configuration based on used framework can be used to externalize this datas
Expand All @@ -34,10 +37,6 @@ public class SclAutomationService {
new SubNetworkTypeDTO("RSPACE_PROCESS_NETWORK", SubNetworkDTO.SubnetworkType.MMS.toString(), List.of("PROCESS_AP", "TOTO_AP_GE")),
new SubNetworkTypeDTO("RSPACE_ADMIN_NETWORK", SubNetworkDTO.SubnetworkType.IP.toString(), List.of("ADMIN_AP", "TATA_AP_EFFACEC")));

private SclAutomationService() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

/**
* Create an SCD file from specified parameters, it calls all functions defined in the process one by one, every step
* return an SCD file which will be used by the next step.
Expand All @@ -47,15 +46,15 @@ private SclAutomationService() {
* @return an SCD object
* @throws ScdException
*/
public static SCL createSCD(@NonNull SCL ssd, @NonNull HeaderDTO headerDTO, List<SCL> stds) throws ScdException {
SCL scd = SclService.initScl(headerDTO.getId(), headerDTO.getVersion(), headerDTO.getRevision());
public SCL createSCD(@NonNull SCL ssd, @NonNull HeaderDTO headerDTO, List<SCL> stds) throws ScdException {
SCL scd = sclService.initScl(headerDTO.getId(), headerDTO.getVersion(), headerDTO.getRevision());
if (!headerDTO.getHistoryItems().isEmpty()) {
HeaderDTO.HistoryItem hItem = headerDTO.getHistoryItems().get(0);
SclService.addHistoryItem(scd, hItem.getWho(), hItem.getWhat(), hItem.getWhy());
sclService.addHistoryItem(scd, hItem.getWho(), hItem.getWhat(), hItem.getWhy());
}
SubstationService.addSubstation(scd, ssd);
SclService.importSTDElementsInSCD(scd, stds, SUB_NETWORK_TYPES);
SclService.removeAllControlBlocksAndDatasetsAndExtRefSrcBindings(scd);
substationService.addSubstation(scd, ssd);
sclService.importSTDElementsInSCD(scd, stds, SUB_NETWORK_TYPES);
sclService.removeAllControlBlocksAndDatasetsAndExtRefSrcBindings(scd);
return scd;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
import org.lfenergy.compas.sct.commons.scl.ied.LDeviceAdapter;
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.*;
import static org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller.assertIsMarshallable;

class SclAutomationServiceTest {

SclAutomationService sclAutomationService = new SclAutomationService();

private HeaderDTO headerDTO;

@BeforeEach
Expand All @@ -44,7 +43,7 @@ void createSCD_should_return_generatedSCD() {
SCL ssd = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/scd.xml");
SCL std = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/std.xml");
// When
SCL scd = SclAutomationService.createSCD(ssd, headerDTO, List.of(std));
SCL scd = sclAutomationService.createSCD(ssd, headerDTO, List.of(std));
// Then
assertNotNull(scd.getHeader().getId());
assertNull(scd.getHeader().getHistory());
Expand All @@ -68,7 +67,7 @@ void createSCD_With_HItem() {
SCL std2 = SclTestMarshaller.getSCLFromFile("/std_2.xml");
SCL std3 = SclTestMarshaller.getSCLFromFile("/std_3.xml");
// When
SCL scd = SclAutomationService.createSCD(ssd, headerDTO, List.of(std1, std2, std3));
SCL scd = sclAutomationService.createSCD(ssd, headerDTO, List.of(std1, std2, std3));
// Then
assertNotNull(scd.getHeader().getId());
assertEquals(1, scd.getHeader().getHistory().getHitem().size());
Expand All @@ -93,7 +92,7 @@ void createSCD_With_HItems() {
SCL std2 = SclTestMarshaller.getSCLFromFile("/std_2.xml");
SCL std3 = SclTestMarshaller.getSCLFromFile("/std_3.xml");
// When
SCL scd = SclAutomationService.createSCD(ssd, headerDTO, List.of(std1, std2, std3));
SCL scd = sclAutomationService.createSCD(ssd, headerDTO, List.of(std1, std2, std3));
// Then
assertNotNull(scd.getHeader().getId());
assertEquals(1, scd.getHeader().getHistory().getHitem().size());
Expand All @@ -108,7 +107,7 @@ void createSCD_SSD_Without_Substation() {
// When & Then
List<SCL> stdListEmpty = List.of();
assertThrows(ScdException.class,
() -> SclAutomationService.createSCD(ssd, headerDTO, stdListEmpty));
() -> sclAutomationService.createSCD(ssd, headerDTO, stdListEmpty));
}

@Test
Expand All @@ -123,7 +122,7 @@ void createSCD_should_throw_exception_when_null_ssd() {
List<SCL> stdList = List.of(std1);

// When & Then
assertThrows(NullPointerException.class, () -> SclAutomationService.createSCD(null, headerDTO, stdList));
assertThrows(NullPointerException.class, () -> sclAutomationService.createSCD(null, headerDTO, stdList));
}

@Test
Expand All @@ -134,7 +133,7 @@ void createSCD_should_throw_exception_when_null_headerDTO() {
List<SCL> stdList = List.of(std1);

// When & Then
assertThrows(NullPointerException.class, () -> SclAutomationService.createSCD(ssd, null, stdList));
assertThrows(NullPointerException.class, () -> sclAutomationService.createSCD(ssd, null, stdList));
}

@Test
Expand All @@ -143,7 +142,7 @@ void createSCD_should_delete_ControlBlocks_DataSet_and_ExtRef_src_attributes() {
SCL ssd = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/ssd.xml");
SCL std = SclTestMarshaller.getSCLFromFile("/scl-remove-controlBlocks-dataSet-extRefSrc/scl-with-control-blocks.xml");
// When
SCL scd = SclAutomationService.createSCD(ssd, headerDTO, List.of(std));
SCL scd = sclAutomationService.createSCD(ssd, headerDTO, List.of(std));
// Then
LN0 ln0 = new SclRootAdapter(scd).streamIEDAdapters()
.findFirst()
Expand All @@ -158,16 +157,4 @@ void createSCD_should_delete_ControlBlocks_DataSet_and_ExtRef_src_attributes() {
assertIsMarshallable(scd);
}

@Test
void class_should_not_be_instantiable() {
// Given
Constructor<?>[] constructors = SclAutomationService.class.getDeclaredConstructors();
assertThat(constructors).hasSize(1);
Constructor<?> constructor = constructors[0];
constructor.setAccessible(true);
// When & Then
assertThatThrownBy(constructor::newInstance)
.isInstanceOf(InvocationTargetException.class)
.getCause().isInstanceOf(UnsupportedOperationException.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2023 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.compas.sct.commons.dto;

import org.lfenergy.compas.scl2007b4.model.SCL;
import org.lfenergy.compas.scl2007b4.model.TPrivate;

import java.util.List;

public record PrivateLinkedToSTDs(TPrivate tPrivate, List<SCL> stdList) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.lfenergy.compas.sct.commons.scl;

import lombok.NonNull;
import lombok.experimental.UtilityClass;
import org.lfenergy.compas.scl2007b4.model.*;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.scl.icd.IcdHeader;
Expand All @@ -26,19 +27,12 @@
* <ol>
* <li>{@link PrivateService#extractCompasPrivates(TBaseElement, Class)
* <em>Returns the value of the <b>TPrivate </b> containment reference list from given <b>TBaseElement </b> By class type</em>}</li>
*
* <li>{@link PrivateService#extractCompasPrivates(TBaseElement, Class)}
* <em>Returns the value of the <b>TPrivate </b> containment reference list from given <b>TPrivate </b> elements By class type</em>}
* </li>
* </ol>
* @see org.lfenergy.compas.scl2007b4.model.TPrivate
*/
@UtilityClass
public final class PrivateService {

private PrivateService() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

private static final ObjectFactory objectFactory = new ObjectFactory();

/**
Expand Down
Loading

0 comments on commit e38130a

Please sign in to comment.