Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH committed Jun 19, 2024
1 parent 1f74215 commit 4b94c66
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 107 deletions.
16 changes: 6 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.powsybl.ws.commons.computation;

import com.fasterxml.jackson.databind.InjectableValues;
import com.powsybl.commons.report.ReportNodeDeserializer;
import com.powsybl.commons.report.ReportNodeJsonModule;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ComputationConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> builder.modulesToInstall(new ReportNodeJsonModule())
.postConfigurer(objMapper -> objMapper.setInjectableValues(new InjectableValues.Std().addValue(ReportNodeDeserializer.DICTIONARY_VALUE_ID, null)));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,113 +7,83 @@

package com.powsybl.ws.commons.computation.service;

import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.commons.report.ReportNodeDeserializer;
import com.powsybl.commons.report.ReportNodeJsonModule;
import lombok.extern.slf4j.Slf4j;
import okhttp3.HttpUrl;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import com.powsybl.ws.commons.computation.ComputationConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient;
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.client.match.MockRestRequestMatchers;
import org.springframework.test.web.client.response.MockRestResponseCreators;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.assertj.core.api.Assertions.*;

/**
* @author Mathieu Deharbe <mathieu.deharbe_externe at rte-france.com
*/
@RunWith(SpringRunner.class)
@Slf4j
public class ReportServiceTest {

@RestClientTest(ReportService.class)
@AutoConfigureWebClient(registerRestTemplate = true)
@ContextConfiguration(classes = {ComputationConfig.class, ReportService.class})
class ReportServiceTest {
private static final UUID REPORT_UUID = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
private static final UUID REPORT_ERROR_UUID = UUID.fromString("9928181c-7977-4592-ba19-88027e4254e4");

private static final String REPORT_JSON = "{\"version\":\"2.0\",\"reportRoot\":{\"messageKey\":\"test\",\"dictionaries\":{\"default\":{\"test\":\"a test\"}}}}";

private MockWebServer server;

@Autowired
private ReportService reportService;

@Before
public void setUp() throws IOException {
var objectMapper = Jackson2ObjectMapperBuilder.json().build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.registerModule(new ReportNodeJsonModule());
objectMapper.setInjectableValues(new InjectableValues.Std().addValue(ReportNodeDeserializer.DICTIONARY_VALUE_ID, null));
@Autowired
private MockRestServiceServer server;

reportService = new ReportService(objectMapper,
initMockWebServer(),
new RestTemplate());
@AfterEach
void tearDown() {
server.verify();
}

@After
public void tearDown() {
try {
server.shutdown();
} catch (Exception e) {
// Nothing to do
}
@Test
void testSendReport() {
final ReportNode reportNode = ReportNode.newRootReportNode().withMessageTemplate("test", "a test").build();
server.expect(MockRestRequestMatchers.method(HttpMethod.PUT))
.andExpect(MockRestRequestMatchers.requestTo("http://report-server/v1/reports/" + REPORT_UUID))
.andExpect(MockRestRequestMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(MockRestRequestMatchers.content().json(REPORT_JSON))
.andRespond(MockRestResponseCreators.withSuccess());
assertThatNoException().isThrownBy(() -> reportService.sendReport(REPORT_UUID, reportNode));
}

private String initMockWebServer() throws IOException {
server = new MockWebServer();
server.start();

final Dispatcher dispatcher = new Dispatcher() {
@NotNull
@Override
public MockResponse dispatch(RecordedRequest request) {
String requestPath = Objects.requireNonNull(request.getPath());
if (requestPath.equals(String.format("/v1/reports/%s", REPORT_UUID))) {
assertEquals(REPORT_JSON, request.getBody().readUtf8());
return new MockResponse().setResponseCode(HttpStatus.OK.value());
} else if (requestPath.equals(String.format("/v1/reports/%s?reportTypeFilter=MockReportType&errorOnReportNotFound=false", REPORT_UUID))) {
assertEquals("", request.getBody().readUtf8());
return new MockResponse().setResponseCode(HttpStatus.OK.value());
} else if (requestPath.equals(String.format("/v1/reports/%s", REPORT_ERROR_UUID))) {
return new MockResponse().setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
} else {
return new MockResponse().setResponseCode(HttpStatus.NOT_FOUND.value()).setBody("Path not supported: " + request.getPath());
}
}
};

server.setDispatcher(dispatcher);

// Ask the server for its URL. You'll need this to make HTTP requests.
HttpUrl baseHttpUrl = server.url("");
return baseHttpUrl.toString().substring(0, baseHttpUrl.toString().length() - 1);
@Test
void testSendReportFailed() {
final ReportNode reportNode = ReportNode.newRootReportNode().withMessageTemplate("test", "a test").build();
server.expect(MockRestRequestMatchers.method(HttpMethod.PUT))
.andExpect(MockRestRequestMatchers.requestTo("http://report-server/v1/reports/" + REPORT_ERROR_UUID))
.andRespond(MockRestResponseCreators.withServerError());
assertThatThrownBy(() -> reportService.sendReport(REPORT_ERROR_UUID, reportNode)).isInstanceOf(RestClientException.class);
}

@Test
public void testSendReport() {
ReportNode reportNode = ReportNode.newRootReportNode().withMessageTemplate("test", "a test").build();
reportService.sendReport(REPORT_UUID, reportNode);
assertThrows(RestClientException.class, () -> reportService.sendReport(REPORT_ERROR_UUID, reportNode));
void testDeleteReport() {
server.expect(MockRestRequestMatchers.method(HttpMethod.DELETE))
.andExpect(MockRestRequestMatchers.requestTo("http://report-server/v1/reports/" + REPORT_UUID + "?reportTypeFilter=MockReportType&errorOnReportNotFound=false"))
.andExpect(MockRestRequestMatchers.content().bytes(new byte[0]))
.andRespond(MockRestResponseCreators.withSuccess());
assertThatNoException().isThrownBy(() -> reportService.deleteReport(REPORT_UUID, "MockReportType"));
}

@Test
public void testDeleteReport() {
reportService.deleteReport(REPORT_UUID, "MockReportType");
assertThrows(RestClientException.class, () -> reportService.deleteReport(REPORT_ERROR_UUID, "MockReportType"));
void testDeleteReportFailed() {
server.expect(MockRestRequestMatchers.method(HttpMethod.DELETE))
.andExpect(MockRestRequestMatchers.requestTo("http://report-server/v1/reports/" + REPORT_ERROR_UUID + "?reportTypeFilter=MockReportType&errorOnReportNotFound=false"))
.andExpect(MockRestRequestMatchers.content().bytes(new byte[0]))
.andRespond(MockRestResponseCreators.withServerError());
assertThatThrownBy(() -> reportService.deleteReport(REPORT_ERROR_UUID, "MockReportType")).isInstanceOf(RestClientException.class);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.powsybl.ws_common_spring_test;

import com.powsybl.ws.commons.computation.config.JacksonConfig;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
Expand All @@ -10,6 +9,6 @@
@SuppressWarnings({
"java:S2187" //this isn't a class containing tests
})
@SpringBootApplication(scanBasePackageClasses = {JacksonConfig.class})
@SpringBootApplication
public class SpringBootApplicationForTest {
}

0 comments on commit 4b94c66

Please sign in to comment.