Skip to content

Commit

Permalink
refactor(#110): check review part2 [use mockito properly]
Browse files Browse the repository at this point in the history
Remove unused test package.
Use latest version of mockito.
Fix related to version 5.4.0

Signed-off-by: Samir Romdhani <[email protected]>
  • Loading branch information
samirromdhani committed Aug 25, 2023
1 parent 6e37f28 commit 580a98a
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 24 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<logback-classic.version>1.4.5</logback-classic.version>
<assertj.version>3.22.0</assertj.version>
<lombok.version>1.18.24</lombok.version>
<mockito.version>5.5.0</mockito.version>
<jackson-databind.version>2.13.4.1</jackson-databind.version>
</properties>

Expand Down Expand Up @@ -78,6 +79,18 @@
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.lfenergy.compas.core</groupId>
<artifactId>scl-extension</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions sct-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// SPDX-FileCopyrightText: 2021 2022 2023 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0

package org.lfenergy.compas.sct.app;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lfenergy.compas.scl2007b4.model.SCL;
import org.lfenergy.compas.scl2007b4.model.THeader;
import org.lfenergy.compas.sct.commons.dto.HeaderDTO;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.service.ISclService;
import org.lfenergy.compas.sct.commons.service.ISubstationService;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller.assertIsMarshallable;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class SclAutomationServiceTest {

@InjectMocks
private SclAutomationService sclAutomationService ;
@Mock
private ISclService sclService;
@Mock
private ISubstationService substationService;

public static final short RELEASE = 4;
public static final String REVISION = "B";
public static final String VERSION = "2007";
private SCL scl;
private HeaderDTO headerDTO;

@BeforeEach
void setUp() {
scl = new SCL();
scl.setRelease(RELEASE);
scl.setVersion(VERSION);
scl.setRevision(REVISION);
THeader header = new THeader();
header.setId(UUID.randomUUID().toString());
scl.setHeader(header);
headerDTO = new HeaderDTO();
headerDTO.setId(UUID.randomUUID());
headerDTO.setRevision("Revision");
headerDTO.setVersion("Version");
}

@Test
void createSCD_without_headerHistory_should_return_generatedSCD() throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
// Given
SCL ssd = (SCL) BeanUtils.cloneBean(scl);
SCL std = (SCL) BeanUtils.cloneBean(scl);
Mockito.when(sclService.initScl(any(UUID.class), anyString(), anyString())).thenReturn((SCL) BeanUtils.cloneBean(scl));
// When
SCL scd = sclAutomationService.createSCD(ssd, headerDTO, List.of(std));
// Then
assertThat(scd.getHeader()).isNotNull();
assertThat(scd.getHeader().getHistory()).isNull();
assertThat(scd.getDataTypeTemplates()).isNull();
assertThat(scd.getCommunication()).isNull();
assertThat(scd.getSubstation()).isEmpty();
assertThat(scd.getIED()).isEmpty();
assertIsMarshallable(scd);
verify(sclService, times(1)).initScl(headerDTO.getId(), headerDTO.getVersion(), headerDTO.getRevision());
verify(sclService, times(0)).addHistoryItem(any(SCL.class), anyString(), anyString(), anyString());
verify(substationService, times(1)).addSubstation(any(SCL.class), any(SCL.class));
verify(sclService, times(1)).importSTDElementsInSCD(any(SCL.class), anyList(), anyList());
verify(sclService, times(1)).removeAllControlBlocksAndDatasetsAndExtRefSrcBindings(any(SCL.class));
}

@Test
void createSCD_with_headerHistory_should_return_generatedSCD() throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
// Given
SCL ssd = (SCL) BeanUtils.cloneBean(scl);
SCL std = (SCL) BeanUtils.cloneBean(scl);
HeaderDTO.HistoryItem historyItem = new HeaderDTO.HistoryItem();
historyItem.setWhat("test");
historyItem.setWho("name");
historyItem.setWhy("test");
headerDTO.getHistoryItems().add(historyItem);
when(sclService.initScl(any(UUID.class), anyString(), anyString())).thenReturn((SCL) BeanUtils.cloneBean(scl));
doNothing().when(sclService).addHistoryItem(any(SCL.class), anyString(), anyString(), anyString());
// When
SCL scd = sclAutomationService.createSCD(ssd, headerDTO, List.of(std));
// Then
assertThat(scd.getHeader()).isNotNull();
assertThat(scd.getHeader().getHistory()).isNull();
assertThat(scd.getDataTypeTemplates()).isNull();
assertThat(scd.getCommunication()).isNull();
assertThat(scd.getSubstation()).isEmpty();
assertThat(scd.getIED()).isEmpty();
assertIsMarshallable(scd);
verify(sclService, times(1)).initScl(headerDTO.getId(), headerDTO.getVersion(), headerDTO.getRevision());
verify(sclService, times(1)).addHistoryItem(any(SCL.class), eq(historyItem.getWho()), eq(historyItem.getWhat()), eq(historyItem.getWhy()));
verify(substationService, times(1)).addSubstation(any(SCL.class), any(SCL.class));
verify(sclService, times(1)).importSTDElementsInSCD(any(SCL.class), anyList(), anyList());
verify(sclService, times(1)).removeAllControlBlocksAndDatasetsAndExtRefSrcBindings(any(SCL.class));
}

@Test
void createSCD_should_throw_exception_when_sclService_initScl_Fail() throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
// Given
SCL ssd = (SCL) BeanUtils.cloneBean(scl);
SCL std = (SCL) BeanUtils.cloneBean(scl);
doThrow(new ScdException("initScl fail")).when(sclService).initScl(any(UUID.class), anyString(), anyString());
// When Then
assertThatThrownBy(() -> sclAutomationService.createSCD(ssd, headerDTO, List.of(std)))
.isInstanceOf(ScdException.class)
.hasMessage("initScl fail");
}

@Test
void createSCD_should_throw_exception_when_sclService_addHistoryItem_Fail() throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
// Given
SCL ssd = (SCL) BeanUtils.cloneBean(scl);
SCL std = (SCL) BeanUtils.cloneBean(scl);
headerDTO.getHistoryItems().add(new HeaderDTO.HistoryItem());
when(sclService.initScl(any(UUID.class), anyString(), anyString())).thenReturn((SCL) BeanUtils.cloneBean(scl));
doThrow(new ScdException("addHistoryItem fail")).when(sclService).addHistoryItem(any(SCL.class), any(), any(), any());
// When Then
assertThatThrownBy(() -> sclAutomationService.createSCD(ssd, headerDTO, List.of(std)))
.isInstanceOf(ScdException.class)
.hasMessage("addHistoryItem fail");
}

@Test
void createSCD_should_throw_exception_when_substationService_addSubstation_Fail() throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
// Given
SCL ssd = (SCL) BeanUtils.cloneBean(scl);
SCL std = (SCL) BeanUtils.cloneBean(scl);
headerDTO.getHistoryItems().add(new HeaderDTO.HistoryItem());
when(sclService.initScl(any(UUID.class), anyString(), anyString())).thenReturn((SCL) BeanUtils.cloneBean(scl));
doNothing().when(sclService).addHistoryItem(any(SCL.class), any(), any(), any());
doThrow(new ScdException("addSubstation fail")).when(substationService).addSubstation(any(SCL.class), any(SCL.class));
// When Then
assertThatThrownBy(() -> sclAutomationService.createSCD(ssd, headerDTO, List.of(std)))
.isInstanceOf(ScdException.class)
.hasMessage("addSubstation fail");
}

@Test
void createSCD_should_throw_exception_when_sclService_importSTDElementsInSCD_Fail() throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
// Given
SCL ssd = (SCL) BeanUtils.cloneBean(scl);
SCL std = (SCL) BeanUtils.cloneBean(scl);
headerDTO.getHistoryItems().add(new HeaderDTO.HistoryItem());
when(sclService.initScl(any(UUID.class), anyString(), anyString())).thenReturn((SCL) BeanUtils.cloneBean(scl));
doNothing().when(sclService).addHistoryItem(any(SCL.class), any(), any(), any());
doNothing().when(substationService).addSubstation(any(SCL.class), any(SCL.class));
doThrow(new ScdException("importSTDElementsInSCD fail"))
.when(sclService).importSTDElementsInSCD(any(SCL.class), anyList(), anyList());
// When Then
assertThatThrownBy(() -> sclAutomationService.createSCD(ssd, headerDTO, List.of(std)))
.isInstanceOf(ScdException.class)
.hasMessage("importSTDElementsInSCD fail");
}

@Test
void createSCD_should_throw_exception_when_sclService_removeAllControlBlocksAndDatasetsAndExtRefSrcBindings_Fail() throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
// Given
SCL ssd = (SCL) BeanUtils.cloneBean(scl);
SCL std = (SCL) BeanUtils.cloneBean(scl);
headerDTO.getHistoryItems().add(new HeaderDTO.HistoryItem());
when(sclService.initScl(any(UUID.class), anyString(), anyString())).thenReturn((SCL) BeanUtils.cloneBean(scl));
doNothing().when(sclService).addHistoryItem(any(SCL.class), any(), any(), any());
doNothing().when(substationService).addSubstation(any(SCL.class), any(SCL.class));
doNothing().when(sclService).importSTDElementsInSCD(any(SCL.class), anyList(), anyList());
doThrow(new ScdException("removeAllControlBlocksAndDatasetsAndExtRefSrcBindings fail"))
.when(sclService).removeAllControlBlocksAndDatasetsAndExtRefSrcBindings(any(SCL.class));
// When Then
assertThatThrownBy(() -> sclAutomationService.createSCD(ssd, headerDTO, List.of(std)))
.isInstanceOf(ScdException.class)
.hasMessage("removeAllControlBlocksAndDatasetsAndExtRefSrcBindings fail");
}

}
22 changes: 0 additions & 22 deletions sct-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/${aggregate.report.dir}</sonar.coverage.jacoco.xmlReportPaths>
<sonar.jacoco.excludes>**/scl2007b4/**/*</sonar.jacoco.excludes>
<opencsv.version>5.7.1</opencsv.version>
<mockito.version>4.9.0</mockito.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -60,39 +59,18 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ void testGetControlBlocks() {
void testGetControlSetByBindingInfo() {

LN0 ln0 = new LN0();
LN0Adapter ln0Adapter = mock(LN0Adapter.class);
ln0Adapter = Mockito.spy(ln0Adapter);
LN0Adapter ln0Adapter = Mockito.spy(new LN0Adapter(null, ln0));

when(ln0Adapter.getCurrentElem()).thenReturn(ln0);

Expand Down

0 comments on commit 580a98a

Please sign in to comment.