Skip to content

Commit

Permalink
MET-5960 code reorganization to be able to use with sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
jeortizquan committed Sep 20, 2024
1 parent a316a9a commit 45ee11c
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 42 deletions.
6 changes: 1 addition & 5 deletions metis-debias/metis-debias-detect-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@
<version>${version.org.wiremock}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${version.apache.httpclient}</version>
</dependency>

</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package eu.europeana.metis.debias.detect.rest.config;

import eu.europeana.metis.debias.detect.rest.client.DeBiasClient;
import eu.europeana.metis.debias.detect.client.DeBiasClient;
import eu.europeana.metis.debias.detect.service.DetectService;
import eu.europeana.metis.utils.CustomTruststoreAppender;
import eu.europeana.metis.utils.CustomTruststoreAppender.TrustStoreConfigurationException;
import eu.europeana.metis.utils.apm.ElasticAPMConfiguration;
import java.lang.invoke.MethodHandles;
import metis.common.config.properties.TruststoreConfigurationProperties;
Expand All @@ -22,7 +23,7 @@
@Configuration
@EnableConfigurationProperties({ElasticAPMConfiguration.class, TruststoreConfigurationProperties.class})
@ComponentScan(basePackages = {
"eu.europeana.metis.debias.rest"})
"eu.europeana.metis.debias.detect.rest"})
public class ApplicationConfiguration {

private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
Expand All @@ -44,7 +45,7 @@ public class ApplicationConfiguration {
*/
@Autowired
public ApplicationConfiguration(TruststoreConfigurationProperties truststoreConfigurationProperties)
throws CustomTruststoreAppender.TrustStoreConfigurationException {
throws TrustStoreConfigurationException {
ApplicationConfiguration.initializeTruststore(truststoreConfigurationProperties);
}

Expand All @@ -65,7 +66,7 @@ public DetectService detectService() {
* @throws TrustStoreConfigurationException the trust store configuration exception
*/
static void initializeTruststore(TruststoreConfigurationProperties truststoreConfigurationProperties)
throws CustomTruststoreAppender.TrustStoreConfigurationException {
throws TrustStoreConfigurationException {
if (StringUtils.isNotEmpty(truststoreConfigurationProperties.getPath()) && StringUtils
.isNotEmpty(truststoreConfigurationProperties.getPassword())) {
CustomTruststoreAppender
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package eu.europeana.metis.debias.detect.rest.exceptions;

import eu.europeana.metis.debias.detect.exceptions.DeBiasBadRequestException;
import eu.europeana.metis.debias.detect.exceptions.DeBiasInternalServerException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.net.URISyntaxException;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
Expand All @@ -18,6 +19,26 @@
@ControllerAdvice
public class ExceptionResponseHandler {

/**
* Handle generic server error.
*
* @param response the response
* @param request the request
* @param exception the exception
* @return the server error
*/
@ResponseBody
@ExceptionHandler({Exception.class})
public ServerError handleResponse(HttpServletResponse response, HttpServletRequest request,
Exception exception) {
final ResponseStatus annotationResponseStatus = AnnotationUtils
.findAnnotation(exception.getClass(), ResponseStatus.class);
HttpStatus status = annotationResponseStatus == null ? HttpStatus.INTERNAL_SERVER_ERROR
: annotationResponseStatus.value();
response.setStatus(status.value());
return new ServerError(response.getStatus(), exception.getMessage());
}

/**
* Handle Bad Request response server error.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.europeana.metis.debias.detect.client.DeBiasClient;
import eu.europeana.metis.debias.detect.exceptions.DeBiasBadRequestException;
import eu.europeana.metis.debias.detect.exceptions.DeBiasInternalServerException;
import eu.europeana.metis.debias.detect.rest.exceptions.ExceptionResponseHandler;
import eu.europeana.metis.debias.detect.model.error.Detail;
import eu.europeana.metis.debias.detect.model.error.ErrorDeBiasResult;
import eu.europeana.metis.debias.detect.model.error.Input;
Expand All @@ -18,13 +22,8 @@
import eu.europeana.metis.debias.detect.model.response.Metadata;
import eu.europeana.metis.debias.detect.model.response.Tag;
import eu.europeana.metis.debias.detect.model.response.ValueDetection;
import eu.europeana.metis.debias.detect.rest.client.DeBiasClient;
import eu.europeana.metis.debias.detect.rest.exceptions.DeBiasBadRequestException;
import eu.europeana.metis.debias.detect.rest.exceptions.DeBiasInternalServerException;
import eu.europeana.metis.debias.detect.rest.exceptions.ExceptionResponseHandler;
import eu.europeana.metis.debias.detect.service.DetectService;
import eu.europeana.metis.utils.RestEndpoints;
import java.net.URISyntaxException;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -39,6 +38,18 @@ class DetectionControllerTest {
private MockMvc mockMvc;
private DetectService detectService;

private static String getDetectionParameterJson() throws JsonProcessingException {
DetectionParameter detectionParameter = new DetectionParameter();
detectionParameter.setValues(List.of(
"sample title of aboriginal and addict",
"a second addict sample title",
"this is a demo of master and slave branch"));
detectionParameter.setLanguage("en");
ObjectMapper mapper = new ObjectMapper();

return mapper.writeValueAsString(detectionParameter);
}

@BeforeEach
void setUp() {
detectService = mock(DeBiasClient.class);
Expand Down Expand Up @@ -164,7 +175,8 @@ void debias_detect_expectBadRequest() throws Exception {
void debias_detect_expectInternalServerError() throws Exception {
String detectionParameterJson = getDetectionParameterJson();

when(detectService.detect(any(DetectionParameter.class))).thenThrow(new DeBiasInternalServerException("Internal Server Error"));
when(detectService.detect(any(DetectionParameter.class))).thenThrow(
new DeBiasInternalServerException("Internal Server Error"));

mockMvc.perform(MockMvcRequestBuilders.post(RestEndpoints.DEBIAS_DETECTION)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -173,16 +185,4 @@ void debias_detect_expectInternalServerError() throws Exception {
.andExpect(status().is(500))
.andExpect(jsonPath("$.errorMessage", containsString("Internal Server Error")));
}

private static String getDetectionParameterJson() throws JsonProcessingException {
DetectionParameter detectionParameter = new DetectionParameter();
detectionParameter.setValues(List.of(
"sample title of aboriginal and addict",
"a second addict sample title",
"this is a demo of master and slave branch"));
detectionParameter.setLanguage("en");
ObjectMapper mapper = new ObjectMapper();

return mapper.writeValueAsString(detectionParameter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.http.JvmProxyConfigurer;
import eu.europeana.metis.debias.detect.model.request.DetectionParameter;
import eu.europeana.metis.debias.detect.rest.client.DeBiasClient;
import eu.europeana.metis.debias.detect.client.DeBiasClient;
import eu.europeana.metis.debias.detect.rest.exceptions.ExceptionResponseHandler;
import eu.europeana.metis.debias.detect.service.DetectService;
import eu.europeana.metis.utils.RestEndpoints;
Expand Down
12 changes: 11 additions & 1 deletion metis-debias/metis-debias-detect-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -31,6 +30,17 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>${version.org.wiremock}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${version.apache.httpclient}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package eu.europeana.metis.debias.detect.rest.client;
package eu.europeana.metis.debias.detect.client;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import eu.europeana.metis.debias.detect.exceptions.DeBiasBadRequestException;
import eu.europeana.metis.debias.detect.exceptions.DeBiasInternalServerException;
import eu.europeana.metis.debias.detect.model.DeBiasResult;
import eu.europeana.metis.debias.detect.model.error.ErrorDeBiasResult;
import eu.europeana.metis.debias.detect.model.request.DetectionParameter;
import eu.europeana.metis.debias.detect.model.response.DetectionDeBiasResult;
import eu.europeana.metis.debias.detect.rest.exceptions.DeBiasBadRequestException;
import eu.europeana.metis.debias.detect.rest.exceptions.DeBiasInternalServerException;
import eu.europeana.metis.debias.detect.service.DetectService;
import java.lang.invoke.MethodHandles;
import java.net.URI;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.europeana.metis.debias.detect.rest.exceptions;
package eu.europeana.metis.debias.detect.exceptions;

import java.io.Serial;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.europeana.metis.debias.detect.rest.exceptions;
package eu.europeana.metis.debias.detect.exceptions;

import java.io.Serial;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package eu.europeana.metis.debias.detect.rest.client;
package eu.europeana.metis.debias.detect.client;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.http.JvmProxyConfigurer;
import eu.europeana.metis.debias.detect.exceptions.DeBiasBadRequestException;
import eu.europeana.metis.debias.detect.model.request.DetectionParameter;
import eu.europeana.metis.debias.detect.model.response.DetectionDeBiasResult;
import eu.europeana.metis.debias.detect.rest.exceptions.DeBiasBadRequestException;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -133,4 +130,3 @@ void detect_noService_errorResponse() {
assertThrows(ResourceAccessException.class, () -> debiasClient.detect(detectionParameter));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"detail": [
{
"type": "string_type",
"loc": [
"body",
"language"
],
"msg": "Input should be a valid string",
"input": null,
"url": "https://errors.pydantic.dev/2.5/v/string_type"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"metadata": {
"annotator": "de-bias",
"thesaurus": null,
"date": "2024-08-27T12:04:17"
},
"results": [
{
"language": "en",
"literal": "sample title of aboriginal and addict",
"tags": [
{
"uri": "http://www.example.org/debias#t_2_en",
"start": 16,
"end": 26,
"length": 10
},
{
"uri": "http://www.example.org/debias#t_3_en",
"start": 31,
"end": 37,
"length": 6
}
]
},
{
"language": "en",
"literal": "a second addict sample title",
"tags": [
{
"uri": "http://www.example.org/debias#t_3_en",
"start": 9,
"end": 15,
"length": 6
}
]
},
{
"language": "en",
"literal": "this is a demo of master and slave branch",
"tags": [
{
"uri": "http://www.example.org/debias#t_198_en",
"start": 29,
"end": 34,
"length": 5
}
]
}
]
}

0 comments on commit 45ee11c

Please sign in to comment.