Commit c534d46c authored by Riabokon Stanislav(EPAM)[GCP]'s avatar Riabokon Stanislav(EPAM)[GCP]
Browse files

Merge branch 'gcp-fix-schema-uniqueness-check' into 'master'

Gcp fix schema uniqueness check

See merge request !116
parents 1d860ade bd79176d
Pipeline #46722 passed with stages
in 37 minutes and 9 seconds
package org.opengroup.osdu.schema.exception;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class AppExceptionHandler {
@ExceptionHandler(AppException.class)
public ResponseEntity<Object> handleAppExceptions(AppException e) {
return this.getErrorResponse(e);
}
private ResponseEntity<Object> getErrorResponse(AppException e) {
String exceptionMsg = Objects.nonNull(e.getOriginalException())
? e.getOriginalException().getMessage()
: e.getError().getMessage();
Integer errorCode = e.getError().getCode();
if (errorCode > 499) {
log.error(exceptionMsg, e.getOriginalException());
} else {
log.warn(exceptionMsg, e.getOriginalException());
}
return new ResponseEntity<>(e.getError(), HttpStatus.resolve(errorCode));
}
}
......@@ -17,6 +17,21 @@
package org.opengroup.osdu.schema.impl.schemainfostore;
import com.google.cloud.Timestamp;
import com.google.cloud.datastore.Blob;
import com.google.cloud.datastore.BlobValue;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreException;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.EntityQuery;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;
import com.google.cloud.datastore.StructuredQuery.CompositeFilter;
import com.google.cloud.datastore.StructuredQuery.Filter;
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
import com.google.gson.Gson;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
......@@ -25,8 +40,9 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
......@@ -46,22 +62,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import com.google.cloud.Timestamp;
import com.google.cloud.datastore.Blob;
import com.google.cloud.datastore.BlobValue;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreException;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.EntityQuery;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;
import com.google.cloud.datastore.StructuredQuery.CompositeFilter;
import com.google.cloud.datastore.StructuredQuery.Filter;
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
import com.google.gson.Gson;
/**
* Repository class to to register Schema in Google store.
*
......@@ -340,9 +340,15 @@ public class GoogleSchemaInfoStore implements ISchemaInfoStore {
Query<Key> query = Query.newKeyQueryBuilder().setNamespace(SchemaConstants.NAMESPACE)
.setKind(SchemaConstants.SCHEMA_KIND).setFilter(PropertyFilter.eq("__key__", schemaKey)).build();
QueryResults<Key> keys = datastore.run(query);
if (keys.hasNext())
return false;
try {
QueryResults<Key> keys = datastore.run(query);
if (keys.hasNext()) {
return false;
}
} catch (DatastoreException e) {
throw new AppException(HttpStatus.SC_BAD_REQUEST, "Schema uniqueness check failed",
String.format("Misconfigured tenant-info for %s, not possible to check schema uniqueness", tenant));
}
}
return true;
}
......
......@@ -14,10 +14,12 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.gcp.multitenancy.DatastoreFactory;
......@@ -478,6 +480,26 @@ public class GoogleSchemaInfoStoreTest {
assertEquals(false, schemaInfoStore.cleanSchema(schemaId));
}
@Test
public void testMisconfiguredTenantInfoShouldThrowException() throws ApplicationException {
Key storageKey = mock(Key.class);
KeyFactory storageKeyFactory = mock(KeyFactory.class);
String schemaId = "schemaId";
String tenantId = "common";
Mockito.when(tenantFactory.getTenantInfo("common")).thenReturn(tenantInfoCommon);
Mockito.when(tenantInfoCommon.getName()).thenReturn("common");
Mockito.when(dataStoreFactory.getDatastore(tenantInfoCommon)).thenReturn(dataStore);
Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory);
Mockito.when(keyFactory.setKind(SchemaConstants.SCHEMA_KIND)).thenReturn(keyFactory);
Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory);
when(keyFactory.newKey(schemaId)).thenReturn(key);
when(storageKeyFactory.newKey(schemaId)).thenReturn(storageKey);
when(dataStore.run(ArgumentMatchers.any())).thenThrow(new DatastoreException(401,"",""));
expectedException.expect(AppException.class);
expectedException.expectMessage("Misconfigured tenant-info for common, not possible to check schema uniqueness");
schemaInfoStore.isUnique(schemaId, tenantId);
}
private SchemaRequest getMockSchemaObject_Published() {
return SchemaRequest.builder().schema("{}")
.schemaInfo(SchemaInfo.builder()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment