Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ayoub LABIDI <ayoub.labidi@protonmail.com>
  • Loading branch information
ayolab committed Jan 2, 2024
1 parent 5eb78ef commit c596fdb
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public class LoadFlowParametersEntity {
@Column(name = "dcPowerFactor", columnDefinition = "double default 1.0")
private double dcPowerFactor;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "load_flow_parameters_id")
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(name = "load_flow_parameters_entity_id", foreignKey = @ForeignKey(name = "loadFlowParametersEntity_specificParameters_fk"))
private List<LoadFlowSpecificParameterEntity> specificParameters;

public LoadFlowParametersEntity(LoadFlowParametersInfos loadFlowParametersInfos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -27,7 +28,9 @@
@AllArgsConstructor
@Getter
@Entity
@Table(name = "loadFlowSpecificParameters")
@Table(name = "loadFlowSpecificParameters", indexes = {
@Index(name = "loadFlowParametersEntity_specificParameters_index", columnList = "load_flow_parameters_entity_id")
})
public class LoadFlowSpecificParameterEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public LoadFlowParametersService(LoadFlowParametersRepository loadFlowParameters
}

public UUID createParameters(LoadFlowParametersInfos parametersInfos) {
return loadFlowParametersRepository.save(parametersInfos.toEntity()).toLoadFlowParametersInfos().getUuid();
return loadFlowParametersRepository.save(parametersInfos.toEntity()).getId();
}

public LoadFlowParametersInfos getParameters(UUID parametersUuid) {
Expand Down Expand Up @@ -59,6 +59,6 @@ public void deleteParameters(UUID parametersUuid) {
public UUID duplicateParameters(UUID sourceParametersUuid) {
LoadFlowParametersEntity sourceParameters = loadFlowParametersRepository.findById(sourceParametersUuid).orElseThrow();
LoadFlowParametersEntity duplicatedParameters = sourceParameters.copy();
return loadFlowParametersRepository.save(duplicatedParameters).toLoadFlowParametersInfos().getUuid();
return loadFlowParametersRepository.save(duplicatedParameters).getId();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet author="labidiayo (generated)" id="1703600892902-1">
<changeSet author="labidiayo (generated)" id="1703778855378-1">
<createTable tableName="load_flow_parameters">
<column name="id" type="UUID">
<constraints nullable="false" primaryKey="true" primaryKeyName="load_flow_parametersPK"/>
Expand All @@ -22,34 +22,39 @@
<column defaultValueBoolean="false" name="write_slack_bus" type="BOOLEAN"/>
</createTable>
</changeSet>
<changeSet author="labidiayo (generated)" id="1703600892902-2">
<changeSet author="labidiayo (generated)" id="1703778855378-2">
<createTable tableName="load_flow_parameters_entity_countries_to_balance">
<column name="load_flow_parameters_entity_id" type="UUID">
<constraints nullable="false"/>
</column>
<column name="countries_to_balance" type="VARCHAR(255)"/>
</createTable>
</changeSet>
<changeSet author="labidiayo (generated)" id="1703600892902-3">
<changeSet author="labidiayo (generated)" id="1703778855378-3">
<createTable tableName="load_flow_specific_parameters">
<column name="id" type="UUID">
<constraints nullable="false" primaryKey="true" primaryKeyName="load_flow_specific_parametersPK"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="provider" type="VARCHAR(255)"/>
<column name="value" type="VARCHAR(255)"/>
<column name="load_flow_parameters_id" type="UUID"/>
<column name="load_flow_parameters_entity_id" type="UUID"/>
</createTable>
</changeSet>
<changeSet author="labidiayo (generated)" id="1703600892902-4">
<changeSet author="labidiayo (generated)" id="1703778855378-4">
<createIndex indexName="loadFlowParametersEntity_countriesToBalance_idx1" tableName="load_flow_parameters_entity_countries_to_balance">
<column name="load_flow_parameters_entity_id"/>
</createIndex>
</changeSet>
<changeSet author="labidiayo (generated)" id="1703600892902-5">
<addForeignKeyConstraint baseColumnNames="load_flow_parameters_id" baseTableName="load_flow_specific_parameters" constraintName="FKhv4sw663c5fkfkw42vsrlrbxo" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="load_flow_parameters" validate="true"/>
<changeSet author="labidiayo (generated)" id="1703778855378-5">
<createIndex indexName="loadFlowParametersEntity_specificParameters_index" tableName="load_flow_specific_parameters">
<column name="load_flow_parameters_entity_id"/>
</createIndex>
</changeSet>
<changeSet author="labidiayo (generated)" id="1703600892902-6">
<changeSet author="labidiayo (generated)" id="1703778855378-6">
<addForeignKeyConstraint baseColumnNames="load_flow_parameters_entity_id" baseTableName="load_flow_parameters_entity_countries_to_balance" constraintName="loadFlowParametersEntity_countriesToBalance_fk1" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="load_flow_parameters" validate="true"/>
</changeSet>
<changeSet author="labidiayo (generated)" id="1703778855378-7">
<addForeignKeyConstraint baseColumnNames="load_flow_parameters_entity_id" baseTableName="load_flow_specific_parameters" constraintName="loadFlowParametersEntity_specificParameters_fk" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="load_flow_parameters" validate="true"/>
</changeSet>
</databaseChangeLog>
2 changes: 1 addition & 1 deletion src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ databaseChangeLog:
file: changesets/changelog_20231115T10528Z.xml
relativeToChangelogFile: true
- include:
file: changesets/changelog_20231226T142800Z.xml
file: changesets/changelog_20231228T155403Z.xml
relativeToChangelogFile: true

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.gridsuite.loadflow.server.service.LoadFlowExecutionService;
import org.gridsuite.loadflow.server.service.ReportService;
import org.gridsuite.loadflow.server.service.UuidGeneratorService;
import org.gridsuite.loadflow.server.service.parameters.LoadFlowParametersService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -45,6 +46,7 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.cloud.stream.binder.test.OutputDestination;
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
import org.springframework.http.MediaType;
Expand All @@ -69,6 +71,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -86,6 +89,7 @@ public class LoadFlowControllerTest {
private static final UUID RESULT_UUID = UUID.fromString("0c8de370-3e6c-4d72-b292-d355a97e0d5d");
private static final UUID OTHER_RESULT_UUID = UUID.fromString("0c8de370-3e6c-4d72-b292-d355a97e0d5a");
private static final UUID REPORT_UUID = UUID.fromString("762b7298-8c0f-11ed-a1eb-0242ac120002");
private static final UUID PARAMETERS_UUID = UUID.fromString("762b7298-8c0f-11ed-a1eb-0242ac120003");

private static final String VARIANT_1_ID = "variant_1";
private static final String VARIANT_2_ID = "variant_2";
Expand Down Expand Up @@ -123,6 +127,9 @@ private static final class LimitViolationsMock {
@Autowired
private LoadFlowExecutionService loadFlowExecutionService;

@SpyBean
private LoadFlowParametersService loadFlowParametersService;

@MockBean
private UuidGeneratorService uuidGeneratorService;
private final RestTemplateConfig restTemplateConfig = new RestTemplateConfig();
Expand Down Expand Up @@ -217,7 +224,7 @@ public void runTest() throws Exception {
.thenReturn(CompletableFuture.completedFuture(LoadFlowResultMock.RESULT));

MvcResult result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&receiver=me&variantId=" + VARIANT_2_ID + "&parametersUuid=" + PARAMETERS_UUID, NETWORK_UUID)
.header(HEADER_USER_ID, "userId"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand Down Expand Up @@ -267,11 +274,10 @@ public void testGetLimitViolations() throws Exception {
.commonParameters(loadFlowParameters)
.specificParameters(Collections.emptyMap())
.build();
String jsonLoadFlowParameters = mapper.writeValueAsString(loadFlowParametersInfos);
doReturn(loadFlowParametersInfos).when(loadFlowParametersService).getParametersValues(any(), any());

MvcResult result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&receiver=me&variantId=" + VARIANT_2_ID + "&limitReduction=0.7", NETWORK_UUID)
.content(jsonLoadFlowParameters).contentType(MediaType.APPLICATION_JSON)
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&receiver=me&variantId=" + VARIANT_2_ID + "&parametersUuid=" + PARAMETERS_UUID + "&limitReduction=0.7", NETWORK_UUID)
.header(HEADER_USER_ID, "userId"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand Down Expand Up @@ -304,7 +310,7 @@ public void testDeleteResults() throws Exception {
.thenReturn(CompletableFuture.completedFuture(LoadFlowResultMock.RESULT));

MvcResult result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&receiver=me&variantId=" + VARIANT_2_ID + "&parametersUuid=" + PARAMETERS_UUID, NETWORK_UUID)
.header(HEADER_USER_ID, "userId"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand Down Expand Up @@ -342,7 +348,7 @@ public void stopTest() throws Exception {
.thenReturn(CompletableFuture.completedFuture(LoadFlowResultMock.RESULT));

mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&receiver=me&variantId=" + VARIANT_2_ID + "&parametersUuid=" + PARAMETERS_UUID, NETWORK_UUID)
.header(HEADER_USER_ID, "userId"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand Down Expand Up @@ -393,7 +399,7 @@ public void runWithReportTest() {
.thenReturn(CompletableFuture.completedFuture(LoadFlowResultMock.RESULT));

mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&reporterId=myReporter&receiver=me&reportUuid=" + REPORT_UUID + "&variantId=" + VARIANT_2_ID, NETWORK_UUID)
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&reporterId=myReporter&receiver=me&reportUuid=" + REPORT_UUID + "&variantId=" + VARIANT_2_ID + "&parametersUuid=" + PARAMETERS_UUID, NETWORK_UUID)
.header(HEADER_USER_ID, "user"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand All @@ -412,7 +418,7 @@ public void runWithDefaultVariant() {
.thenReturn(CompletableFuture.completedFuture(LoadFlowResultMock.RESULT));

mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reporterId=myReporter&receiver=me&reportUuid=" + REPORT_UUID, NETWORK_UUID)
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reporterId=myReporter&receiver=me&reportUuid=" + REPORT_UUID + "&parametersUuid=" + PARAMETERS_UUID, NETWORK_UUID)
.header(HEADER_USER_ID, "user"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
Expand Down
Loading

0 comments on commit c596fdb

Please sign in to comment.