Skip to content
Snippets Groups Projects
Commit 9d0ca57c authored by Riabokon Stanislav(EPAM)[GCP]'s avatar Riabokon Stanislav(EPAM)[GCP]
Browse files

Added GlobalOtherExceptionMapper.

Changed an anotation 'ControllerAdvice' to 'RestControllerAdvice' for GlobalOtherExceptionMapper
and GlobalExceptionMapper.
Fixed int tests.
Updated GCP README.md
parent 9b94e0cc
No related branches found
No related tags found
1 merge request!68Integration tests for Google Cloud Run (GONRG-1016)
......@@ -15,6 +15,7 @@
package org.opengroup.osdu.legal.middleware;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.validation.ConstraintViolation;
......@@ -27,6 +28,7 @@ import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.google.gson.Gson;
import javassist.NotFoundException;
import javax.ws.rs.core.MediaType;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
......@@ -36,9 +38,11 @@ import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
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.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
......@@ -49,8 +53,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
@RestController
@RestControllerAdvice
public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
private static final Gson gson = new Gson();
......@@ -74,7 +77,7 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
return this.getErrorResponse(
new AppException(HttpStatus.BAD_REQUEST.value(), "Bad JSON format", e.getMessage()));
}
@ExceptionHandler(UnrecognizedPropertyException.class)
protected ResponseEntity<Object> handleValidationException(UnrecognizedPropertyException e) {
return this.getErrorResponse(
......@@ -119,18 +122,31 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
"An unknown error has occurred."));
}
@Override
@NonNull
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(@NonNull HttpRequestMethodNotSupportedException e,
@NonNull HttpHeaders headers,
@NonNull HttpStatus status,
@NonNull WebRequest request) {
return this.getErrorResponse(
@NonNull WebRequest request) {
return this.getErrorResponse(
new AppException(HttpStatus.METHOD_NOT_ALLOWED.value(), "Method not found.",
"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();
......@@ -140,6 +156,6 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
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.ControllerAdvice;
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.
| `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 | - |
| `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**
......
......@@ -5,11 +5,14 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.opengroup.osdu.legal.util.Constants.DATA_PARTITION_ID;
import com.sun.jersey.core.header.MediaTypes;
import java.util.HashMap;
import java.util.Map;
import com.sun.jersey.api.client.ClientResponse;
import java.util.Objects;
import javax.ws.rs.core.MediaType;
import org.junit.Test;
public abstract class AcceptanceBaseTest {
......@@ -84,17 +87,23 @@ public abstract class AcceptanceBaseTest {
return response;
}
protected ClientResponse validateAccess(int expectedResponse) throws Exception {
Map<String, String> headers = new HashMap<>();
headers.put(DATA_PARTITION_ID, LegalTagUtils.getMyDataPartition());
ClientResponse response = legalTagUtils.send(this.getApi(), this.getHttpMethod(), legalTagUtils.accessToken(), getBody(), getQuery(), headers);
assertEquals(expectedResponse, response.getStatus());
if(expectedResponse == 204)
assertNull(response.getType());
else if(response.getType() != null) {
assertTrue(response.getType().toString().toLowerCase().indexOf("application/json") >= 0);
}
return response;
}
protected ClientResponse validateAccess(int expectedResponse) throws Exception {
Map<String, String> headers = new HashMap<>();
headers.put(DATA_PARTITION_ID, LegalTagUtils.getMyDataPartition());
ClientResponse response = legalTagUtils
.send(this.getApi(), this.getHttpMethod(), legalTagUtils.accessToken(), getBody(),
getQuery(), headers);
assertEquals(expectedResponse, response.getStatus());
if (expectedResponse == 204) {
if (Objects.nonNull(response.getType())) {
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;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import javax.ws.rs.core.MediaType;
public class TestUtils {
......@@ -77,19 +78,24 @@ public class TestUtils {
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.setConnectTimeout(5000);
// 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);
Client client = getClient();
// client.setConnectTimeout(5000);
// client.setReadTimeout(30000);
WebResource webResource = client.resource(getApiPath(path + query));
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")
public <T> T getResult(ClientResponse response, int exepectedStatus, Class<T> classOfT) {
......
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