Skip to content
Snippets Groups Projects
Commit 74a16034 authored by Rostislav Dublin (EPAM)'s avatar Rostislav Dublin (EPAM)
Browse files

Merge branch 'fix-cloud-run-tests-gcp' into 'master'

Integration tests for Google Cloud Run (GONRG-1016)

See merge request !68
parents 587925b2 28935725
No related branches found
No related tags found
1 merge request!68Integration tests for Google Cloud Run (GONRG-1016)
Pipeline #12878 failed
...@@ -14,20 +14,21 @@ ...@@ -14,20 +14,21 @@
package org.opengroup.osdu.legal.middleware; package org.opengroup.osdu.legal.middleware;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.gson.Gson;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javassist.NotFoundException;
import javax.inject.Inject;
import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException; import javax.validation.ValidationException;
import javax.inject.Inject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.google.gson.Gson;
import javassist.NotFoundException;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
...@@ -36,21 +37,14 @@ import org.springframework.http.ResponseEntity; ...@@ -36,21 +37,14 @@ import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest; import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.opengroup.osdu.core.common.model.http.AppException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice @RestControllerAdvice
@RestController
public class GlobalExceptionMapper extends ResponseEntityExceptionHandler { public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
private static final Gson gson = new Gson(); private static final Gson gson = new Gson();
...@@ -74,7 +68,7 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler { ...@@ -74,7 +68,7 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
return this.getErrorResponse( return this.getErrorResponse(
new AppException(HttpStatus.BAD_REQUEST.value(), "Bad JSON format", e.getMessage())); new AppException(HttpStatus.BAD_REQUEST.value(), "Bad JSON format", e.getMessage()));
} }
@ExceptionHandler(UnrecognizedPropertyException.class) @ExceptionHandler(UnrecognizedPropertyException.class)
protected ResponseEntity<Object> handleValidationException(UnrecognizedPropertyException e) { protected ResponseEntity<Object> handleValidationException(UnrecognizedPropertyException e) {
return this.getErrorResponse( return this.getErrorResponse(
...@@ -124,13 +118,24 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler { ...@@ -124,13 +118,24 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(@NonNull HttpRequestMethodNotSupportedException e, protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(@NonNull HttpRequestMethodNotSupportedException e,
@NonNull HttpHeaders headers, @NonNull HttpHeaders headers,
@NonNull HttpStatus status, @NonNull HttpStatus status,
@NonNull WebRequest request) { @NonNull WebRequest request) {
return this.getErrorResponse( return this.getErrorResponse(
new AppException(HttpStatus.METHOD_NOT_ALLOWED.value(), "Method not found.", new AppException(HttpStatus.METHOD_NOT_ALLOWED.value(), "Method not found.",
"Method not found.", e)); "Method not found.", e));
} }
private ResponseEntity<Object> getErrorResponse(AppException e) { @Override
@NonNull
protected ResponseEntity<Object> handleMethodArgumentNotValid(@NonNull MethodArgumentNotValidException e,
@NonNull HttpHeaders headers,
@NonNull HttpStatus status,
@NonNull WebRequest request) {
return this.getErrorResponse(
new AppException(HttpStatus.BAD_REQUEST.value(), "Validation failed.",
"Validation failed.", e));
}
public ResponseEntity<Object> getErrorResponse(AppException e) {
String exceptionMsg = e.getError().getMessage(); String exceptionMsg = e.getError().getMessage();
...@@ -140,6 +145,6 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler { ...@@ -140,6 +145,6 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
this.logger.warning(exceptionMsg, e); this.logger.warning(exceptionMsg, e);
} }
return new ResponseEntity<Object>(gson.toJson(exceptionMsg), HttpStatus.resolve(e.getError().getCode())); return new ResponseEntity<Object>(gson.toJson(exceptionMsg),HttpStatus.resolve(e.getError().getCode()));
} }
} }
\ No newline at end of file
// Copyright 2017-2020, Schlumberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.legal.middleware;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalOtherExceptionMapper {
private GlobalExceptionMapper mapper;
public GlobalOtherExceptionMapper(GlobalExceptionMapper mapper) {
this.mapper = mapper;
}
@ExceptionHandler(Exception.class)
protected ResponseEntity<Object> handleGeneralException(Exception e) {
return mapper.getErrorResponse(
new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error.",
"An unknown error has occurred.", e));
}
}
...@@ -125,6 +125,7 @@ You will need to have the following environment variables defined. ...@@ -125,6 +125,7 @@ You will need to have the following environment variables defined.
| `INTEGRATION_TESTER` | `********` | Service account for API calls. Note: this user must have entitlements configured already | yes | https://console.cloud.google.com/iam-admin/serviceaccounts | | `INTEGRATION_TESTER` | `********` | Service account for API calls. Note: this user must have entitlements configured already | yes | https://console.cloud.google.com/iam-admin/serviceaccounts |
| `HOST_URL` | `http://localhsot:8080/api/legal/v1/` | - | yes | - | | `HOST_URL` | `http://localhsot:8080/api/legal/v1/` | - | yes | - |
| `MY_TENANT` | `osdu` | OSDU tenant used for testing | yes | - | | `MY_TENANT` | `osdu` | OSDU tenant used for testing | yes | - |
| `SKIP_HTTP_TESTS` | ex `true` | jetty server returns 403 when running locally when deployed jettyserver is not used and the app returns a 302 so just run against deployed version only when checking http -> https redirects. Use 'true' for Google Cloud Run | yes | - |
**Entitlements configuration for integration accounts** **Entitlements configuration for integration accounts**
......
...@@ -233,7 +233,26 @@ ...@@ -233,7 +233,26 @@
<configuration> <configuration>
<version>1</version> <version>1</version>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>
...@@ -45,6 +45,12 @@ ...@@ -45,6 +45,12 @@
<artifactId>pact-jvm-provider-junit_2.12</artifactId> <artifactId>pact-jvm-provider-junit_2.12</artifactId>
<version>3.5.5</version> <version>3.5.5</version>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
......
...@@ -5,13 +5,14 @@ import static org.junit.Assert.assertNull; ...@@ -5,13 +5,14 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.opengroup.osdu.legal.util.Constants.DATA_PARTITION_ID; import static org.opengroup.osdu.legal.util.Constants.DATA_PARTITION_ID;
import com.sun.jersey.api.client.ClientResponse;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import com.sun.jersey.api.client.ClientResponse; import lombok.extern.java.Log;
import org.junit.Test; import org.junit.Test;
@Log
public abstract class AcceptanceBaseTest { public abstract class AcceptanceBaseTest {
protected LegalTagUtils legalTagUtils; protected LegalTagUtils legalTagUtils;
...@@ -84,17 +85,25 @@ public abstract class AcceptanceBaseTest { ...@@ -84,17 +85,25 @@ public abstract class AcceptanceBaseTest {
return response; return response;
} }
protected ClientResponse validateAccess(int expectedResponse) throws Exception { protected ClientResponse validateAccess(int expectedResponse) throws Exception {
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put(DATA_PARTITION_ID, LegalTagUtils.getMyDataPartition()); headers.put(DATA_PARTITION_ID, LegalTagUtils.getMyDataPartition());
ClientResponse response = legalTagUtils.send(this.getApi(), this.getHttpMethod(), legalTagUtils.accessToken(), getBody(), getQuery(), headers); ClientResponse response = legalTagUtils
assertEquals(expectedResponse, response.getStatus()); .send(this.getApi(), this.getHttpMethod(), legalTagUtils.accessToken(), getBody(),
if(expectedResponse == 204) getQuery(), headers);
assertNull(response.getType()); log.info("Response status = " + response.getStatus());
else if(response.getType() != null) { assertEquals(expectedResponse, response.getStatus());
assertTrue(response.getType().toString().toLowerCase().indexOf("application/json") >= 0); if (expectedResponse == 204) {
} if (Objects.nonNull(response.getType())) {
return response; log.info("Content-Type = " + response.getType().toString());
} assertTrue(response.getType().toString().toLowerCase().indexOf("text/html") >= 0); //Google Cloud Run specific
} else {
assertNull(response.getType());
}
} else if (response.getType() != null) {
assertTrue(response.getType().toString().toLowerCase().indexOf("application/json") >= 0);
}
return response;
}
} }
\ No newline at end of file
...@@ -18,6 +18,7 @@ import com.google.gson.Gson; ...@@ -18,6 +18,7 @@ import com.google.gson.Gson;
import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.WebResource;
import javax.ws.rs.core.MediaType;
public class TestUtils { public class TestUtils {
...@@ -77,19 +78,22 @@ public class TestUtils { ...@@ -77,19 +78,22 @@ public class TestUtils {
return headers; return headers;
} }
public ClientResponse send(String path, String httpMethod, String token, String requestBody, String query, Map<String,String> headers) throws Exception { public ClientResponse send(String path, String httpMethod, String token, String requestBody,
String query, Map<String, String> headers) throws Exception {
Client client = getClient(); Client client = getClient();
// client.setConnectTimeout(5000); WebResource webResource = client.resource(getApiPath(path + query));
// client.setReadTimeout(30000);
WebResource webResource = client.resource(getApiPath(path + query));
final WebResource.Builder builder = webResource.accept("application/json").type("application/json")
.header("Authorization", token);
headers.forEach((k, v) -> builder.header(k, v));
ClientResponse response = builder.method(httpMethod, ClientResponse.class, requestBody);
return response; final WebResource.Builder builder = webResource.getRequestBuilder();
} builder.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).
header("Authorization", token);
headers.forEach(builder::header);
ClientResponse response = builder.method(httpMethod, ClientResponse.class, requestBody);
return response;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getResult(ClientResponse response, int exepectedStatus, Class<T> classOfT) { public <T> T getResult(ClientResponse response, int exepectedStatus, Class<T> classOfT) {
......
package org.opengroup.osdu.legal.util; package org.opengroup.osdu.legal.util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import com.google.api.client.util.Strings; import com.google.api.client.util.Strings;
import com.google.auth.oauth2.GoogleCredentials; import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.BlobId; import com.google.cloud.storage.BlobId;
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2017-2019, Schlumberger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.opengroup.osdu.legal</groupId>
<artifactId>os-legal-testing</artifactId>
<version>0.0.2-SNAPSHOT</version>
<description>Root Legal Service project</description>
<packaging>pom</packaging>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<modules>
<module>legal-test-core</module>
<module>legal-test-aws</module>
<module>legal-test-azure</module>
<module>legal-test-gcp</module>
<module>legal-test-ibm</module>
</modules>
<repositories>
<repository>
<id>${gitlab-server}</id>
<url>https://community.opengroup.org/api/v4/groups/17/-/packages/maven</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>${gitlab-server}</id>
<url>https://community.opengroup.org/api/v4/projects/74/packages/maven</url>
</repository>
<snapshotRepository>
<id>${gitlab-server}</id>
<url>https://community.opengroup.org/api/v4/projects/74/packages/maven</url>
</snapshotRepository>
</distributionManagement>
</project>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment