From 6ee872d1435ef496faf265daa4856b21e88e4be6 Mon Sep 17 00:00:00 2001 From: BhushanRade Date: Mon, 8 Jun 2020 20:48:45 +0530 Subject: [PATCH 1/8] IBM provider project created --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 42345eb7..fa359424 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ schema-core provider/schema-gcp + provider/schema-ibm -- GitLab From 315de3bf3881f23b14e4982aebe90ca6b87b7afe Mon Sep 17 00:00:00 2001 From: BhushanRade Date: Mon, 8 Jun 2020 20:49:18 +0530 Subject: [PATCH 2/8] IBM provider project created --- provider/schema-ibm/docker/Dockerfile | 9 + provider/schema-ibm/docker/docker-compose.yml | 11 + provider/schema-ibm/pom.xml | 159 +++++ .../schema/credentials/CloudCredentials.java | 193 ++++++ .../schema/credentials/DatastoreFactory.java | 49 ++ .../schema/credentials/StorageFactory.java | 47 ++ .../schemainfostore/IbmAuthorityStore.java | 109 +++ .../schemainfostore/IbmEntityTypeStore.java | 110 +++ .../schemainfostore/IbmSchemaInfoStore.java | 373 ++++++++++ .../impl/schemainfostore/IbmSourceStore.java | 108 +++ .../impl/schemastore/IbmSchemaStore.java | 96 +++ .../resources/application-kuber.properties | 1 + .../src/main/resources/application.properties | 5 + .../IbmAuthorityStoreTest.java | 165 +++++ .../IbmEntityTypeStoreTest.java | 166 +++++ .../IbmSchemaInfoStoreTest.java | 654 ++++++++++++++++++ .../schemainfostore/IbmSourceStoreTest.java | 162 +++++ .../impl/schemastore/IbmSchemaStoreTest.java | 115 +++ .../src/test/resources/application.properties | 0 .../org.mockito.plugins.MockMaker | 1 + 20 files changed, 2533 insertions(+) create mode 100644 provider/schema-ibm/docker/Dockerfile create mode 100644 provider/schema-ibm/docker/docker-compose.yml create mode 100644 provider/schema-ibm/pom.xml create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/CloudCredentials.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/DatastoreFactory.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/StorageFactory.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStore.java create mode 100644 provider/schema-ibm/src/main/resources/application-kuber.properties create mode 100644 provider/schema-ibm/src/main/resources/application.properties create mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java create mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java create mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java create mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java create mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStoreTest.java create mode 100644 provider/schema-ibm/src/test/resources/application.properties create mode 100644 provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/provider/schema-ibm/docker/Dockerfile b/provider/schema-ibm/docker/Dockerfile new file mode 100644 index 00000000..7858c8bf --- /dev/null +++ b/provider/schema-ibm/docker/Dockerfile @@ -0,0 +1,9 @@ +FROM openjdk:8-slim +RUN apt-get update && apt-get install -y curl + +ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","-Dspring.profiles.active=kuber","/app.jar"] +EXPOSE 8080 + +ARG JAR_FILE +COPY ${JAR_FILE} app.jar + diff --git a/provider/schema-ibm/docker/docker-compose.yml b/provider/schema-ibm/docker/docker-compose.yml new file mode 100644 index 00000000..4b54e053 --- /dev/null +++ b/provider/schema-ibm/docker/docker-compose.yml @@ -0,0 +1,11 @@ +version: "3" +services: + os-schema-app: + build: + args: + JAR_FILE: target/os-schema-gcp-0.0.1-spring-boot.jar + context: .. + dockerfile: docker/Dockerfile + image: gcr.io/opendes/os-schema-app + ports: + - "8080:8080" diff --git a/provider/schema-ibm/pom.xml b/provider/schema-ibm/pom.xml new file mode 100644 index 00000000..c34f928f --- /dev/null +++ b/provider/schema-ibm/pom.xml @@ -0,0 +1,159 @@ + + 4.0.0 + + org.opengroup.osdu + os-schema + 0.0.1 + ../../ + + os-schema-ibm + IBM cloud related implementation staff. + jar + + + + + + + org.opengroup.osdu + os-schema-core + 0.0.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.mockito + mockito-all + 1.10.19 + test + + + + + + + + + org.opengroup.osdu + os-core-lib-ibm + 0.0.13-SNAPSHOT + + + + io.cucumber + cucumber-java8 + 5.4.0 + test + + + + io.cucumber + cucumber-junit + 5.4.0 + test + + + + io.cucumber + cucumber-guice + 5.4.0 + test + + + + com.google.inject + guice + 4.2.0 + test + + + + + + + ${gitlab-server} + https://community.opengroup.org/api/v4/groups/17/-/packages/maven + + + + + + ${gitlab-server} + https://community.opengroup.org/api/v4/projects/26/packages/maven + + + ${gitlab-server} + https://community.opengroup.org/api/v4/projects/26/packages/maven + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + spring-boot + + org.opengroup.osdu.schema.SchemaApplication + + + + + + + + maven-war-plugin + + false + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M3 + + ${skipUnitTests} + 3 + true + -Xmx1024m -XX:MaxPermSize=256m + + + + + + + + \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/CloudCredentials.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/CloudCredentials.java new file mode 100644 index 00000000..86880f79 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/CloudCredentials.java @@ -0,0 +1,193 @@ +package org.opengroup.osdu.schema.credentials; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateUtils; +import org.apache.http.HttpHeaders; +import org.opengroup.osdu.core.common.http.HttpClient; +import org.opengroup.osdu.core.common.http.HttpRequest; +import org.opengroup.osdu.core.common.http.HttpResponse; +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.exceptions.ApplicationException; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.iam.v1.Iam; +import com.google.api.services.iam.v1.Iam.Projects; +import com.google.api.services.iam.v1.Iam.Projects.ServiceAccounts; +import com.google.api.services.iam.v1.Iam.Projects.ServiceAccounts.SignJwt; +import com.google.api.services.iam.v1.IamScopes; +import com.google.api.services.iam.v1.model.SignJwtRequest; +import com.google.api.services.iam.v1.model.SignJwtResponse; +import com.google.auth.oauth2.AccessToken; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.gson.JsonObject; + +import lombok.extern.java.Log; + +/** + * This class is used to implement the domain wide delegation using the service + * account token creator role. This class extends GoogleCredentials, and creates + * access token by creating the JWT token without private keys, and signing it + * with Google OAuth2 service. + * + */ + +@Log +public class CloudCredentials extends GoogleCredentials { + + private static final long serialVersionUID = -8461791038757192780L; + + private static final String JWT_AUDIENCE = "https://www.googleapis.com/oauth2/v4/token"; + private static final String SERVICE_ACCOUNT_NAME_FORMAT = "projects/%s/serviceAccounts/%s"; + private static final String ACCESS_TOKEN = "access_token"; + private static final String EXPIRES_IN = "expires_in"; + private static final String SCOPE = "https://www.googleapis.com/auth/devstorage.full_control https://www.googleapis.com/auth/datastore"; + + private static final JsonFactory JSON_FACTORY = new JacksonFactory(); + + private final TenantInfo tenant; + private Iam iam; + + public CloudCredentials(TenantInfo tenant) throws ApplicationException { + + if ((tenant == null) || StringUtils.isBlank(tenant.getName()) || StringUtils.isBlank(tenant.getProjectId()) + || StringUtils.isBlank(tenant.getServiceAccount())) { + throw new ApplicationException("Tenant name, project id or service account is not set."); + } + this.tenant = tenant; + } + + /** + * Overrides the GoogleCredentials refreshAccessToken. Returns JWT token for the + * service account of the tenant. + * + * @throws IOException + */ + @Override + public AccessToken refreshAccessToken() throws IOException { + + try { + + Map signJwtPayload = this.getPayload(); + String signedJwt = getSignedJwt(signJwtPayload); + return getAccessToken(signedJwt); + + } catch (ApplicationException e) { + log.log(Level.SEVERE, "Error : An unexpected error occurred while geting refresh access token : ", e); + throw new IOException("An error occurred when accessing third-party APIs"); + } + + } + + /** + * Creates the payload for JWT signing + * + * @return payload as map + */ + private Map getPayload() { + + Map payload = new HashMap<>(); + + payload.put("scope", SCOPE); + // This will get current time in seconds and add 1 hours (i.e 3600 seconds to + // it) + payload.put("exp", System.currentTimeMillis() / 1000 + 3600); + payload.put("iat", System.currentTimeMillis() / 1000); + payload.put("iss", this.tenant.getServiceAccount()); + payload.put("aud", JWT_AUDIENCE); + + return payload; + } + + /** + * Signs the JWT token of the tenant service account, without private keys. + * + * @param signJwtPayload + * @return the signed JWT + * @throws ApplicationException + */ + private String getSignedJwt(Map signJwtPayload) throws ApplicationException { + try { + SignJwtRequest signJwtRequest = new SignJwtRequest(); + signJwtRequest.setPayload(JSON_FACTORY.toString(signJwtPayload)); + + String serviceAccountName = String.format(SERVICE_ACCOUNT_NAME_FORMAT, this.tenant.getProjectId(), + this.tenant.getServiceAccount()); + + Iam iamInstance = this.getIam(); + Projects projects = iamInstance.projects(); + ServiceAccounts serviceAccounts = projects.serviceAccounts(); + SignJwt signJwt = serviceAccounts.signJwt(serviceAccountName, signJwtRequest); + + SignJwtResponse signJwtResponse = signJwt.execute(); + return signJwtResponse.getSignedJwt(); + + } catch (IOException | GeneralSecurityException e) { + log.log(Level.SEVERE, "Error occoured: ", e); + throw new ApplicationException("An error occurred when accessing third-party APIs"); + } + + } + + /** + * Creates the access token from the signed JWT for the tenant service account + * by signing it with Google OAuth2 service. + * + * @param signedJwt the signed JWT + * @return Access token that is used by Google services such as GCS, PubSub, + * Datastore at the time of the access + * @throws GeneralSecurityException + * @throws ApplicationException + */ + private AccessToken getAccessToken(String signedJwt) throws ApplicationException { + + HttpRequest request = HttpRequest.post().url(JWT_AUDIENCE) + .headers(Collections.singletonMap(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded")) + .body(String.format("%s=%s&%s=%s", "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer", + "assertion", signedJwt)) + .build(); + + HttpResponse response = new HttpClient().send(request); + JsonObject jsonResult = response.getAsJsonObject(); + + if (!jsonResult.has(ACCESS_TOKEN)) { + throw new ApplicationException("An error occurred when accessing third-party APIs"); + } + + return new AccessToken(jsonResult.get(ACCESS_TOKEN).getAsString(), + DateUtils.addSeconds(new Date(), jsonResult.get(EXPIRES_IN).getAsInt())); + + } + + /** + * Gets the Iam object of the services project, which is further used for + * signing the JWT + * + * @return the Iam object of the services project + * @throws IOException + * @throws GeneralSecurityException + */ + private Iam getIam() throws GeneralSecurityException, IOException { + if (this.iam == null) { + + Iam.Builder builder = new Iam.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, + GoogleCredential.getApplicationDefault().createScoped(Arrays.asList(IamScopes.CLOUD_PLATFORM))) + .setApplicationName(SchemaConstants.APPLICATION_NAME); + + this.iam = builder.build(); + } + return this.iam; + } +} \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/DatastoreFactory.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/DatastoreFactory.java new file mode 100644 index 00000000..f9c873d7 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/DatastoreFactory.java @@ -0,0 +1,49 @@ +package org.opengroup.osdu.schema.credentials; + +import java.util.concurrent.TimeUnit; + +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.common.util.Crc32c; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.springframework.stereotype.Component; +import org.threeten.bp.Duration; + +import com.google.api.gax.retrying.RetrySettings; +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreOptions; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; + +@Component +public class DatastoreFactory { + + private Cache cache = CacheBuilder.newBuilder() + .expireAfterAccess(60 * 60, TimeUnit.SECONDS).maximumSize(100).build(); + + // The numbers used for settings are selected on random basis, We can update + // them with experience/issue faced + private static final RetrySettings RETRY_SETTINGS = RetrySettings.newBuilder().setMaxAttempts(6) + .setInitialRetryDelay(Duration.ofSeconds(1)).setMaxRetryDelay(Duration.ofSeconds(10)) + .setRetryDelayMultiplier(2.0).setTotalTimeout(Duration.ofSeconds(50)) + .setInitialRpcTimeout(Duration.ofSeconds(50)).setRpcTimeoutMultiplier(1.1) + .setMaxRpcTimeout(Duration.ofSeconds(50)).build(); + + public Datastore getDatastore(TenantInfo tenantInfo) throws ApplicationException { + + String cacheKey = this.getCredentialsCacheKey(tenantInfo.getName()); + CloudCredentials credential = this.cache.getIfPresent(cacheKey); + + if (credential == null) { + credential = new CloudCredentials(tenantInfo); + this.cache.put(cacheKey, credential); + } + + return DatastoreOptions.newBuilder().setRetrySettings(RETRY_SETTINGS).setCredentials(credential) + .setProjectId(tenantInfo.getProjectId()).build().getService(); + } + + private String getCredentialsCacheKey(String tenantName) { + return Crc32c.hashToBase64EncodedString(String.format("CloudCredential:%s", tenantName)); + } + +} \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/StorageFactory.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/StorageFactory.java new file mode 100644 index 00000000..1c817e43 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/StorageFactory.java @@ -0,0 +1,47 @@ +package org.opengroup.osdu.schema.credentials; + +import java.util.concurrent.TimeUnit; + +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.springframework.stereotype.Component; +import org.threeten.bp.Duration; + +import com.google.api.gax.retrying.RetrySettings; +import com.google.cloud.TransportOptions; +import com.google.cloud.http.HttpTransportOptions; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; + +@Component +public class StorageFactory { + + // The numbers used for settings are selected on random basis, We can update + // them with experience/issue faced + private static final RetrySettings RETRY_SETTINGS = RetrySettings.newBuilder().setMaxAttempts(6) + .setInitialRetryDelay(Duration.ofSeconds(1)).setMaxRetryDelay(Duration.ofSeconds(10)) + .setRetryDelayMultiplier(2.0).setTotalTimeout(Duration.ofSeconds(50)) + .setInitialRpcTimeout(Duration.ofSeconds(50)).setRpcTimeoutMultiplier(1.1) + .setMaxRpcTimeout(Duration.ofSeconds(50)).build(); + + // The numbers used for settings are selected on random basis, We can update + // them with experience/issue faced + private static final TransportOptions TRANSPORT_OPTIONS = HttpTransportOptions.newBuilder() + .setReadTimeout(40 * 1000).setConnectTimeout(10 * 1000).build(); + + private Cache cache = CacheBuilder.newBuilder() + .expireAfterAccess(60 * 60, TimeUnit.SECONDS).maximumSize(100).build(); + + public Storage getStorage(TenantInfo tenantInfo) throws ApplicationException { + CloudCredentials credential = this.cache.getIfPresent(tenantInfo.getName()); + if (credential == null) { + credential = new CloudCredentials(tenantInfo); + this.cache.put(tenantInfo.getName(), credential); + } + + return StorageOptions.newBuilder().setCredentials(credential).setProjectId(tenantInfo.getProjectId()) + .setRetrySettings(RETRY_SETTINGS).setTransportOptions(TRANSPORT_OPTIONS).build().getService(); + } +} \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java new file mode 100644 index 00000000..4c92cc3e --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java @@ -0,0 +1,109 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import java.text.MessageFormat; + +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.credentials.DatastoreFactory; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.model.Authority; +import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.IAuthorityStore; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import com.google.cloud.Timestamp; +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreException; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; + +import lombok.extern.java.Log; + +/** + * Repository class to to register authority in Google store. + * + */ + +@Log +@Repository +public class IbmAuthorityStore implements IAuthorityStore { + + @Autowired + private DpsHeaders headers; + + @Autowired + private DatastoreFactory dataStoreFactory; + + @Autowired + private ITenantFactory tenantFactory; + + /** + * Method to get authority from google store + * + * @param authorityId + * @return Authority object + * @throws ApplicationException + * @throws NotFoundException + */ + @Override + public Authority get(String authorityId) throws NotFoundException, ApplicationException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.AUTHORITY_KIND).newKey(authorityId); + + Entity entity = datastore.get(key); + if (entity == null) { + throw new NotFoundException("bad input parameter"); + } else { + return mapEntityToDto(entity); + } + + } + + /** + * Method to create authority in google store of dataPartitionId project + * + * @param authority + * @param dataPartitionId + * @return Authority object + * @throws ApplicationException + * @throws BadRequestException + */ + @Override + public Authority create(Authority authority) throws ApplicationException, BadRequestException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.AUTHORITY_KIND).newKey(authority.getAuthorityId()); + Entity entity = getEntityObject(authority, key); + try { + datastore.add(entity); + } catch (DatastoreException ex) { + if ("ALREADY_EXISTS".equals(ex.getReason())) { + log.warning(SchemaConstants.AUTHORITY_EXISTS_ALREADY_REGISTERED); + throw new BadRequestException( + MessageFormat.format(SchemaConstants.AUTHORITY_EXISTS_EXCEPTION, authority.getAuthorityId())); + } else { + log.severe(MessageFormat.format(SchemaConstants.OBJECT_INVALID, ex.getMessage())); + throw new ApplicationException(SchemaConstants.INVALID_INPUT); + } + } + log.info(SchemaConstants.AUTHORITY_CREATED); + return mapEntityToDto(entity); + } + + private Authority mapEntityToDto(Entity entity) { + + Authority authority = new Authority(); + authority.setAuthorityId(entity.getKey().getName()); + return authority; + + } + + private Entity getEntityObject(Authority authority, Key key) { + return Entity.newBuilder(key).set(SchemaConstants.DATE_CREATED, Timestamp.now()).build(); + } + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java new file mode 100644 index 00000000..be6f4c66 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java @@ -0,0 +1,110 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import java.text.MessageFormat; + +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.credentials.DatastoreFactory; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.model.EntityType; +import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.IEntityTypeStore; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import com.google.cloud.Timestamp; +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreException; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; + +import lombok.extern.java.Log; + +/** + * Repository class to register Entity type in Google store. + * + * + */ +@Log +@Repository +public class IbmEntityTypeStore implements IEntityTypeStore { + + @Autowired + private DpsHeaders headers; + + @Autowired + private DatastoreFactory dataStoreFactory; + + @Autowired + private ITenantFactory tenantFactory; + + /** + * Method to get entity type from google store + * + * @param entityTypeId + * @return EntityType object + * @throws ApplicationException + * @throws NotFoundException + */ + @Override + public EntityType get(String entityTypeId) throws NotFoundException, ApplicationException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.ENTITYTYPE_KIND).newKey(entityTypeId); + Entity entity = datastore.get(key); + if (entity == null) { + throw new NotFoundException("bad input parameter"); + } else { + return mapEntityToDto(entity); + } + + } + + /** + * Method to create entityType in google store of dataPartitionId GCP + * + * @param entityType + * @param dataPartitionId + * @return entityType object + * @throws ApplicationException + * @throws BadRequestException + */ + @Override + public EntityType create(EntityType entityType) throws BadRequestException, ApplicationException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.ENTITYTYPE_KIND).newKey(entityType.getEntityTypeId()); + Entity entity = getEntityObject(entityType, key); + try { + datastore.add(entity); + } catch (DatastoreException ex) { + if ("ALREADY_EXISTS".equals(ex.getReason())) { + log.warning(SchemaConstants.ENTITY_TYPE_EXISTS); + throw new BadRequestException( + MessageFormat.format(SchemaConstants.ENTITY_TYPE_EXISTS_EXCEPTION, entityType.getEntityTypeId())); + } else { + log.severe(MessageFormat.format(SchemaConstants.OBJECT_INVALID, ex.getMessage())); + throw new ApplicationException("Invalid input, object invalid"); + } + } + log.info(SchemaConstants.ENTITY_TYPE_CREATED); + return mapEntityToDto(entity); + } + + private EntityType mapEntityToDto(Entity entity) { + + EntityType entityType = new EntityType(); + entityType.setEntityTypeId(entity.getKey().getName()); + return entityType; + + } + + private Entity getEntityObject(EntityType entityType, Key key) { + Entity.Builder entityBuilder = Entity.newBuilder(key); + entityBuilder.set(SchemaConstants.DATE_CREATED, Timestamp.now()); + return entityBuilder.build(); + } + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java new file mode 100644 index 00000000..4cef4c22 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java @@ -0,0 +1,373 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeMap; +import java.util.stream.Collectors; + +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; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.credentials.DatastoreFactory; +import org.opengroup.osdu.schema.enums.SchemaScope; +import org.opengroup.osdu.schema.enums.SchemaStatus; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.model.QueryParams; +import org.opengroup.osdu.schema.model.SchemaIdentity; +import org.opengroup.osdu.schema.model.SchemaInfo; +import org.opengroup.osdu.schema.model.SchemaRequest; +import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.ISchemaInfoStore; +import org.opengroup.osdu.schema.util.VersionHierarchyUtil; +import org.springframework.beans.factory.annotation.Autowired; +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; +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 lombok.extern.java.Log; + +/** + * Repository class to to register Schema in Google store. + * + */ +@Log +@Repository +public class IbmSchemaInfoStore implements ISchemaInfoStore { + + @Autowired + private DpsHeaders headers; + + @Autowired + private DatastoreFactory dataStoreFactory; + + @Autowired + private ITenantFactory tenantFactory; + + /** + * Method to get schemaInfo from google store + * + * @param schemaId + * @return schemaInfo object + * @throws ApplicationException + * @throws NotFoundException + */ + @Override + public SchemaInfo getSchemaInfo(String schemaId) throws ApplicationException, NotFoundException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE).setKind(SchemaConstants.SCHEMA_KIND) + .newKey(schemaId); + Entity entity = datastore.get(key); + if (entity != null) { + return getSchemaInfoObject(entity, datastore); + } + throw new NotFoundException(SchemaConstants.SCHEMA_NOT_PRESENT); + + } + + /** + * Method to Create schema in google store of tenantId GCP + * + * @param schema + * @param tenantId + * @return schemaInfo object + * @throws ApplicationException + * @throws BadRequestException + */ + @Override + public SchemaInfo createSchemaInfo(SchemaRequest schema) throws ApplicationException, BadRequestException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + KeyFactory keyFactory = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.SCHEMA_KIND); + Entity entity = getEntityObject(schema, datastore, keyFactory); + try { + datastore.add(entity); + } catch (DatastoreException ex) { + if (SchemaConstants.ALREADY_EXISTS.equals(ex.getReason())) { + log.warning(SchemaConstants.SCHEMA_EXISTS); + throw new BadRequestException(SchemaConstants.SCHEMA_EXISTS + " with Id: " + + schema.getSchemaInfo().getSchemaIdentity().getId()); + } else { + log.severe(SchemaConstants.OBJECT_INVALID); + throw new ApplicationException(SchemaConstants.SCHEMA_CREATION_FAILED_INVALID_OBJECT); + } + } + log.info(SchemaConstants.SCHEMA_CREATED); + return getSchemaInfoObject(entity, datastore); + } + + /** + * Method to update schema in google store of tenantId GCP + * + * @param schema + * @param tenantId + * @return schemaInfo object + * @throws ApplicationException + * @throws BadRequestException + */ + @Override + public SchemaInfo updateSchemaInfo(SchemaRequest schema) throws ApplicationException, BadRequestException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + KeyFactory keyFactory = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.SCHEMA_KIND); + Entity entity = getEntityObject(schema, datastore, keyFactory); + try { + datastore.put(entity); + } catch (DatastoreException ex) { + log.severe(SchemaConstants.OBJECT_INVALID); + throw new ApplicationException("Invalid object, updation failed"); + } + return getSchemaInfoObject(entity, datastore); + } + + /** + * Method to clean schemaInfo in google datastore of tenantId GCP + * + * @param schemaId + * @return status + * @throws ApplicationException + */ + @Override + public boolean cleanSchema(String schemaId) throws ApplicationException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + KeyFactory keyFactory = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.SCHEMA_KIND); + Key key = keyFactory.newKey(schemaId); + try { + datastore.delete(key); + return true; + } catch (DatastoreException ex) { + return false; + } + } + + @Override + public String getLatestMinorVerSchema(SchemaInfo schemaInfo) throws ApplicationException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + Query query = Query.newEntityQueryBuilder().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.SCHEMA_KIND) + .setFilter(CompositeFilter.and( + PropertyFilter.eq(SchemaConstants.AUTHORITY, schemaInfo.getSchemaIdentity().getAuthority()), + PropertyFilter.eq(SchemaConstants.ENTITY_TYPE, schemaInfo.getSchemaIdentity().getEntityType()), + PropertyFilter.eq(SchemaConstants.MAJOR_VERSION, + schemaInfo.getSchemaIdentity().getSchemaVersionMajor()), + PropertyFilter.eq(SchemaConstants.SOURCE, schemaInfo.getSchemaIdentity().getSource()))) + .build(); + + QueryResults result = datastore.run(query); + TreeMap sortedMap = new TreeMap<>(Collections.reverseOrder()); + while (result.hasNext()) { + Entity entity = result.next(); + sortedMap.put(entity.getLong(SchemaConstants.MINOR_VERSION), entity.getBlob(SchemaConstants.SCHEMA)); + } + if (sortedMap.size() != 0) { + Entry blob = sortedMap.firstEntry(); + return new String(blob.getValue().toByteArray()); + } + return new String(); + } + + private SchemaInfo getSchemaInfoObject(Entity entity, Datastore datastore) { + + SchemaIdentity superSededBy = null; + if (entity.contains(SchemaConstants.SUPERSEDED_BY)) { + KeyFactory keyFactory = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE).setKind(SchemaConstants.SCHEMA_KIND); + Entity superSededEntity = datastore.get(keyFactory.newKey(entity.getString(SchemaConstants.SUPERSEDED_BY))); + superSededBy = getSchemaIdentity(superSededEntity); + } + + return SchemaInfo.builder().createdBy(entity.getString(SchemaConstants.CREATED_BY)) + .dateCreated(entity.getTimestamp(SchemaConstants.DATE_CREATED).toDate()) + .scope(SchemaScope.valueOf(entity.getString(SchemaConstants.SCOPE))) + .status(SchemaStatus.valueOf(entity.getString(SchemaConstants.STATUS))) + .schemaIdentity(getSchemaIdentity(entity)).supersededBy(superSededBy).build(); + + } + + private Entity getEntityObject(SchemaRequest schema, Datastore datastore, KeyFactory keyFactory) + throws BadRequestException, ApplicationException { + + Key key = keyFactory.newKey(schema.getSchemaInfo().getSchemaIdentity().getId()); + + Entity.Builder entityBuilder = Entity.newBuilder(key); + if (schema.getSchemaInfo().getSupersededBy() != null) { + if (schema.getSchemaInfo().getSupersededBy().getId() == null + || datastore.get(keyFactory.newKey(schema.getSchemaInfo().getSupersededBy().getId())) == null) { + log.severe(SchemaConstants.INVALID_SUPERSEDEDBY_ID); + throw new BadRequestException(SchemaConstants.INVALID_SUPERSEDEDBY_ID); + } + entityBuilder.set(SchemaConstants.SUPERSEDED_BY, schema.getSchemaInfo().getSupersededBy().getId()); + } + + entityBuilder.set(SchemaConstants.DATE_CREATED, Timestamp.now()); + entityBuilder.set(SchemaConstants.CREATED_BY, headers.getUserEmail()); + entityBuilder.set(SchemaConstants.AUTHORITY, schema.getSchemaInfo().getSchemaIdentity().getAuthority()); + entityBuilder.set(SchemaConstants.SOURCE, schema.getSchemaInfo().getSchemaIdentity().getSource()); + entityBuilder.set(SchemaConstants.ENTITY_TYPE, schema.getSchemaInfo().getSchemaIdentity().getEntityType()); + entityBuilder.set(SchemaConstants.MAJOR_VERSION, + schema.getSchemaInfo().getSchemaIdentity().getSchemaVersionMajor()); + entityBuilder.set(SchemaConstants.MINOR_VERSION, + schema.getSchemaInfo().getSchemaIdentity().getSchemaVersionMinor()); + entityBuilder.set(SchemaConstants.PATCH_VERSION, + schema.getSchemaInfo().getSchemaIdentity().getSchemaVersionPatch()); + entityBuilder.set(SchemaConstants.SCOPE, schema.getSchemaInfo().getScope().name()); + entityBuilder.set(SchemaConstants.STATUS, schema.getSchemaInfo().getStatus().name()); + Gson gson = new Gson(); + Blob schemaBlob = Blob.copyFrom(gson.toJson(schema.getSchema()).getBytes()); + entityBuilder.set(SchemaConstants.SCHEMA, BlobValue.newBuilder(schemaBlob).setExcludeFromIndexes(true).build()); + + return entityBuilder.build(); + } + + private SchemaIdentity getSchemaIdentity(Entity entity) { + + return SchemaIdentity.builder().id(entity.getKey().getName()) + .authority(entity.getString(SchemaConstants.AUTHORITY)).source(entity.getString(SchemaConstants.SOURCE)) + .entityType(entity.getString(SchemaConstants.ENTITY_TYPE)) + .schemaVersionMajor(entity.getLong(SchemaConstants.MAJOR_VERSION)) + .schemaVersionMinor(entity.getLong(SchemaConstants.MINOR_VERSION)) + .schemaVersionPatch(entity.getLong(SchemaConstants.PATCH_VERSION)).build(); + + } + + @Override + public List getSchemaInfoList(QueryParams queryParams, String tenantId) throws ApplicationException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(tenantId)); + List filterList = getFilters(queryParams); + + EntityQuery.Builder queryBuilder = Query.newEntityQueryBuilder().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.SCHEMA_KIND); + + if (!filterList.isEmpty()) { + queryBuilder.setFilter( + CompositeFilter.and(filterList.get(0), filterList.toArray(new Filter[filterList.size()]))); + } + + QueryResults result = datastore.run(queryBuilder.build()); + List schemaList = new LinkedList<>(); + while (result.hasNext()) { + Entity entity = result.next(); + schemaList.add(getSchemaInfoObject(entity, datastore)); + } + + if (queryParams.getLatestVersion() != null && queryParams.getLatestVersion()) { + return getLatestVersionSchemaList(schemaList); + } + + return schemaList; + } + + private List getLatestVersionSchemaList(List filteredSchemaList) { + List latestSchemaList = new LinkedList<>(); + SchemaInfo previousSchemaInfo = null; + TreeMap sortedMap = new TreeMap<>( + new VersionHierarchyUtil.SortingVersionComparator()); + + for (SchemaInfo schemaInfoObject : filteredSchemaList) { + if ((previousSchemaInfo != null) && !(checkAuthorityMatch(previousSchemaInfo, schemaInfoObject) + && checkSourceMatch(previousSchemaInfo, schemaInfoObject) + && checkEntityMatch(previousSchemaInfo, schemaInfoObject))) { + Entry latestVersionEntry = sortedMap.firstEntry(); + latestSchemaList.add(latestVersionEntry.getValue()); + sortedMap.clear(); + } + previousSchemaInfo = schemaInfoObject; + SchemaIdentity schemaIdentity = schemaInfoObject.getSchemaIdentity(); + VersionHierarchyUtil version = new VersionHierarchyUtil(schemaIdentity.getSchemaVersionMajor(), + schemaIdentity.getSchemaVersionMinor(), schemaIdentity.getSchemaVersionPatch()); + sortedMap.put(version, schemaInfoObject); + } + if (sortedMap.size() != 0) { + Entry latestVersionEntry = sortedMap.firstEntry(); + latestSchemaList.add(latestVersionEntry.getValue()); + } + + return latestSchemaList; + } + + private boolean checkEntityMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) { + return schemaInfoObject.getSchemaIdentity().getEntityType() + .equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getEntityType()); + } + + private boolean checkSourceMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) { + return schemaInfoObject.getSchemaIdentity().getSource() + .equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getSource()); + } + + private boolean checkAuthorityMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) { + return schemaInfoObject.getSchemaIdentity().getAuthority() + .equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getAuthority()); + } + + private List getFilters(QueryParams queryParams) { + List filterList = new LinkedList(); + if (queryParams.getAuthority() != null) { + filterList.add(PropertyFilter.eq(SchemaConstants.AUTHORITY, queryParams.getAuthority())); + } + if (queryParams.getSource() != null) { + filterList.add(PropertyFilter.eq(SchemaConstants.SOURCE, queryParams.getSource())); + } + if (queryParams.getEntityType() != null) { + filterList.add(PropertyFilter.eq(SchemaConstants.ENTITY_TYPE, queryParams.getEntityType())); + } + if (queryParams.getSchemaVersionMajor() != null) { + filterList.add(PropertyFilter.eq(SchemaConstants.MAJOR_VERSION, queryParams.getSchemaVersionMajor())); + } + if (queryParams.getSchemaVersionMinor() != null) { + filterList.add(PropertyFilter.eq(SchemaConstants.MINOR_VERSION, queryParams.getSchemaVersionMinor())); + } + if (queryParams.getStatus() != null) { + filterList.add(PropertyFilter.eq(SchemaConstants.STATUS, queryParams.getStatus().toUpperCase())); + } + return filterList; + } + + @Override + public boolean isUnique(String schemaId, String tenantId) throws ApplicationException { + + Set tenantList = new HashSet<>(); + tenantList.add(SchemaConstants.ACCOUNT_ID_COMMON_PROJECT); + tenantList.add(tenantId); + + // code to call check uniqueness + if (tenantId.equalsIgnoreCase(SchemaConstants.ACCOUNT_ID_COMMON_PROJECT)) { + List privateTenantList = tenantFactory.listTenantInfo().stream().map(TenantInfo::getDataPartitionId) + .collect(Collectors.toList()); + tenantList.addAll(privateTenantList); + + } + for (String tenant : tenantList) { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(tenant)); + Key schemaKey = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.SCHEMA_KIND).newKey(schemaId); + + Query query = Query.newKeyQueryBuilder().setNamespace(SchemaConstants.NAMESPACE) + .setKind(SchemaConstants.SCHEMA_KIND).setFilter(PropertyFilter.eq("__key__", schemaKey)).build(); + QueryResults keys = datastore.run(query); + if (keys.hasNext()) + return false; + } + return true; + } +} \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java new file mode 100644 index 00000000..afeb9fb7 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java @@ -0,0 +1,108 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import java.text.MessageFormat; + +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.credentials.DatastoreFactory; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.model.Source; +import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.ISourceStore; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import com.google.cloud.Timestamp; +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreException; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; + +import lombok.extern.java.Log; + +/** + * Repository class to to register Source in Google store. + * + * + */ +@Log +@Repository +public class IbmSourceStore implements ISourceStore { + + @Autowired + private DpsHeaders headers; + + @Autowired + private DatastoreFactory dataStoreFactory; + + @Autowired + private ITenantFactory tenantFactory; + + /** + * Method to create Source in google store + * + * @param sourceId + * @return Source object + * @throws ApplicationException + */ + @Override + public Source get(String sourceId) throws NotFoundException, ApplicationException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE).setKind(SchemaConstants.SOURCE_KIND) + .newKey(sourceId); + Entity entity = datastore.get(key); + if (entity == null) { + throw new NotFoundException("bad input parameter"); + } else { + return mapEntityToDto(entity); + } + + } + + /** + * Method to create Source in google store of dataPartitionId GCP + * + * @param Source + * @param dataPartitionId + * @return Source object + */ + @Override + public Source create(Source source) throws BadRequestException, ApplicationException { + Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); + Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE).setKind(SchemaConstants.SOURCE_KIND) + .newKey(source.getSourceId()); + Entity entity = getEntityObject(source, key); + + try { + datastore.add(entity); + } catch (DatastoreException ex) { + if ("ALREADY_EXISTS".equals(ex.getReason())) { + log.warning(SchemaConstants.SOURCE_EXISTS); + throw new BadRequestException( + MessageFormat.format(SchemaConstants.SOURCE_EXISTS_EXCEPTION, source.getSourceId())); + } else { + log.severe(SchemaConstants.OBJECT_INVALID); + throw new ApplicationException(SchemaConstants.INVALID_INPUT); + } + } + log.info(SchemaConstants.SOURCE_CREATED); + return mapEntityToDto(entity); + } + + private Source mapEntityToDto(Entity entity) { + + Source source = new Source(); + source.setSourceId(entity.getKey().getName()); + return source; + + } + + private Entity getEntityObject(Source source, Key key) { + Entity.Builder entityBuilder = Entity.newBuilder(key); + entityBuilder.set(SchemaConstants.DATE_CREATED, Timestamp.now()); + return entityBuilder.build(); + } + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStore.java new file mode 100644 index 00000000..03aeb0a9 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStore.java @@ -0,0 +1,96 @@ +package org.opengroup.osdu.schema.impl.schemastore; + +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.credentials.StorageFactory; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.provider.interfaces.schemastore.ISchemaStore; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.BlobId; +import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageException; + +/** + * Repository class to to register resolved Schema in Google storage. + * + * + */ +@Repository +public class IbmSchemaStore implements ISchemaStore { + + @Autowired + private DpsHeaders headers; + + @Autowired + private StorageFactory storageFactory; + + @Autowired + TenantFactory tenantFactory; + + /** + * Method to get schema from google Storage given Tenant ProjectInfo + * + * @param dataPartitionId + * @param filePath + * @throws NotFoundException + * @return schema object + * @throws ApplicationException + * @throws NotFoundException + */ + @Override + public String getSchema(String dataPartitionId, String filePath) throws ApplicationException, NotFoundException { + filePath = filePath + SchemaConstants.JSON_EXTENSION; + String bucketname = getSchemaBucketName(dataPartitionId); + Storage storage = storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId)); + Blob blob = storage.get(bucketname, filePath); + if (blob != null) + return new String(blob.getContent()); + throw new NotFoundException(SchemaConstants.SCHEMA_NOT_PRESENT); + } + + /** + * Method to write schema to google Storage given Tenant ProjectInfo + * + * @param dataPartitionId + * @param filePath + * @param content + * @return schema object + * @throws ApplicationException + */ + + @Override + public String createSchema(String filePath, String content) throws ApplicationException { + String dataPartitionId = headers.getPartitionId(); + filePath = filePath + SchemaConstants.JSON_EXTENSION; + String bucketname = getSchemaBucketName(dataPartitionId); + BlobId blobId = BlobId.of(bucketname, filePath); + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); + Storage storage = storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId)); + try { + Blob blob = storage.create(blobInfo, content.getBytes()); + return blob.getName(); + } catch (StorageException ex) { + throw new ApplicationException(SchemaConstants.INTERNAL_SERVER_ERROR); + } + } + + private String getSchemaBucketName(String dataPartitionId) { + return tenantFactory.getTenantInfo(dataPartitionId).getProjectId() + SchemaConstants.SCHEMA_BUCKET_EXTENSION; + } + + @Override + public boolean cleanSchemaProject(String schemaId) throws ApplicationException { + String dataPartitionId = headers.getPartitionId(); + String fileName = schemaId + SchemaConstants.JSON_EXTENSION; + String bucketname = getSchemaBucketName(dataPartitionId); + BlobId blobId = BlobId.of(bucketname, fileName); + return storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId)).delete(blobId); + } + +} diff --git a/provider/schema-ibm/src/main/resources/application-kuber.properties b/provider/schema-ibm/src/main/resources/application-kuber.properties new file mode 100644 index 00000000..6f30385e --- /dev/null +++ b/provider/schema-ibm/src/main/resources/application-kuber.properties @@ -0,0 +1 @@ +AUTHORIZE_API=http://os-entitlement-service/entitlements/v1 diff --git a/provider/schema-ibm/src/main/resources/application.properties b/provider/schema-ibm/src/main/resources/application.properties new file mode 100644 index 00000000..e331ce85 --- /dev/null +++ b/provider/schema-ibm/src/main/resources/application.properties @@ -0,0 +1,5 @@ +server.servlet.context-path=/api/schema-service/v1 +logging.level.org.springframework.web=DEBUG +server.port=8088 +AUTHORIZE_API=http://localhost:8080s/entitlements/v1 +AUTHORIZE_API_KEY= \ No newline at end of file diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java new file mode 100644 index 00000000..32fc3ac4 --- /dev/null +++ b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java @@ -0,0 +1,165 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.credentials.DatastoreFactory; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.model.Authority; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.google.cloud.Timestamp; +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreException; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; +import com.google.cloud.datastore.KeyFactory; + +@RunWith(SpringJUnit4ClassRunner.class) +public class IbmAuthorityStoreTest { + + @InjectMocks + IbmAuthorityStore mockGoogleAuthorityStore; + + @Mock + KeyFactory keyFactory; + + @Mock + Entity mockEntity; + + @Mock + Datastore dataStore; + + @Mock + DatastoreFactory dataStoreFactory; + + @Mock + Authority mockAuthority; + + @Mock + Key key; + + @Mock + DpsHeaders headers; + + @Mock + TenantFactory tenantFactory; + + @Mock + TenantInfo tenantInfo; + + @Test + public void testGetAuthority() throws NotFoundException, ApplicationException { + String authorityId = "testAuthorityId"; + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.AUTHORITY_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey(authorityId)).thenReturn(key); + Mockito.when(dataStore.get(key)).thenReturn(mockEntity); + Mockito.when(mockEntity.getKey()).thenReturn(key); + assertNotNull(mockGoogleAuthorityStore.get(authorityId)); + } + + @Test + public void testGetAuthority_NotFoundException() { + String authorityId = ""; + try { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.AUTHORITY_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey(authorityId)).thenReturn(key); + Mockito.when(dataStore.get(key)).thenReturn(null); + mockGoogleAuthorityStore.get(authorityId); + fail("Should not succeed"); + } catch (NotFoundException e) { + assertEquals("bad input parameter", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreateAuthority() throws NotFoundException, ApplicationException, BadRequestException { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.AUTHORITY_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey("os")).thenReturn(key); + Mockito.when(dataStore.get(key)).thenReturn(mockEntity); + Entity entity = Entity.newBuilder(key).set(SchemaConstants.DATE_CREATED, Timestamp.now()).build(); + Mockito.when(dataStore.add(entity)).thenReturn(entity); + Mockito.when(mockAuthority.getAuthorityId()).thenReturn("os"); + assertNotNull(mockGoogleAuthorityStore.create(mockAuthority)); + } + + @Test + public void testCreateAuthority_BadRequestException() + throws NotFoundException, ApplicationException, BadRequestException { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + mockGoogleAuthorityStore = Mockito.spy(mockGoogleAuthorityStore); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.AUTHORITY_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey("os")).thenReturn(key); + Mockito.when(dataStore.add(Mockito.any(Entity.class))) + .thenThrow(new DatastoreException(400, SchemaConstants.ALREADY_EXISTS, SchemaConstants.ALREADY_EXISTS)); + Mockito.when(mockAuthority.getAuthorityId()).thenReturn("os"); + try { + mockGoogleAuthorityStore.create(mockAuthority); + fail("Should not succeed"); + } catch (BadRequestException e) { + assertEquals("Authority already registered with Id: os", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreateAuthority_ApplicationException() + throws NotFoundException, ApplicationException, BadRequestException { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + mockGoogleAuthorityStore = Mockito.spy(mockGoogleAuthorityStore); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.AUTHORITY_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey("os")).thenReturn(key); + Mockito.when(dataStore.add(Mockito.any(Entity.class))).thenThrow(DatastoreException.class); + Mockito.when(mockAuthority.getAuthorityId()).thenReturn("os"); + try { + mockGoogleAuthorityStore.create(mockAuthority); + fail("Should not succeed"); + } catch (ApplicationException e) { + assertEquals(SchemaConstants.INVALID_INPUT, e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + +} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java new file mode 100644 index 00000000..7cb434c8 --- /dev/null +++ b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java @@ -0,0 +1,166 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.credentials.DatastoreFactory; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.model.EntityType; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.google.cloud.Timestamp; +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreException; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; +import com.google.cloud.datastore.KeyFactory; + +@RunWith(SpringJUnit4ClassRunner.class) +public class IbmEntityTypeStoreTest { + + @InjectMocks + IbmEntityTypeStore googleEntityStore; + + @Mock + KeyFactory keyFactory; + + @Mock + Entity entity; + + @Mock + Datastore dataStore; + + @Mock + EntityType mockEntityType; + + @Mock + Key key; + + @Mock + DpsHeaders headers; + + @Mock + DatastoreFactory dataStoreFactory; + + @Mock + TenantInfo tenantInfo; + + @Mock + TenantFactory tenantFactory; + + @Test + public void testGet() throws NotFoundException, ApplicationException { + System.out.println("testGet"); + String entityId = "testEntityId"; + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.ENTITY_TYPE)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey(entityId)).thenReturn(key); + Mockito.when(dataStore.get(key)).thenReturn(entity); + Mockito.when(entity.getKey()).thenReturn(key); + assertNotNull(googleEntityStore.get(entityId)); + } + + @Test + public void testGet_NotFoundException() { + System.out.println("testGet_NotFoundException"); + String entityId = ""; + try { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.ENTITY_TYPE)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey(entityId)).thenReturn(key); + Mockito.when(dataStore.get(key)).thenReturn(null); + googleEntityStore.get(entityId); + fail("Should not succeed"); + } catch (NotFoundException e) { + assertEquals("bad input parameter", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreate() throws NotFoundException, ApplicationException, BadRequestException { + System.out.println("testCreate"); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.ENTITY_TYPE)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey("wellbore")).thenReturn(key); + Entity entity = Entity.newBuilder(key).set(SchemaConstants.DATE_CREATED, Timestamp.now()).build(); + Mockito.when(dataStore.add(entity)).thenReturn(entity); + Mockito.when(mockEntityType.getEntityTypeId()).thenReturn("wellbore"); + assertNotNull(googleEntityStore.create(mockEntityType)); + } + + @Test + public void testCreate_BadRequestException() throws NotFoundException, ApplicationException, BadRequestException { + System.out.println("testCreate_BadRequestException"); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(mockEntityType.getEntityTypeId()).thenReturn("wks"); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.ENTITYTYPE_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey("wks")).thenReturn(key); + Mockito.when(dataStore.add(Mockito.any(Entity.class))) + .thenThrow(new DatastoreException(400, SchemaConstants.ALREADY_EXISTS, SchemaConstants.ALREADY_EXISTS)); + + try { + googleEntityStore.create(mockEntityType); + fail("Should not succeed"); + } catch (BadRequestException e) { + assertEquals("EntityType already registered with Id: wks", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreate_ApplicationException() throws NotFoundException, ApplicationException, BadRequestException { + System.out.println("testCreate_ApplicationException"); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.ENTITYTYPE_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey("wks")).thenReturn(key); + Mockito.when(dataStore.add(Mockito.any(Entity.class))).thenThrow(DatastoreException.class); + Mockito.when(mockEntityType.getEntityTypeId()).thenReturn("wks"); + try { + googleEntityStore.create(mockEntityType); + fail("Should not succeed"); + } catch (ApplicationException e) { + assertEquals("Invalid input, object invalid", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + +} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java new file mode 100644 index 00000000..c706a41c --- /dev/null +++ b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java @@ -0,0 +1,654 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.credentials.DatastoreFactory; +import org.opengroup.osdu.schema.enums.SchemaScope; +import org.opengroup.osdu.schema.enums.SchemaStatus; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.model.QueryParams; +import org.opengroup.osdu.schema.model.SchemaIdentity; +import org.opengroup.osdu.schema.model.SchemaInfo; +import org.opengroup.osdu.schema.model.SchemaRequest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.google.cloud.Timestamp; +import com.google.cloud.datastore.Blob; +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreException; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; +import com.google.cloud.datastore.KeyFactory; +import com.google.cloud.datastore.ProjectionEntity; +import com.google.cloud.datastore.Query; +import com.google.cloud.datastore.QueryResults; +import com.google.cloud.datastore.TimestampValue; + +@RunWith(SpringJUnit4ClassRunner.class) +public class IbmSchemaInfoStoreTest { + + @InjectMocks + IbmSchemaInfoStore schemaInfoStore; + + @Mock + KeyFactory keyFactory; + + @Mock + ProjectionEntity projectionEntity; + + @Mock + Entity entity; + + @Mock + Datastore dataStore; + + @Mock + SchemaRequest schemaRequest; + + @Mock + Key key; + + @Mock + Blob blob; + + @Mock + Query query; + + @Mock + QueryResults queryResult; + + @Mock + DatastoreFactory dataStoreFactory; + + @Mock + DpsHeaders headers; + + @Mock + Query keyQuery; + + @Mock + QueryResults keys; + + @Mock + TenantInfo tenantInfo; + + @Mock + TenantInfo tenantInfoCommon; + + @Mock + TenantFactory tenantFactory; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void testGetLatestMinorVersion_ReturnNull() throws NotFoundException, ApplicationException { + when(headers.getPartitionId()).thenReturn("tenant"); + Mockito.when(tenantFactory.getTenantInfo("tenant")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); + Mockito.when(entity.getBlob(SchemaConstants.SCHEMA)).thenReturn(blob); + Mockito.when(blob.toByteArray()).thenReturn("{}".getBytes()); + assertEquals("", schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo())); + } + + @Test + public void testGetSchemaInfo_NotEmptyEntity() throws NotFoundException, ApplicationException { + String schemaId = "schemaId"; + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(keyFactory.newKey(schemaId)).thenReturn(key); + Mockito.when(dataStore.get(key)).thenReturn(getMockEntityObject()); + SchemaInfo schemaInfo = schemaInfoStore.getSchemaInfo(schemaId); + assertEquals(SchemaStatus.PUBLISHED, schemaInfo.getStatus()); + } + + @Test + public void testGetSchemaInfo_EmptyEntity() throws NotFoundException, ApplicationException { + + expectedException.expect(NotFoundException.class); + expectedException.expectMessage(SchemaConstants.SCHEMA_NOT_PRESENT); + String schemaId = "schemaId"; + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(keyFactory.newKey(schemaId)).thenReturn(key); + Mockito.when(dataStore.get(key)).thenReturn(null); + schemaInfoStore.getSchemaInfo(schemaId); + } + + @Test + public void testCreateSchemaInfo_Positive() throws ApplicationException, BadRequestException { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); + Mockito.when(headers.getUserEmail()).thenReturn("hmadhani@delfi.com"); + Mockito.when(dataStore.add(entity)).thenReturn(entity); + assertNotNull(schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published())); + } + + @Test + public void testIsUnique_True() throws ApplicationException { + Key storageKey = mock(Key.class); + KeyFactory storageKeyFactory = mock(KeyFactory.class); + String schemaId = "schemaId"; + String tenantId = "tenant"; + Mockito.when(tenantFactory.getTenantInfo("tenant")).thenReturn(tenantInfo); + Mockito.when(tenantFactory.getTenantInfo("common")).thenReturn(tenantInfoCommon); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + 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); + Query any = Mockito.any(Query.class); + when(dataStore.run(any)).thenReturn(keys); + when(keys.hasNext()).thenReturn(false); + assertTrue(schemaInfoStore.isUnique(schemaId, tenantId)); + } + + @Test + public void testIsUnique_False() throws ApplicationException { + Key storageKey = mock(Key.class); + KeyFactory storageKeyFactory = mock(KeyFactory.class); + String schemaId = "schemaId"; + String tenantId = "tenant"; + Mockito.when(tenantFactory.getTenantInfo("tenant")).thenReturn(tenantInfo); + Mockito.when(tenantFactory.getTenantInfo("common")).thenReturn(tenantInfoCommon); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + 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); + Query any = Mockito.any(Query.class); + when(dataStore.run(any)).thenReturn(keys); + when(keys.hasNext()).thenReturn(true); + assertFalse(schemaInfoStore.isUnique(schemaId, tenantId)); + } + + @Test + public void testIsUnique_False_CommomTenant() throws ApplicationException { + Key storageKey = mock(Key.class); + KeyFactory storageKeyFactory = mock(KeyFactory.class); + String schemaId = "schemaId"; + String tenantId = "common"; + Mockito.when(tenantFactory.getTenantInfo("tenant")).thenReturn(tenantInfo); + Mockito.when(tenantFactory.getTenantInfo("common")).thenReturn(tenantInfoCommon); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + 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); + Query any = Mockito.any(Query.class); + when(dataStore.run(any)).thenReturn(keys); + when(keys.hasNext()).thenReturn(true); + assertFalse(schemaInfoStore.isUnique(schemaId, tenantId)); + } + + @Test + public void testUpdateSchemaInfo() throws NotFoundException, ApplicationException, BadRequestException { + String tenantId = "test"; + Mockito.when(headers.getPartitionId()).thenReturn(tenantId); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); + Mockito.when(headers.getUserEmail()).thenReturn("hmadhani@delfi.com"); + Mockito.when(dataStore.put(entity)).thenReturn(entity); + assertNotNull(schemaInfoStore.updateSchemaInfo(getMockSchemaObject_Published())); + } + + @Test + public void testCreateSchemaInfo_WithSupersededBy() + throws NotFoundException, ApplicationException, BadRequestException { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); + Key mockSupsededKey = Mockito.mock(Key.class); + Mockito.when(keyFactory.newKey("os:wks:well.1.2.1")).thenReturn(mockSupsededKey); + Mockito.when(headers.getUserEmail()).thenReturn("hmadhani@delfi.com"); + Mockito.when(dataStore.add(entity)).thenReturn(entity); + Mockito.when(dataStore.get(mockSupsededKey)).thenReturn(entity); + Mockito.when(entity.getKey()).thenReturn(mockSupsededKey); + assertNotNull(schemaInfoStore.createSchemaInfo(getMockSchemaObject_SuperSededBy())); + } + + @Test + public void testUpdateSchemaInfo_SupersededByException() + throws NotFoundException, ApplicationException, BadRequestException { + try { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); + Key mockSupsededKey = Mockito.mock(Key.class); + Mockito.when(keyFactory.newKey("os:wks:well.1.2.1")).thenReturn(mockSupsededKey); + Mockito.when(dataStore.add(entity)).thenReturn(entity); + Mockito.when(dataStore.get(mockSupsededKey)).thenReturn(null); + Mockito.when(entity.getKey()).thenReturn(mockSupsededKey); + schemaInfoStore.updateSchemaInfo(getMockSchemaObject_SuperSededBy()); + fail("Should not succeed"); + } catch (BadRequestException e) { + assertEquals("Invalid SuperSededBy id", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testUpdateInfo_SupersededByWithoutIdException() + throws NotFoundException, ApplicationException, BadRequestException { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); + SchemaRequest schemaRequest = getMockSchemaObject_SuperSededByWithoutId(); + expectedException.expect(BadRequestException.class); + expectedException.expectMessage(SchemaConstants.INVALID_SUPERSEDEDBY_ID); + schemaInfoStore.updateSchemaInfo(schemaRequest); + } + + @Test + public void testCreateSchemaInfo_BadRequestException() + throws NotFoundException, ApplicationException, BadRequestException { + + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(headers.getUserEmail()).thenReturn("dummy-user"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); + Mockito.when(dataStore.add(Mockito.any(Entity.class))) + .thenThrow(new DatastoreException(400, SchemaConstants.ALREADY_EXISTS, SchemaConstants.ALREADY_EXISTS)); + try { + schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published()); + fail("Should not succeed"); + } catch (BadRequestException e) { + assertEquals("Schema already registered with Id: os:wks:well.1.1.1", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreateSchemaInfo_ApplicationException() + throws NotFoundException, ApplicationException, BadRequestException { + + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(headers.getUserEmail()).thenReturn("dummy-user"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); + Mockito.when(dataStore.add(Mockito.any(Entity.class))).thenThrow(DatastoreException.class); + try { + schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published()); + fail("Should not succeed"); + } catch (ApplicationException e) { + assertEquals(SchemaConstants.SCHEMA_CREATION_FAILED_INVALID_OBJECT, e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testUpdateSchemaInfo_ApplicationException() + throws NotFoundException, ApplicationException, BadRequestException { + + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(headers.getUserEmail()).thenReturn("dummy-user"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); + Mockito.when(dataStore.put(Mockito.any(Entity.class))).thenThrow(DatastoreException.class); + try { + schemaInfoStore.updateSchemaInfo(getMockSchemaObject_Published()); + fail("Should not succeed"); + } catch (ApplicationException e) { + assertEquals("Invalid object, updation failed", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testGetLatestMinorVersion_Entity() throws NotFoundException, ApplicationException { + when(headers.getPartitionId()).thenReturn("tenant"); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(headers.getUserEmail()).thenReturn("hmadhani@delfi.com"); + entity = Entity.newBuilder(getMockEntityObject()).set(SchemaConstants.SCHEMA, blob).build(); + Mockito.when(dataStore.add(entity)).thenReturn(getMockEntityObject()); + Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.hasNext()).thenReturn(true, false); + Mockito.when(queryResult.next()).thenReturn(entity); + Mockito.when(blob.toByteArray()).thenReturn("{}".getBytes()); + assertEquals("{}", schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo())); + } + + @Test + public void testGetSchemaInfoList_withoutqueryparam() + throws NotFoundException, ApplicationException, BadRequestException { + + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.hasNext()).thenReturn(true, false); + Mockito.when(queryResult.next()).thenReturn(getMockEntityObject()); + assertEquals(1, + schemaInfoStore.getSchemaInfoList(QueryParams.builder().limit(100).offset(0).build(), "test").size()); + } + + @Test + public void testGetSchemaInfoList_withqueryparam() + throws NotFoundException, ApplicationException, BadRequestException { + + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.hasNext()).thenReturn(true, false); + Mockito.when(queryResult.next()).thenReturn(getMockEntityObject()); + assertEquals(1, + schemaInfoStore.getSchemaInfoList(QueryParams.builder().authority("test").source("test").entityType("test") + .schemaVersionMajor(1l).schemaVersionMinor(1l).scope("test").status("test").latestVersion(false) + .limit(100).offset(0).build(), "test").size()); + } + + @Test + public void testGetSchemaInfoList_latestVersionTrue_NoSchemaMatchScenario() + throws NotFoundException, ApplicationException, BadRequestException { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.hasNext()).thenReturn(false); + assertEquals(0, + schemaInfoStore + .getSchemaInfoList(QueryParams.builder().authority("test").source("test").entityType("test") + .scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), "test") + .size()); + } + + @Test + public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentMajorVersion() + throws NotFoundException, ApplicationException, BadRequestException { + + Entity entity = Entity.newBuilder(key).set("createdBy", "subham") + .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") + .set("status", "PUBLISHED").setKey(key).set("authority", "os1").set("source", "techlog1") + .set("entityType", "wellbore1").set("majorVersion", 1).set("minorVersion", 1024).set("patchVersion", 2) + .build(); + Entity latestEntity = getMockEntityObject(); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.hasNext()).thenReturn(true, true, false); + Mockito.when(queryResult.next()).thenReturn(entity, latestEntity); + assertEquals(1, schemaInfoStore.getSchemaInfoList( + QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), + "test").size()); + } + + @Test + public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentMinorVersion() + throws NotFoundException, ApplicationException, BadRequestException { + + Entity entity = Entity.newBuilder(key).set("createdBy", "subham") + .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") + .set("status", "PUBLISHED").setKey(key).set("authority", "os1").set("source", "techlog1") + .set("entityType", "wellbore1").set("majorVersion", 2).set("minorVersion", 102).set("patchVersion", 2) + .build(); + Entity latestEntity = getMockEntityObject(); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.hasNext()).thenReturn(true, true, false); + Mockito.when(queryResult.next()).thenReturn(entity, latestEntity); + assertEquals(1, schemaInfoStore.getSchemaInfoList( + QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), + "test").size()); + } + + @Test + public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentSource() + throws NotFoundException, ApplicationException, BadRequestException { + + Entity entity = Entity.newBuilder(key).set("createdBy", "subham") + .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") + .set("status", "PUBLISHED").setKey(key).set("authority", "os1").set("source", "differenttechlog1") + .set("entityType", "wellbore1").set("majorVersion", 2).set("minorVersion", 1023).set("patchVersion", 2) + .build(); + Entity latestEntity = getMockEntityObject(); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.hasNext()).thenReturn(true, true, false); + Mockito.when(queryResult.next()).thenReturn(entity, latestEntity); + assertEquals(2, schemaInfoStore.getSchemaInfoList( + QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), + "test").size()); + } + + @Test + public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentAuthority() + throws NotFoundException, ApplicationException, BadRequestException { + + Entity entity = Entity.newBuilder(key).set("createdBy", "subham") + .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") + .set("status", "PUBLISHED").setKey(key).set("authority", "differentos1").set("source", "techlog1") + .set("entityType", "wellbore1").set("majorVersion", 2).set("minorVersion", 1023).set("patchVersion", 2) + .build(); + Entity latestEntity = getMockEntityObject(); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.hasNext()).thenReturn(true, true, false); + Mockito.when(queryResult.next()).thenReturn(entity, latestEntity); + assertEquals(2, schemaInfoStore.getSchemaInfoList( + QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), + "test").size()); + } + + @Test + public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentEntity() + throws NotFoundException, ApplicationException, BadRequestException { + + Entity entity = Entity.newBuilder(key).set("createdBy", "subham") + .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") + .set("status", "PUBLISHED").setKey(key).set("authority", "os1").set("source", "techlog1") + .set("entityType", "differentwellbore1").set("majorVersion", 2).set("minorVersion", 1023) + .set("patchVersion", 2).build(); + Entity latestEntity = getMockEntityObject(); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); + Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.hasNext()).thenReturn(true, true, false); + Mockito.when(queryResult.next()).thenReturn(entity, latestEntity); + assertEquals(2, schemaInfoStore.getSchemaInfoList( + QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), + "test").size()); + } + + @Test + public void testCleanSchema_Success() throws ApplicationException { + String schemaId = "schemaId"; + String dataPartitionId = "tenant1"; + when(headers.getPartitionId()).thenReturn(dataPartitionId); + Mockito.when(dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId()))) + .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); + Key key = mock(Key.class); + Mockito.when(keyFactory.newKey(schemaId)).thenReturn(key); + assertEquals(true, schemaInfoStore.cleanSchema(schemaId)); + } + + @Test + public void testCleanSchema_Failure() throws ApplicationException { + String schemaId = "schemaId"; + String dataPartitionId = "tenant1"; + when(headers.getPartitionId()).thenReturn(dataPartitionId); + Mockito.when(dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId()))) + .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); + Key key = mock(Key.class); + Mockito.when(keyFactory.newKey(schemaId)).thenReturn(key); + doThrow(DatastoreException.class).when(dataStore).delete(key); + assertEquals(false, schemaInfoStore.cleanSchema(schemaId)); + } + + private SchemaRequest getMockSchemaObject_Published() { + return SchemaRequest.builder().schema("{}") + .schemaInfo(SchemaInfo.builder() + .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) + .id("os:wks:well.1.1.1").build()) + .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).createdBy("subham").build()) + .build(); + + } + + private SchemaInfo getMockSchemaInfo() { + return SchemaInfo.builder().schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L).id("os:wks:well.1.1.1").build()) + .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).createdBy("subham") + + .build(); + + } + + private SchemaRequest getMockSchemaObject_SuperSededBy() { + return SchemaRequest.builder().schema("{}") + .schemaInfo(SchemaInfo.builder() + .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) + .id("os:wks:well.1.1.1").build()) + .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).createdBy("subham") + .supersededBy(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(2L) + .id("os:wks:well.1.2.1").build()) + .build()) + .build(); + + } + + private SchemaRequest getMockSchemaObject_SuperSededByWithoutId() { + return SchemaRequest.builder().schema("{}") + .schemaInfo(SchemaInfo.builder() + .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) + .id("os:wks:well.1.1.1").build()) + .scope(SchemaScope.SHARED).status(SchemaStatus.DEVELOPMENT).createdBy("subham") + .supersededBy(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L).build()) + .build()) + .build(); + + } + + private Entity getMockEntityObject() { + return Entity.newBuilder(key).set("createdBy", "subham") + .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") + .set("status", "PUBLISHED").setKey(key).set("authority", "os1").set("source", "techlog1") + .set("entityType", "wellbore1").set("majorVersion", 2).set("minorVersion", 1024).set("patchVersion", 2) + .build(); + } + +} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java new file mode 100644 index 00000000..8699aa70 --- /dev/null +++ b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java @@ -0,0 +1,162 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.credentials.DatastoreFactory; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.model.Source; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.google.cloud.Timestamp; +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreException; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; +import com.google.cloud.datastore.KeyFactory; + +@RunWith(SpringJUnit4ClassRunner.class) +public class IbmSourceStoreTest { + + @InjectMocks + IbmSourceStore sourceStore; + + @Mock + KeyFactory keyFactory; + + @Mock + Entity entity; + + @Mock + Datastore dataStore; + + @Mock + Source mockSource; + + @Mock + Key key; + + @Mock + DpsHeaders headers; + + @Mock + DatastoreFactory dataStoreFactory; + + @Mock + TenantInfo tenantInfo; + + @Mock + TenantFactory tenantFactory; + + @Test + public void testGet() throws NotFoundException, ApplicationException { + String sourceId = "sourceId"; + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.SOURCE_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey(sourceId)).thenReturn(key); + Mockito.when(dataStore.get(key)).thenReturn(entity); + Mockito.when(entity.getKey()).thenReturn(key); + assertNotNull(sourceStore.get(sourceId)); + } + + @Test + public void testGet_NotFoundException() { + String sourceId = ""; + try { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.SOURCE_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey(sourceId)).thenReturn(key); + Mockito.when(dataStore.get(key)).thenReturn(null); + sourceStore.get(sourceId); + fail("Should not succeed"); + } catch (NotFoundException e) { + assertEquals("bad input parameter", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreate() throws NotFoundException, ApplicationException, BadRequestException { + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.SOURCE_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey("wks")).thenReturn(key); + Entity entity = Entity.newBuilder(key).set(SchemaConstants.DATE_CREATED, Timestamp.now()).build(); + Mockito.when(dataStore.add(entity)).thenReturn(entity); + Mockito.when(mockSource.getSourceId()).thenReturn("wks"); + assertNotNull(sourceStore.create(mockSource)); + } + + @Test + public void testCreate_BadRequestException() throws NotFoundException, ApplicationException, BadRequestException { + sourceStore = Mockito.spy(sourceStore); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.SOURCE_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey("wks")).thenReturn(key); + Mockito.when(dataStore.add(Mockito.any(Entity.class))) + .thenThrow(new DatastoreException(400, SchemaConstants.ALREADY_EXISTS, SchemaConstants.ALREADY_EXISTS)); + Mockito.when(mockSource.getSourceId()).thenReturn("wks"); + try { + sourceStore.create(mockSource); + fail("Should not succeed"); + } catch (BadRequestException e) { + assertEquals("Source already registered with Id: wks", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreate_ApplicationException() throws NotFoundException, ApplicationException, BadRequestException { + sourceStore = Mockito.spy(sourceStore); + Mockito.when(headers.getPartitionId()).thenReturn("test"); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); + Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); + Mockito.when(keyFactory.setKind(SchemaConstants.SOURCE_KIND)).thenReturn(keyFactory); + Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); + Mockito.when(keyFactory.newKey("wks")).thenReturn(key); + Mockito.when(dataStore.add(Mockito.any(Entity.class))).thenThrow(DatastoreException.class); + Mockito.when(mockSource.getSourceId()).thenReturn("wks"); + try { + sourceStore.create(mockSource); + fail("Should not succeed"); + } catch (ApplicationException e) { + assertEquals(SchemaConstants.INVALID_INPUT, e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + +} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStoreTest.java new file mode 100644 index 00000000..3931b360 --- /dev/null +++ b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStoreTest.java @@ -0,0 +1,115 @@ +package org.opengroup.osdu.schema.impl.schemastore; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.credentials.StorageFactory; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.BlobId; +import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageException; + +@RunWith(SpringJUnit4ClassRunner.class) +public class IbmSchemaStoreTest { + + @InjectMocks + IbmSchemaStore schemaStore; + + @Mock + private StorageFactory storageFactory; + + @Mock + Storage storage; + + @Mock + Blob blob; + + @Mock + DpsHeaders headers; + + @Mock + TenantFactory tenantFactory; + + @Mock + TenantInfo TenantInfo; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private static final String BUCKET = "test-schema"; + private static final String dataPartitionId = "dataPartitionId"; + private static final String FILE_PATH = "/test-folder/test-file"; + private static final String CONTENT = "Hello World"; + + @Test + public void testCreateSchema() throws ApplicationException { + + Mockito.when(headers.getPartitionId()).thenReturn(dataPartitionId); + String filepath = FILE_PATH + SchemaConstants.JSON_EXTENSION; + BlobId blobId = Blob.newBuilder(BUCKET, filepath).build().getBlobId(); + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); + Mockito.when(tenantFactory.getTenantInfo(dataPartitionId)).thenReturn(TenantInfo); + Mockito.when(TenantInfo.getProjectId()).thenReturn("test"); + Mockito.when(storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId))).thenReturn(storage); + Mockito.when(storage.create(blobInfo, CONTENT.getBytes())).thenReturn(blob); + Mockito.when(blob.getName()).thenReturn(BUCKET + filepath); + + String blobName = schemaStore.createSchema(FILE_PATH, CONTENT); + + Assert.assertEquals((BUCKET + filepath), blobName); + } + + @Test + public void testCreateSchema_Failure() throws ApplicationException { + expectedException.expect(ApplicationException.class); + expectedException.expectMessage(SchemaConstants.INTERNAL_SERVER_ERROR); + Mockito.when(headers.getPartitionId()).thenReturn(dataPartitionId); + String filepath = FILE_PATH + SchemaConstants.JSON_EXTENSION; + BlobId blobId = Blob.newBuilder(BUCKET, filepath).build().getBlobId(); + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); + Mockito.when(tenantFactory.getTenantInfo(dataPartitionId)).thenReturn(TenantInfo); + Mockito.when(TenantInfo.getProjectId()).thenReturn("test"); + Mockito.when(storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId))).thenReturn(storage); + Mockito.when(storage.create(blobInfo, CONTENT.getBytes())).thenThrow(StorageException.class); + schemaStore.createSchema(FILE_PATH, CONTENT); + } + + @Test + public void testGetSchema() throws ApplicationException, NotFoundException { + String filePath = FILE_PATH + SchemaConstants.JSON_EXTENSION; + Mockito.when(tenantFactory.getTenantInfo(dataPartitionId)).thenReturn(TenantInfo); + Mockito.when(TenantInfo.getProjectId()).thenReturn("test"); + Mockito.when(storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId))).thenReturn(storage); + Mockito.when(storage.get(BUCKET, filePath)).thenReturn(blob); + Mockito.when(blob.getContent()).thenReturn(CONTENT.getBytes()); + Assert.assertEquals(CONTENT, schemaStore.getSchema(dataPartitionId, FILE_PATH)); + } + + @Test + public void testGetSchema_NotFound() throws ApplicationException, NotFoundException { + + expectedException.expect(NotFoundException.class); + expectedException.expectMessage(SchemaConstants.SCHEMA_NOT_PRESENT); + String filePath = FILE_PATH + SchemaConstants.JSON_EXTENSION; + Mockito.when(tenantFactory.getTenantInfo(dataPartitionId)).thenReturn(TenantInfo); + Mockito.when(TenantInfo.getProjectId()).thenReturn("test"); + Mockito.when(storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId))).thenReturn(storage); + Mockito.when(storage.get(BUCKET, filePath)).thenReturn(null); + schemaStore.getSchema(dataPartitionId, FILE_PATH); + } + +} diff --git a/provider/schema-ibm/src/test/resources/application.properties b/provider/schema-ibm/src/test/resources/application.properties new file mode 100644 index 00000000..e69de29b diff --git a/provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 00000000..ca6ee9ce --- /dev/null +++ b/provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline \ No newline at end of file -- GitLab From ca43b9e4a5d948946181fcf8fa98e4fc43806d7c Mon Sep 17 00:00:00 2001 From: Anuj gupta Date: Mon, 22 Jun 2020 19:26:01 +0530 Subject: [PATCH 3/8] Revert "IBM provider project created from master" This reverts commit 315de3bf3881f23b14e4982aebe90ca6b87b7afe. --- provider/schema-ibm/docker/Dockerfile | 9 - provider/schema-ibm/docker/docker-compose.yml | 11 - provider/schema-ibm/pom.xml | 159 ----- .../schema/credentials/CloudCredentials.java | 193 ------ .../schema/credentials/DatastoreFactory.java | 49 -- .../schema/credentials/StorageFactory.java | 47 -- .../schemainfostore/IbmAuthorityStore.java | 109 --- .../schemainfostore/IbmEntityTypeStore.java | 110 --- .../schemainfostore/IbmSchemaInfoStore.java | 373 ---------- .../impl/schemainfostore/IbmSourceStore.java | 108 --- .../impl/schemastore/IbmSchemaStore.java | 96 --- .../resources/application-kuber.properties | 1 - .../src/main/resources/application.properties | 5 - .../IbmAuthorityStoreTest.java | 165 ----- .../IbmEntityTypeStoreTest.java | 166 ----- .../IbmSchemaInfoStoreTest.java | 654 ------------------ .../schemainfostore/IbmSourceStoreTest.java | 162 ----- .../impl/schemastore/IbmSchemaStoreTest.java | 115 --- .../src/test/resources/application.properties | 0 .../org.mockito.plugins.MockMaker | 1 - 20 files changed, 2533 deletions(-) delete mode 100644 provider/schema-ibm/docker/Dockerfile delete mode 100644 provider/schema-ibm/docker/docker-compose.yml delete mode 100644 provider/schema-ibm/pom.xml delete mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/CloudCredentials.java delete mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/DatastoreFactory.java delete mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/StorageFactory.java delete mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java delete mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java delete mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java delete mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java delete mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStore.java delete mode 100644 provider/schema-ibm/src/main/resources/application-kuber.properties delete mode 100644 provider/schema-ibm/src/main/resources/application.properties delete mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java delete mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java delete mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java delete mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java delete mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStoreTest.java delete mode 100644 provider/schema-ibm/src/test/resources/application.properties delete mode 100644 provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/provider/schema-ibm/docker/Dockerfile b/provider/schema-ibm/docker/Dockerfile deleted file mode 100644 index 7858c8bf..00000000 --- a/provider/schema-ibm/docker/Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -FROM openjdk:8-slim -RUN apt-get update && apt-get install -y curl - -ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","-Dspring.profiles.active=kuber","/app.jar"] -EXPOSE 8080 - -ARG JAR_FILE -COPY ${JAR_FILE} app.jar - diff --git a/provider/schema-ibm/docker/docker-compose.yml b/provider/schema-ibm/docker/docker-compose.yml deleted file mode 100644 index 4b54e053..00000000 --- a/provider/schema-ibm/docker/docker-compose.yml +++ /dev/null @@ -1,11 +0,0 @@ -version: "3" -services: - os-schema-app: - build: - args: - JAR_FILE: target/os-schema-gcp-0.0.1-spring-boot.jar - context: .. - dockerfile: docker/Dockerfile - image: gcr.io/opendes/os-schema-app - ports: - - "8080:8080" diff --git a/provider/schema-ibm/pom.xml b/provider/schema-ibm/pom.xml deleted file mode 100644 index c34f928f..00000000 --- a/provider/schema-ibm/pom.xml +++ /dev/null @@ -1,159 +0,0 @@ - - 4.0.0 - - org.opengroup.osdu - os-schema - 0.0.1 - ../../ - - os-schema-ibm - IBM cloud related implementation staff. - jar - - - - - - - org.opengroup.osdu - os-schema-core - 0.0.1 - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.mockito - mockito-all - 1.10.19 - test - - - - - - - - - org.opengroup.osdu - os-core-lib-ibm - 0.0.13-SNAPSHOT - - - - io.cucumber - cucumber-java8 - 5.4.0 - test - - - - io.cucumber - cucumber-junit - 5.4.0 - test - - - - io.cucumber - cucumber-guice - 5.4.0 - test - - - - com.google.inject - guice - 4.2.0 - test - - - - - - - ${gitlab-server} - https://community.opengroup.org/api/v4/groups/17/-/packages/maven - - - - - - ${gitlab-server} - https://community.opengroup.org/api/v4/projects/26/packages/maven - - - ${gitlab-server} - https://community.opengroup.org/api/v4/projects/26/packages/maven - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - spring-boot - - org.opengroup.osdu.schema.SchemaApplication - - - - - - - - maven-war-plugin - - false - - - - - org.apache.maven.plugins - maven-surefire-plugin - 3.0.0-M3 - - ${skipUnitTests} - 3 - true - -Xmx1024m -XX:MaxPermSize=256m - - - - - - - - \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/CloudCredentials.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/CloudCredentials.java deleted file mode 100644 index 86880f79..00000000 --- a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/CloudCredentials.java +++ /dev/null @@ -1,193 +0,0 @@ -package org.opengroup.osdu.schema.credentials; - -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; - -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.time.DateUtils; -import org.apache.http.HttpHeaders; -import org.opengroup.osdu.core.common.http.HttpClient; -import org.opengroup.osdu.core.common.http.HttpRequest; -import org.opengroup.osdu.core.common.http.HttpResponse; -import org.opengroup.osdu.core.common.model.tenant.TenantInfo; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.exceptions.ApplicationException; - -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; -import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson2.JacksonFactory; -import com.google.api.services.iam.v1.Iam; -import com.google.api.services.iam.v1.Iam.Projects; -import com.google.api.services.iam.v1.Iam.Projects.ServiceAccounts; -import com.google.api.services.iam.v1.Iam.Projects.ServiceAccounts.SignJwt; -import com.google.api.services.iam.v1.IamScopes; -import com.google.api.services.iam.v1.model.SignJwtRequest; -import com.google.api.services.iam.v1.model.SignJwtResponse; -import com.google.auth.oauth2.AccessToken; -import com.google.auth.oauth2.GoogleCredentials; -import com.google.gson.JsonObject; - -import lombok.extern.java.Log; - -/** - * This class is used to implement the domain wide delegation using the service - * account token creator role. This class extends GoogleCredentials, and creates - * access token by creating the JWT token without private keys, and signing it - * with Google OAuth2 service. - * - */ - -@Log -public class CloudCredentials extends GoogleCredentials { - - private static final long serialVersionUID = -8461791038757192780L; - - private static final String JWT_AUDIENCE = "https://www.googleapis.com/oauth2/v4/token"; - private static final String SERVICE_ACCOUNT_NAME_FORMAT = "projects/%s/serviceAccounts/%s"; - private static final String ACCESS_TOKEN = "access_token"; - private static final String EXPIRES_IN = "expires_in"; - private static final String SCOPE = "https://www.googleapis.com/auth/devstorage.full_control https://www.googleapis.com/auth/datastore"; - - private static final JsonFactory JSON_FACTORY = new JacksonFactory(); - - private final TenantInfo tenant; - private Iam iam; - - public CloudCredentials(TenantInfo tenant) throws ApplicationException { - - if ((tenant == null) || StringUtils.isBlank(tenant.getName()) || StringUtils.isBlank(tenant.getProjectId()) - || StringUtils.isBlank(tenant.getServiceAccount())) { - throw new ApplicationException("Tenant name, project id or service account is not set."); - } - this.tenant = tenant; - } - - /** - * Overrides the GoogleCredentials refreshAccessToken. Returns JWT token for the - * service account of the tenant. - * - * @throws IOException - */ - @Override - public AccessToken refreshAccessToken() throws IOException { - - try { - - Map signJwtPayload = this.getPayload(); - String signedJwt = getSignedJwt(signJwtPayload); - return getAccessToken(signedJwt); - - } catch (ApplicationException e) { - log.log(Level.SEVERE, "Error : An unexpected error occurred while geting refresh access token : ", e); - throw new IOException("An error occurred when accessing third-party APIs"); - } - - } - - /** - * Creates the payload for JWT signing - * - * @return payload as map - */ - private Map getPayload() { - - Map payload = new HashMap<>(); - - payload.put("scope", SCOPE); - // This will get current time in seconds and add 1 hours (i.e 3600 seconds to - // it) - payload.put("exp", System.currentTimeMillis() / 1000 + 3600); - payload.put("iat", System.currentTimeMillis() / 1000); - payload.put("iss", this.tenant.getServiceAccount()); - payload.put("aud", JWT_AUDIENCE); - - return payload; - } - - /** - * Signs the JWT token of the tenant service account, without private keys. - * - * @param signJwtPayload - * @return the signed JWT - * @throws ApplicationException - */ - private String getSignedJwt(Map signJwtPayload) throws ApplicationException { - try { - SignJwtRequest signJwtRequest = new SignJwtRequest(); - signJwtRequest.setPayload(JSON_FACTORY.toString(signJwtPayload)); - - String serviceAccountName = String.format(SERVICE_ACCOUNT_NAME_FORMAT, this.tenant.getProjectId(), - this.tenant.getServiceAccount()); - - Iam iamInstance = this.getIam(); - Projects projects = iamInstance.projects(); - ServiceAccounts serviceAccounts = projects.serviceAccounts(); - SignJwt signJwt = serviceAccounts.signJwt(serviceAccountName, signJwtRequest); - - SignJwtResponse signJwtResponse = signJwt.execute(); - return signJwtResponse.getSignedJwt(); - - } catch (IOException | GeneralSecurityException e) { - log.log(Level.SEVERE, "Error occoured: ", e); - throw new ApplicationException("An error occurred when accessing third-party APIs"); - } - - } - - /** - * Creates the access token from the signed JWT for the tenant service account - * by signing it with Google OAuth2 service. - * - * @param signedJwt the signed JWT - * @return Access token that is used by Google services such as GCS, PubSub, - * Datastore at the time of the access - * @throws GeneralSecurityException - * @throws ApplicationException - */ - private AccessToken getAccessToken(String signedJwt) throws ApplicationException { - - HttpRequest request = HttpRequest.post().url(JWT_AUDIENCE) - .headers(Collections.singletonMap(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded")) - .body(String.format("%s=%s&%s=%s", "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer", - "assertion", signedJwt)) - .build(); - - HttpResponse response = new HttpClient().send(request); - JsonObject jsonResult = response.getAsJsonObject(); - - if (!jsonResult.has(ACCESS_TOKEN)) { - throw new ApplicationException("An error occurred when accessing third-party APIs"); - } - - return new AccessToken(jsonResult.get(ACCESS_TOKEN).getAsString(), - DateUtils.addSeconds(new Date(), jsonResult.get(EXPIRES_IN).getAsInt())); - - } - - /** - * Gets the Iam object of the services project, which is further used for - * signing the JWT - * - * @return the Iam object of the services project - * @throws IOException - * @throws GeneralSecurityException - */ - private Iam getIam() throws GeneralSecurityException, IOException { - if (this.iam == null) { - - Iam.Builder builder = new Iam.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, - GoogleCredential.getApplicationDefault().createScoped(Arrays.asList(IamScopes.CLOUD_PLATFORM))) - .setApplicationName(SchemaConstants.APPLICATION_NAME); - - this.iam = builder.build(); - } - return this.iam; - } -} \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/DatastoreFactory.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/DatastoreFactory.java deleted file mode 100644 index f9c873d7..00000000 --- a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/DatastoreFactory.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.opengroup.osdu.schema.credentials; - -import java.util.concurrent.TimeUnit; - -import org.opengroup.osdu.core.common.model.tenant.TenantInfo; -import org.opengroup.osdu.core.common.util.Crc32c; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.springframework.stereotype.Component; -import org.threeten.bp.Duration; - -import com.google.api.gax.retrying.RetrySettings; -import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreOptions; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; - -@Component -public class DatastoreFactory { - - private Cache cache = CacheBuilder.newBuilder() - .expireAfterAccess(60 * 60, TimeUnit.SECONDS).maximumSize(100).build(); - - // The numbers used for settings are selected on random basis, We can update - // them with experience/issue faced - private static final RetrySettings RETRY_SETTINGS = RetrySettings.newBuilder().setMaxAttempts(6) - .setInitialRetryDelay(Duration.ofSeconds(1)).setMaxRetryDelay(Duration.ofSeconds(10)) - .setRetryDelayMultiplier(2.0).setTotalTimeout(Duration.ofSeconds(50)) - .setInitialRpcTimeout(Duration.ofSeconds(50)).setRpcTimeoutMultiplier(1.1) - .setMaxRpcTimeout(Duration.ofSeconds(50)).build(); - - public Datastore getDatastore(TenantInfo tenantInfo) throws ApplicationException { - - String cacheKey = this.getCredentialsCacheKey(tenantInfo.getName()); - CloudCredentials credential = this.cache.getIfPresent(cacheKey); - - if (credential == null) { - credential = new CloudCredentials(tenantInfo); - this.cache.put(cacheKey, credential); - } - - return DatastoreOptions.newBuilder().setRetrySettings(RETRY_SETTINGS).setCredentials(credential) - .setProjectId(tenantInfo.getProjectId()).build().getService(); - } - - private String getCredentialsCacheKey(String tenantName) { - return Crc32c.hashToBase64EncodedString(String.format("CloudCredential:%s", tenantName)); - } - -} \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/StorageFactory.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/StorageFactory.java deleted file mode 100644 index 1c817e43..00000000 --- a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/credentials/StorageFactory.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.opengroup.osdu.schema.credentials; - -import java.util.concurrent.TimeUnit; - -import org.opengroup.osdu.core.common.model.tenant.TenantInfo; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.springframework.stereotype.Component; -import org.threeten.bp.Duration; - -import com.google.api.gax.retrying.RetrySettings; -import com.google.cloud.TransportOptions; -import com.google.cloud.http.HttpTransportOptions; -import com.google.cloud.storage.Storage; -import com.google.cloud.storage.StorageOptions; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; - -@Component -public class StorageFactory { - - // The numbers used for settings are selected on random basis, We can update - // them with experience/issue faced - private static final RetrySettings RETRY_SETTINGS = RetrySettings.newBuilder().setMaxAttempts(6) - .setInitialRetryDelay(Duration.ofSeconds(1)).setMaxRetryDelay(Duration.ofSeconds(10)) - .setRetryDelayMultiplier(2.0).setTotalTimeout(Duration.ofSeconds(50)) - .setInitialRpcTimeout(Duration.ofSeconds(50)).setRpcTimeoutMultiplier(1.1) - .setMaxRpcTimeout(Duration.ofSeconds(50)).build(); - - // The numbers used for settings are selected on random basis, We can update - // them with experience/issue faced - private static final TransportOptions TRANSPORT_OPTIONS = HttpTransportOptions.newBuilder() - .setReadTimeout(40 * 1000).setConnectTimeout(10 * 1000).build(); - - private Cache cache = CacheBuilder.newBuilder() - .expireAfterAccess(60 * 60, TimeUnit.SECONDS).maximumSize(100).build(); - - public Storage getStorage(TenantInfo tenantInfo) throws ApplicationException { - CloudCredentials credential = this.cache.getIfPresent(tenantInfo.getName()); - if (credential == null) { - credential = new CloudCredentials(tenantInfo); - this.cache.put(tenantInfo.getName(), credential); - } - - return StorageOptions.newBuilder().setCredentials(credential).setProjectId(tenantInfo.getProjectId()) - .setRetrySettings(RETRY_SETTINGS).setTransportOptions(TRANSPORT_OPTIONS).build().getService(); - } -} \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java deleted file mode 100644 index 4c92cc3e..00000000 --- a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java +++ /dev/null @@ -1,109 +0,0 @@ -package org.opengroup.osdu.schema.impl.schemainfostore; - -import java.text.MessageFormat; - -import org.opengroup.osdu.core.common.model.http.DpsHeaders; -import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.credentials.DatastoreFactory; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.opengroup.osdu.schema.exceptions.BadRequestException; -import org.opengroup.osdu.schema.exceptions.NotFoundException; -import org.opengroup.osdu.schema.model.Authority; -import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.IAuthorityStore; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Repository; - -import com.google.cloud.Timestamp; -import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreException; -import com.google.cloud.datastore.Entity; -import com.google.cloud.datastore.Key; - -import lombok.extern.java.Log; - -/** - * Repository class to to register authority in Google store. - * - */ - -@Log -@Repository -public class IbmAuthorityStore implements IAuthorityStore { - - @Autowired - private DpsHeaders headers; - - @Autowired - private DatastoreFactory dataStoreFactory; - - @Autowired - private ITenantFactory tenantFactory; - - /** - * Method to get authority from google store - * - * @param authorityId - * @return Authority object - * @throws ApplicationException - * @throws NotFoundException - */ - @Override - public Authority get(String authorityId) throws NotFoundException, ApplicationException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.AUTHORITY_KIND).newKey(authorityId); - - Entity entity = datastore.get(key); - if (entity == null) { - throw new NotFoundException("bad input parameter"); - } else { - return mapEntityToDto(entity); - } - - } - - /** - * Method to create authority in google store of dataPartitionId project - * - * @param authority - * @param dataPartitionId - * @return Authority object - * @throws ApplicationException - * @throws BadRequestException - */ - @Override - public Authority create(Authority authority) throws ApplicationException, BadRequestException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.AUTHORITY_KIND).newKey(authority.getAuthorityId()); - Entity entity = getEntityObject(authority, key); - try { - datastore.add(entity); - } catch (DatastoreException ex) { - if ("ALREADY_EXISTS".equals(ex.getReason())) { - log.warning(SchemaConstants.AUTHORITY_EXISTS_ALREADY_REGISTERED); - throw new BadRequestException( - MessageFormat.format(SchemaConstants.AUTHORITY_EXISTS_EXCEPTION, authority.getAuthorityId())); - } else { - log.severe(MessageFormat.format(SchemaConstants.OBJECT_INVALID, ex.getMessage())); - throw new ApplicationException(SchemaConstants.INVALID_INPUT); - } - } - log.info(SchemaConstants.AUTHORITY_CREATED); - return mapEntityToDto(entity); - } - - private Authority mapEntityToDto(Entity entity) { - - Authority authority = new Authority(); - authority.setAuthorityId(entity.getKey().getName()); - return authority; - - } - - private Entity getEntityObject(Authority authority, Key key) { - return Entity.newBuilder(key).set(SchemaConstants.DATE_CREATED, Timestamp.now()).build(); - } - -} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java deleted file mode 100644 index be6f4c66..00000000 --- a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java +++ /dev/null @@ -1,110 +0,0 @@ -package org.opengroup.osdu.schema.impl.schemainfostore; - -import java.text.MessageFormat; - -import org.opengroup.osdu.core.common.model.http.DpsHeaders; -import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.credentials.DatastoreFactory; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.opengroup.osdu.schema.exceptions.BadRequestException; -import org.opengroup.osdu.schema.exceptions.NotFoundException; -import org.opengroup.osdu.schema.model.EntityType; -import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.IEntityTypeStore; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Repository; - -import com.google.cloud.Timestamp; -import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreException; -import com.google.cloud.datastore.Entity; -import com.google.cloud.datastore.Key; - -import lombok.extern.java.Log; - -/** - * Repository class to register Entity type in Google store. - * - * - */ -@Log -@Repository -public class IbmEntityTypeStore implements IEntityTypeStore { - - @Autowired - private DpsHeaders headers; - - @Autowired - private DatastoreFactory dataStoreFactory; - - @Autowired - private ITenantFactory tenantFactory; - - /** - * Method to get entity type from google store - * - * @param entityTypeId - * @return EntityType object - * @throws ApplicationException - * @throws NotFoundException - */ - @Override - public EntityType get(String entityTypeId) throws NotFoundException, ApplicationException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.ENTITYTYPE_KIND).newKey(entityTypeId); - Entity entity = datastore.get(key); - if (entity == null) { - throw new NotFoundException("bad input parameter"); - } else { - return mapEntityToDto(entity); - } - - } - - /** - * Method to create entityType in google store of dataPartitionId GCP - * - * @param entityType - * @param dataPartitionId - * @return entityType object - * @throws ApplicationException - * @throws BadRequestException - */ - @Override - public EntityType create(EntityType entityType) throws BadRequestException, ApplicationException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.ENTITYTYPE_KIND).newKey(entityType.getEntityTypeId()); - Entity entity = getEntityObject(entityType, key); - try { - datastore.add(entity); - } catch (DatastoreException ex) { - if ("ALREADY_EXISTS".equals(ex.getReason())) { - log.warning(SchemaConstants.ENTITY_TYPE_EXISTS); - throw new BadRequestException( - MessageFormat.format(SchemaConstants.ENTITY_TYPE_EXISTS_EXCEPTION, entityType.getEntityTypeId())); - } else { - log.severe(MessageFormat.format(SchemaConstants.OBJECT_INVALID, ex.getMessage())); - throw new ApplicationException("Invalid input, object invalid"); - } - } - log.info(SchemaConstants.ENTITY_TYPE_CREATED); - return mapEntityToDto(entity); - } - - private EntityType mapEntityToDto(Entity entity) { - - EntityType entityType = new EntityType(); - entityType.setEntityTypeId(entity.getKey().getName()); - return entityType; - - } - - private Entity getEntityObject(EntityType entityType, Key key) { - Entity.Builder entityBuilder = Entity.newBuilder(key); - entityBuilder.set(SchemaConstants.DATE_CREATED, Timestamp.now()); - return entityBuilder.build(); - } - -} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java deleted file mode 100644 index 4cef4c22..00000000 --- a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java +++ /dev/null @@ -1,373 +0,0 @@ -package org.opengroup.osdu.schema.impl.schemainfostore; - -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeMap; -import java.util.stream.Collectors; - -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; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.credentials.DatastoreFactory; -import org.opengroup.osdu.schema.enums.SchemaScope; -import org.opengroup.osdu.schema.enums.SchemaStatus; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.opengroup.osdu.schema.exceptions.BadRequestException; -import org.opengroup.osdu.schema.exceptions.NotFoundException; -import org.opengroup.osdu.schema.model.QueryParams; -import org.opengroup.osdu.schema.model.SchemaIdentity; -import org.opengroup.osdu.schema.model.SchemaInfo; -import org.opengroup.osdu.schema.model.SchemaRequest; -import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.ISchemaInfoStore; -import org.opengroup.osdu.schema.util.VersionHierarchyUtil; -import org.springframework.beans.factory.annotation.Autowired; -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; -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 lombok.extern.java.Log; - -/** - * Repository class to to register Schema in Google store. - * - */ -@Log -@Repository -public class IbmSchemaInfoStore implements ISchemaInfoStore { - - @Autowired - private DpsHeaders headers; - - @Autowired - private DatastoreFactory dataStoreFactory; - - @Autowired - private ITenantFactory tenantFactory; - - /** - * Method to get schemaInfo from google store - * - * @param schemaId - * @return schemaInfo object - * @throws ApplicationException - * @throws NotFoundException - */ - @Override - public SchemaInfo getSchemaInfo(String schemaId) throws ApplicationException, NotFoundException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE).setKind(SchemaConstants.SCHEMA_KIND) - .newKey(schemaId); - Entity entity = datastore.get(key); - if (entity != null) { - return getSchemaInfoObject(entity, datastore); - } - throw new NotFoundException(SchemaConstants.SCHEMA_NOT_PRESENT); - - } - - /** - * Method to Create schema in google store of tenantId GCP - * - * @param schema - * @param tenantId - * @return schemaInfo object - * @throws ApplicationException - * @throws BadRequestException - */ - @Override - public SchemaInfo createSchemaInfo(SchemaRequest schema) throws ApplicationException, BadRequestException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - KeyFactory keyFactory = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.SCHEMA_KIND); - Entity entity = getEntityObject(schema, datastore, keyFactory); - try { - datastore.add(entity); - } catch (DatastoreException ex) { - if (SchemaConstants.ALREADY_EXISTS.equals(ex.getReason())) { - log.warning(SchemaConstants.SCHEMA_EXISTS); - throw new BadRequestException(SchemaConstants.SCHEMA_EXISTS + " with Id: " - + schema.getSchemaInfo().getSchemaIdentity().getId()); - } else { - log.severe(SchemaConstants.OBJECT_INVALID); - throw new ApplicationException(SchemaConstants.SCHEMA_CREATION_FAILED_INVALID_OBJECT); - } - } - log.info(SchemaConstants.SCHEMA_CREATED); - return getSchemaInfoObject(entity, datastore); - } - - /** - * Method to update schema in google store of tenantId GCP - * - * @param schema - * @param tenantId - * @return schemaInfo object - * @throws ApplicationException - * @throws BadRequestException - */ - @Override - public SchemaInfo updateSchemaInfo(SchemaRequest schema) throws ApplicationException, BadRequestException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - KeyFactory keyFactory = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.SCHEMA_KIND); - Entity entity = getEntityObject(schema, datastore, keyFactory); - try { - datastore.put(entity); - } catch (DatastoreException ex) { - log.severe(SchemaConstants.OBJECT_INVALID); - throw new ApplicationException("Invalid object, updation failed"); - } - return getSchemaInfoObject(entity, datastore); - } - - /** - * Method to clean schemaInfo in google datastore of tenantId GCP - * - * @param schemaId - * @return status - * @throws ApplicationException - */ - @Override - public boolean cleanSchema(String schemaId) throws ApplicationException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - KeyFactory keyFactory = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.SCHEMA_KIND); - Key key = keyFactory.newKey(schemaId); - try { - datastore.delete(key); - return true; - } catch (DatastoreException ex) { - return false; - } - } - - @Override - public String getLatestMinorVerSchema(SchemaInfo schemaInfo) throws ApplicationException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - Query query = Query.newEntityQueryBuilder().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.SCHEMA_KIND) - .setFilter(CompositeFilter.and( - PropertyFilter.eq(SchemaConstants.AUTHORITY, schemaInfo.getSchemaIdentity().getAuthority()), - PropertyFilter.eq(SchemaConstants.ENTITY_TYPE, schemaInfo.getSchemaIdentity().getEntityType()), - PropertyFilter.eq(SchemaConstants.MAJOR_VERSION, - schemaInfo.getSchemaIdentity().getSchemaVersionMajor()), - PropertyFilter.eq(SchemaConstants.SOURCE, schemaInfo.getSchemaIdentity().getSource()))) - .build(); - - QueryResults result = datastore.run(query); - TreeMap sortedMap = new TreeMap<>(Collections.reverseOrder()); - while (result.hasNext()) { - Entity entity = result.next(); - sortedMap.put(entity.getLong(SchemaConstants.MINOR_VERSION), entity.getBlob(SchemaConstants.SCHEMA)); - } - if (sortedMap.size() != 0) { - Entry blob = sortedMap.firstEntry(); - return new String(blob.getValue().toByteArray()); - } - return new String(); - } - - private SchemaInfo getSchemaInfoObject(Entity entity, Datastore datastore) { - - SchemaIdentity superSededBy = null; - if (entity.contains(SchemaConstants.SUPERSEDED_BY)) { - KeyFactory keyFactory = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE).setKind(SchemaConstants.SCHEMA_KIND); - Entity superSededEntity = datastore.get(keyFactory.newKey(entity.getString(SchemaConstants.SUPERSEDED_BY))); - superSededBy = getSchemaIdentity(superSededEntity); - } - - return SchemaInfo.builder().createdBy(entity.getString(SchemaConstants.CREATED_BY)) - .dateCreated(entity.getTimestamp(SchemaConstants.DATE_CREATED).toDate()) - .scope(SchemaScope.valueOf(entity.getString(SchemaConstants.SCOPE))) - .status(SchemaStatus.valueOf(entity.getString(SchemaConstants.STATUS))) - .schemaIdentity(getSchemaIdentity(entity)).supersededBy(superSededBy).build(); - - } - - private Entity getEntityObject(SchemaRequest schema, Datastore datastore, KeyFactory keyFactory) - throws BadRequestException, ApplicationException { - - Key key = keyFactory.newKey(schema.getSchemaInfo().getSchemaIdentity().getId()); - - Entity.Builder entityBuilder = Entity.newBuilder(key); - if (schema.getSchemaInfo().getSupersededBy() != null) { - if (schema.getSchemaInfo().getSupersededBy().getId() == null - || datastore.get(keyFactory.newKey(schema.getSchemaInfo().getSupersededBy().getId())) == null) { - log.severe(SchemaConstants.INVALID_SUPERSEDEDBY_ID); - throw new BadRequestException(SchemaConstants.INVALID_SUPERSEDEDBY_ID); - } - entityBuilder.set(SchemaConstants.SUPERSEDED_BY, schema.getSchemaInfo().getSupersededBy().getId()); - } - - entityBuilder.set(SchemaConstants.DATE_CREATED, Timestamp.now()); - entityBuilder.set(SchemaConstants.CREATED_BY, headers.getUserEmail()); - entityBuilder.set(SchemaConstants.AUTHORITY, schema.getSchemaInfo().getSchemaIdentity().getAuthority()); - entityBuilder.set(SchemaConstants.SOURCE, schema.getSchemaInfo().getSchemaIdentity().getSource()); - entityBuilder.set(SchemaConstants.ENTITY_TYPE, schema.getSchemaInfo().getSchemaIdentity().getEntityType()); - entityBuilder.set(SchemaConstants.MAJOR_VERSION, - schema.getSchemaInfo().getSchemaIdentity().getSchemaVersionMajor()); - entityBuilder.set(SchemaConstants.MINOR_VERSION, - schema.getSchemaInfo().getSchemaIdentity().getSchemaVersionMinor()); - entityBuilder.set(SchemaConstants.PATCH_VERSION, - schema.getSchemaInfo().getSchemaIdentity().getSchemaVersionPatch()); - entityBuilder.set(SchemaConstants.SCOPE, schema.getSchemaInfo().getScope().name()); - entityBuilder.set(SchemaConstants.STATUS, schema.getSchemaInfo().getStatus().name()); - Gson gson = new Gson(); - Blob schemaBlob = Blob.copyFrom(gson.toJson(schema.getSchema()).getBytes()); - entityBuilder.set(SchemaConstants.SCHEMA, BlobValue.newBuilder(schemaBlob).setExcludeFromIndexes(true).build()); - - return entityBuilder.build(); - } - - private SchemaIdentity getSchemaIdentity(Entity entity) { - - return SchemaIdentity.builder().id(entity.getKey().getName()) - .authority(entity.getString(SchemaConstants.AUTHORITY)).source(entity.getString(SchemaConstants.SOURCE)) - .entityType(entity.getString(SchemaConstants.ENTITY_TYPE)) - .schemaVersionMajor(entity.getLong(SchemaConstants.MAJOR_VERSION)) - .schemaVersionMinor(entity.getLong(SchemaConstants.MINOR_VERSION)) - .schemaVersionPatch(entity.getLong(SchemaConstants.PATCH_VERSION)).build(); - - } - - @Override - public List getSchemaInfoList(QueryParams queryParams, String tenantId) throws ApplicationException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(tenantId)); - List filterList = getFilters(queryParams); - - EntityQuery.Builder queryBuilder = Query.newEntityQueryBuilder().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.SCHEMA_KIND); - - if (!filterList.isEmpty()) { - queryBuilder.setFilter( - CompositeFilter.and(filterList.get(0), filterList.toArray(new Filter[filterList.size()]))); - } - - QueryResults result = datastore.run(queryBuilder.build()); - List schemaList = new LinkedList<>(); - while (result.hasNext()) { - Entity entity = result.next(); - schemaList.add(getSchemaInfoObject(entity, datastore)); - } - - if (queryParams.getLatestVersion() != null && queryParams.getLatestVersion()) { - return getLatestVersionSchemaList(schemaList); - } - - return schemaList; - } - - private List getLatestVersionSchemaList(List filteredSchemaList) { - List latestSchemaList = new LinkedList<>(); - SchemaInfo previousSchemaInfo = null; - TreeMap sortedMap = new TreeMap<>( - new VersionHierarchyUtil.SortingVersionComparator()); - - for (SchemaInfo schemaInfoObject : filteredSchemaList) { - if ((previousSchemaInfo != null) && !(checkAuthorityMatch(previousSchemaInfo, schemaInfoObject) - && checkSourceMatch(previousSchemaInfo, schemaInfoObject) - && checkEntityMatch(previousSchemaInfo, schemaInfoObject))) { - Entry latestVersionEntry = sortedMap.firstEntry(); - latestSchemaList.add(latestVersionEntry.getValue()); - sortedMap.clear(); - } - previousSchemaInfo = schemaInfoObject; - SchemaIdentity schemaIdentity = schemaInfoObject.getSchemaIdentity(); - VersionHierarchyUtil version = new VersionHierarchyUtil(schemaIdentity.getSchemaVersionMajor(), - schemaIdentity.getSchemaVersionMinor(), schemaIdentity.getSchemaVersionPatch()); - sortedMap.put(version, schemaInfoObject); - } - if (sortedMap.size() != 0) { - Entry latestVersionEntry = sortedMap.firstEntry(); - latestSchemaList.add(latestVersionEntry.getValue()); - } - - return latestSchemaList; - } - - private boolean checkEntityMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) { - return schemaInfoObject.getSchemaIdentity().getEntityType() - .equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getEntityType()); - } - - private boolean checkSourceMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) { - return schemaInfoObject.getSchemaIdentity().getSource() - .equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getSource()); - } - - private boolean checkAuthorityMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) { - return schemaInfoObject.getSchemaIdentity().getAuthority() - .equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getAuthority()); - } - - private List getFilters(QueryParams queryParams) { - List filterList = new LinkedList(); - if (queryParams.getAuthority() != null) { - filterList.add(PropertyFilter.eq(SchemaConstants.AUTHORITY, queryParams.getAuthority())); - } - if (queryParams.getSource() != null) { - filterList.add(PropertyFilter.eq(SchemaConstants.SOURCE, queryParams.getSource())); - } - if (queryParams.getEntityType() != null) { - filterList.add(PropertyFilter.eq(SchemaConstants.ENTITY_TYPE, queryParams.getEntityType())); - } - if (queryParams.getSchemaVersionMajor() != null) { - filterList.add(PropertyFilter.eq(SchemaConstants.MAJOR_VERSION, queryParams.getSchemaVersionMajor())); - } - if (queryParams.getSchemaVersionMinor() != null) { - filterList.add(PropertyFilter.eq(SchemaConstants.MINOR_VERSION, queryParams.getSchemaVersionMinor())); - } - if (queryParams.getStatus() != null) { - filterList.add(PropertyFilter.eq(SchemaConstants.STATUS, queryParams.getStatus().toUpperCase())); - } - return filterList; - } - - @Override - public boolean isUnique(String schemaId, String tenantId) throws ApplicationException { - - Set tenantList = new HashSet<>(); - tenantList.add(SchemaConstants.ACCOUNT_ID_COMMON_PROJECT); - tenantList.add(tenantId); - - // code to call check uniqueness - if (tenantId.equalsIgnoreCase(SchemaConstants.ACCOUNT_ID_COMMON_PROJECT)) { - List privateTenantList = tenantFactory.listTenantInfo().stream().map(TenantInfo::getDataPartitionId) - .collect(Collectors.toList()); - tenantList.addAll(privateTenantList); - - } - for (String tenant : tenantList) { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(tenant)); - Key schemaKey = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.SCHEMA_KIND).newKey(schemaId); - - Query query = Query.newKeyQueryBuilder().setNamespace(SchemaConstants.NAMESPACE) - .setKind(SchemaConstants.SCHEMA_KIND).setFilter(PropertyFilter.eq("__key__", schemaKey)).build(); - QueryResults keys = datastore.run(query); - if (keys.hasNext()) - return false; - } - return true; - } -} \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java deleted file mode 100644 index afeb9fb7..00000000 --- a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java +++ /dev/null @@ -1,108 +0,0 @@ -package org.opengroup.osdu.schema.impl.schemainfostore; - -import java.text.MessageFormat; - -import org.opengroup.osdu.core.common.model.http.DpsHeaders; -import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.credentials.DatastoreFactory; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.opengroup.osdu.schema.exceptions.BadRequestException; -import org.opengroup.osdu.schema.exceptions.NotFoundException; -import org.opengroup.osdu.schema.model.Source; -import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.ISourceStore; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Repository; - -import com.google.cloud.Timestamp; -import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreException; -import com.google.cloud.datastore.Entity; -import com.google.cloud.datastore.Key; - -import lombok.extern.java.Log; - -/** - * Repository class to to register Source in Google store. - * - * - */ -@Log -@Repository -public class IbmSourceStore implements ISourceStore { - - @Autowired - private DpsHeaders headers; - - @Autowired - private DatastoreFactory dataStoreFactory; - - @Autowired - private ITenantFactory tenantFactory; - - /** - * Method to create Source in google store - * - * @param sourceId - * @return Source object - * @throws ApplicationException - */ - @Override - public Source get(String sourceId) throws NotFoundException, ApplicationException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE).setKind(SchemaConstants.SOURCE_KIND) - .newKey(sourceId); - Entity entity = datastore.get(key); - if (entity == null) { - throw new NotFoundException("bad input parameter"); - } else { - return mapEntityToDto(entity); - } - - } - - /** - * Method to create Source in google store of dataPartitionId GCP - * - * @param Source - * @param dataPartitionId - * @return Source object - */ - @Override - public Source create(Source source) throws BadRequestException, ApplicationException { - Datastore datastore = dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId())); - Key key = datastore.newKeyFactory().setNamespace(SchemaConstants.NAMESPACE).setKind(SchemaConstants.SOURCE_KIND) - .newKey(source.getSourceId()); - Entity entity = getEntityObject(source, key); - - try { - datastore.add(entity); - } catch (DatastoreException ex) { - if ("ALREADY_EXISTS".equals(ex.getReason())) { - log.warning(SchemaConstants.SOURCE_EXISTS); - throw new BadRequestException( - MessageFormat.format(SchemaConstants.SOURCE_EXISTS_EXCEPTION, source.getSourceId())); - } else { - log.severe(SchemaConstants.OBJECT_INVALID); - throw new ApplicationException(SchemaConstants.INVALID_INPUT); - } - } - log.info(SchemaConstants.SOURCE_CREATED); - return mapEntityToDto(entity); - } - - private Source mapEntityToDto(Entity entity) { - - Source source = new Source(); - source.setSourceId(entity.getKey().getName()); - return source; - - } - - private Entity getEntityObject(Source source, Key key) { - Entity.Builder entityBuilder = Entity.newBuilder(key); - entityBuilder.set(SchemaConstants.DATE_CREATED, Timestamp.now()); - return entityBuilder.build(); - } - -} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStore.java deleted file mode 100644 index 03aeb0a9..00000000 --- a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStore.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.opengroup.osdu.schema.impl.schemastore; - -import org.opengroup.osdu.core.common.model.http.DpsHeaders; -import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.credentials.StorageFactory; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.opengroup.osdu.schema.exceptions.NotFoundException; -import org.opengroup.osdu.schema.provider.interfaces.schemastore.ISchemaStore; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Repository; - -import com.google.cloud.storage.Blob; -import com.google.cloud.storage.BlobId; -import com.google.cloud.storage.BlobInfo; -import com.google.cloud.storage.Storage; -import com.google.cloud.storage.StorageException; - -/** - * Repository class to to register resolved Schema in Google storage. - * - * - */ -@Repository -public class IbmSchemaStore implements ISchemaStore { - - @Autowired - private DpsHeaders headers; - - @Autowired - private StorageFactory storageFactory; - - @Autowired - TenantFactory tenantFactory; - - /** - * Method to get schema from google Storage given Tenant ProjectInfo - * - * @param dataPartitionId - * @param filePath - * @throws NotFoundException - * @return schema object - * @throws ApplicationException - * @throws NotFoundException - */ - @Override - public String getSchema(String dataPartitionId, String filePath) throws ApplicationException, NotFoundException { - filePath = filePath + SchemaConstants.JSON_EXTENSION; - String bucketname = getSchemaBucketName(dataPartitionId); - Storage storage = storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId)); - Blob blob = storage.get(bucketname, filePath); - if (blob != null) - return new String(blob.getContent()); - throw new NotFoundException(SchemaConstants.SCHEMA_NOT_PRESENT); - } - - /** - * Method to write schema to google Storage given Tenant ProjectInfo - * - * @param dataPartitionId - * @param filePath - * @param content - * @return schema object - * @throws ApplicationException - */ - - @Override - public String createSchema(String filePath, String content) throws ApplicationException { - String dataPartitionId = headers.getPartitionId(); - filePath = filePath + SchemaConstants.JSON_EXTENSION; - String bucketname = getSchemaBucketName(dataPartitionId); - BlobId blobId = BlobId.of(bucketname, filePath); - BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); - Storage storage = storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId)); - try { - Blob blob = storage.create(blobInfo, content.getBytes()); - return blob.getName(); - } catch (StorageException ex) { - throw new ApplicationException(SchemaConstants.INTERNAL_SERVER_ERROR); - } - } - - private String getSchemaBucketName(String dataPartitionId) { - return tenantFactory.getTenantInfo(dataPartitionId).getProjectId() + SchemaConstants.SCHEMA_BUCKET_EXTENSION; - } - - @Override - public boolean cleanSchemaProject(String schemaId) throws ApplicationException { - String dataPartitionId = headers.getPartitionId(); - String fileName = schemaId + SchemaConstants.JSON_EXTENSION; - String bucketname = getSchemaBucketName(dataPartitionId); - BlobId blobId = BlobId.of(bucketname, fileName); - return storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId)).delete(blobId); - } - -} diff --git a/provider/schema-ibm/src/main/resources/application-kuber.properties b/provider/schema-ibm/src/main/resources/application-kuber.properties deleted file mode 100644 index 6f30385e..00000000 --- a/provider/schema-ibm/src/main/resources/application-kuber.properties +++ /dev/null @@ -1 +0,0 @@ -AUTHORIZE_API=http://os-entitlement-service/entitlements/v1 diff --git a/provider/schema-ibm/src/main/resources/application.properties b/provider/schema-ibm/src/main/resources/application.properties deleted file mode 100644 index e331ce85..00000000 --- a/provider/schema-ibm/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -server.servlet.context-path=/api/schema-service/v1 -logging.level.org.springframework.web=DEBUG -server.port=8088 -AUTHORIZE_API=http://localhost:8080s/entitlements/v1 -AUTHORIZE_API_KEY= \ No newline at end of file diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java deleted file mode 100644 index 32fc3ac4..00000000 --- a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java +++ /dev/null @@ -1,165 +0,0 @@ -package org.opengroup.osdu.schema.impl.schemainfostore; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.opengroup.osdu.core.common.model.http.DpsHeaders; -import org.opengroup.osdu.core.common.model.tenant.TenantInfo; -import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.credentials.DatastoreFactory; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.opengroup.osdu.schema.exceptions.BadRequestException; -import org.opengroup.osdu.schema.exceptions.NotFoundException; -import org.opengroup.osdu.schema.model.Authority; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.google.cloud.Timestamp; -import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreException; -import com.google.cloud.datastore.Entity; -import com.google.cloud.datastore.Key; -import com.google.cloud.datastore.KeyFactory; - -@RunWith(SpringJUnit4ClassRunner.class) -public class IbmAuthorityStoreTest { - - @InjectMocks - IbmAuthorityStore mockGoogleAuthorityStore; - - @Mock - KeyFactory keyFactory; - - @Mock - Entity mockEntity; - - @Mock - Datastore dataStore; - - @Mock - DatastoreFactory dataStoreFactory; - - @Mock - Authority mockAuthority; - - @Mock - Key key; - - @Mock - DpsHeaders headers; - - @Mock - TenantFactory tenantFactory; - - @Mock - TenantInfo tenantInfo; - - @Test - public void testGetAuthority() throws NotFoundException, ApplicationException { - String authorityId = "testAuthorityId"; - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.AUTHORITY_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey(authorityId)).thenReturn(key); - Mockito.when(dataStore.get(key)).thenReturn(mockEntity); - Mockito.when(mockEntity.getKey()).thenReturn(key); - assertNotNull(mockGoogleAuthorityStore.get(authorityId)); - } - - @Test - public void testGetAuthority_NotFoundException() { - String authorityId = ""; - try { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.AUTHORITY_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey(authorityId)).thenReturn(key); - Mockito.when(dataStore.get(key)).thenReturn(null); - mockGoogleAuthorityStore.get(authorityId); - fail("Should not succeed"); - } catch (NotFoundException e) { - assertEquals("bad input parameter", e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - - @Test - public void testCreateAuthority() throws NotFoundException, ApplicationException, BadRequestException { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.AUTHORITY_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey("os")).thenReturn(key); - Mockito.when(dataStore.get(key)).thenReturn(mockEntity); - Entity entity = Entity.newBuilder(key).set(SchemaConstants.DATE_CREATED, Timestamp.now()).build(); - Mockito.when(dataStore.add(entity)).thenReturn(entity); - Mockito.when(mockAuthority.getAuthorityId()).thenReturn("os"); - assertNotNull(mockGoogleAuthorityStore.create(mockAuthority)); - } - - @Test - public void testCreateAuthority_BadRequestException() - throws NotFoundException, ApplicationException, BadRequestException { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - mockGoogleAuthorityStore = Mockito.spy(mockGoogleAuthorityStore); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.AUTHORITY_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey("os")).thenReturn(key); - Mockito.when(dataStore.add(Mockito.any(Entity.class))) - .thenThrow(new DatastoreException(400, SchemaConstants.ALREADY_EXISTS, SchemaConstants.ALREADY_EXISTS)); - Mockito.when(mockAuthority.getAuthorityId()).thenReturn("os"); - try { - mockGoogleAuthorityStore.create(mockAuthority); - fail("Should not succeed"); - } catch (BadRequestException e) { - assertEquals("Authority already registered with Id: os", e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - - @Test - public void testCreateAuthority_ApplicationException() - throws NotFoundException, ApplicationException, BadRequestException { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - mockGoogleAuthorityStore = Mockito.spy(mockGoogleAuthorityStore); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.AUTHORITY_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey("os")).thenReturn(key); - Mockito.when(dataStore.add(Mockito.any(Entity.class))).thenThrow(DatastoreException.class); - Mockito.when(mockAuthority.getAuthorityId()).thenReturn("os"); - try { - mockGoogleAuthorityStore.create(mockAuthority); - fail("Should not succeed"); - } catch (ApplicationException e) { - assertEquals(SchemaConstants.INVALID_INPUT, e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - -} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java deleted file mode 100644 index 7cb434c8..00000000 --- a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java +++ /dev/null @@ -1,166 +0,0 @@ -package org.opengroup.osdu.schema.impl.schemainfostore; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.opengroup.osdu.core.common.model.http.DpsHeaders; -import org.opengroup.osdu.core.common.model.tenant.TenantInfo; -import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.credentials.DatastoreFactory; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.opengroup.osdu.schema.exceptions.BadRequestException; -import org.opengroup.osdu.schema.exceptions.NotFoundException; -import org.opengroup.osdu.schema.model.EntityType; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.google.cloud.Timestamp; -import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreException; -import com.google.cloud.datastore.Entity; -import com.google.cloud.datastore.Key; -import com.google.cloud.datastore.KeyFactory; - -@RunWith(SpringJUnit4ClassRunner.class) -public class IbmEntityTypeStoreTest { - - @InjectMocks - IbmEntityTypeStore googleEntityStore; - - @Mock - KeyFactory keyFactory; - - @Mock - Entity entity; - - @Mock - Datastore dataStore; - - @Mock - EntityType mockEntityType; - - @Mock - Key key; - - @Mock - DpsHeaders headers; - - @Mock - DatastoreFactory dataStoreFactory; - - @Mock - TenantInfo tenantInfo; - - @Mock - TenantFactory tenantFactory; - - @Test - public void testGet() throws NotFoundException, ApplicationException { - System.out.println("testGet"); - String entityId = "testEntityId"; - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.ENTITY_TYPE)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey(entityId)).thenReturn(key); - Mockito.when(dataStore.get(key)).thenReturn(entity); - Mockito.when(entity.getKey()).thenReturn(key); - assertNotNull(googleEntityStore.get(entityId)); - } - - @Test - public void testGet_NotFoundException() { - System.out.println("testGet_NotFoundException"); - String entityId = ""; - try { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.ENTITY_TYPE)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey(entityId)).thenReturn(key); - Mockito.when(dataStore.get(key)).thenReturn(null); - googleEntityStore.get(entityId); - fail("Should not succeed"); - } catch (NotFoundException e) { - assertEquals("bad input parameter", e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - - @Test - public void testCreate() throws NotFoundException, ApplicationException, BadRequestException { - System.out.println("testCreate"); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.ENTITY_TYPE)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey("wellbore")).thenReturn(key); - Entity entity = Entity.newBuilder(key).set(SchemaConstants.DATE_CREATED, Timestamp.now()).build(); - Mockito.when(dataStore.add(entity)).thenReturn(entity); - Mockito.when(mockEntityType.getEntityTypeId()).thenReturn("wellbore"); - assertNotNull(googleEntityStore.create(mockEntityType)); - } - - @Test - public void testCreate_BadRequestException() throws NotFoundException, ApplicationException, BadRequestException { - System.out.println("testCreate_BadRequestException"); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(mockEntityType.getEntityTypeId()).thenReturn("wks"); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.ENTITYTYPE_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey("wks")).thenReturn(key); - Mockito.when(dataStore.add(Mockito.any(Entity.class))) - .thenThrow(new DatastoreException(400, SchemaConstants.ALREADY_EXISTS, SchemaConstants.ALREADY_EXISTS)); - - try { - googleEntityStore.create(mockEntityType); - fail("Should not succeed"); - } catch (BadRequestException e) { - assertEquals("EntityType already registered with Id: wks", e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - - @Test - public void testCreate_ApplicationException() throws NotFoundException, ApplicationException, BadRequestException { - System.out.println("testCreate_ApplicationException"); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.ENTITYTYPE_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey("wks")).thenReturn(key); - Mockito.when(dataStore.add(Mockito.any(Entity.class))).thenThrow(DatastoreException.class); - Mockito.when(mockEntityType.getEntityTypeId()).thenReturn("wks"); - try { - googleEntityStore.create(mockEntityType); - fail("Should not succeed"); - } catch (ApplicationException e) { - assertEquals("Invalid input, object invalid", e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - -} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java deleted file mode 100644 index c706a41c..00000000 --- a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java +++ /dev/null @@ -1,654 +0,0 @@ -package org.opengroup.osdu.schema.impl.schemainfostore; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.opengroup.osdu.core.common.model.http.DpsHeaders; -import org.opengroup.osdu.core.common.model.tenant.TenantInfo; -import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.credentials.DatastoreFactory; -import org.opengroup.osdu.schema.enums.SchemaScope; -import org.opengroup.osdu.schema.enums.SchemaStatus; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.opengroup.osdu.schema.exceptions.BadRequestException; -import org.opengroup.osdu.schema.exceptions.NotFoundException; -import org.opengroup.osdu.schema.model.QueryParams; -import org.opengroup.osdu.schema.model.SchemaIdentity; -import org.opengroup.osdu.schema.model.SchemaInfo; -import org.opengroup.osdu.schema.model.SchemaRequest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.google.cloud.Timestamp; -import com.google.cloud.datastore.Blob; -import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreException; -import com.google.cloud.datastore.Entity; -import com.google.cloud.datastore.Key; -import com.google.cloud.datastore.KeyFactory; -import com.google.cloud.datastore.ProjectionEntity; -import com.google.cloud.datastore.Query; -import com.google.cloud.datastore.QueryResults; -import com.google.cloud.datastore.TimestampValue; - -@RunWith(SpringJUnit4ClassRunner.class) -public class IbmSchemaInfoStoreTest { - - @InjectMocks - IbmSchemaInfoStore schemaInfoStore; - - @Mock - KeyFactory keyFactory; - - @Mock - ProjectionEntity projectionEntity; - - @Mock - Entity entity; - - @Mock - Datastore dataStore; - - @Mock - SchemaRequest schemaRequest; - - @Mock - Key key; - - @Mock - Blob blob; - - @Mock - Query query; - - @Mock - QueryResults queryResult; - - @Mock - DatastoreFactory dataStoreFactory; - - @Mock - DpsHeaders headers; - - @Mock - Query keyQuery; - - @Mock - QueryResults keys; - - @Mock - TenantInfo tenantInfo; - - @Mock - TenantInfo tenantInfoCommon; - - @Mock - TenantFactory tenantFactory; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void testGetLatestMinorVersion_ReturnNull() throws NotFoundException, ApplicationException { - when(headers.getPartitionId()).thenReturn("tenant"); - Mockito.when(tenantFactory.getTenantInfo("tenant")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); - Mockito.when(entity.getBlob(SchemaConstants.SCHEMA)).thenReturn(blob); - Mockito.when(blob.toByteArray()).thenReturn("{}".getBytes()); - assertEquals("", schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo())); - } - - @Test - public void testGetSchemaInfo_NotEmptyEntity() throws NotFoundException, ApplicationException { - String schemaId = "schemaId"; - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(keyFactory.newKey(schemaId)).thenReturn(key); - Mockito.when(dataStore.get(key)).thenReturn(getMockEntityObject()); - SchemaInfo schemaInfo = schemaInfoStore.getSchemaInfo(schemaId); - assertEquals(SchemaStatus.PUBLISHED, schemaInfo.getStatus()); - } - - @Test - public void testGetSchemaInfo_EmptyEntity() throws NotFoundException, ApplicationException { - - expectedException.expect(NotFoundException.class); - expectedException.expectMessage(SchemaConstants.SCHEMA_NOT_PRESENT); - String schemaId = "schemaId"; - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(keyFactory.newKey(schemaId)).thenReturn(key); - Mockito.when(dataStore.get(key)).thenReturn(null); - schemaInfoStore.getSchemaInfo(schemaId); - } - - @Test - public void testCreateSchemaInfo_Positive() throws ApplicationException, BadRequestException { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); - Mockito.when(headers.getUserEmail()).thenReturn("hmadhani@delfi.com"); - Mockito.when(dataStore.add(entity)).thenReturn(entity); - assertNotNull(schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published())); - } - - @Test - public void testIsUnique_True() throws ApplicationException { - Key storageKey = mock(Key.class); - KeyFactory storageKeyFactory = mock(KeyFactory.class); - String schemaId = "schemaId"; - String tenantId = "tenant"; - Mockito.when(tenantFactory.getTenantInfo("tenant")).thenReturn(tenantInfo); - Mockito.when(tenantFactory.getTenantInfo("common")).thenReturn(tenantInfoCommon); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - 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); - Query any = Mockito.any(Query.class); - when(dataStore.run(any)).thenReturn(keys); - when(keys.hasNext()).thenReturn(false); - assertTrue(schemaInfoStore.isUnique(schemaId, tenantId)); - } - - @Test - public void testIsUnique_False() throws ApplicationException { - Key storageKey = mock(Key.class); - KeyFactory storageKeyFactory = mock(KeyFactory.class); - String schemaId = "schemaId"; - String tenantId = "tenant"; - Mockito.when(tenantFactory.getTenantInfo("tenant")).thenReturn(tenantInfo); - Mockito.when(tenantFactory.getTenantInfo("common")).thenReturn(tenantInfoCommon); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - 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); - Query any = Mockito.any(Query.class); - when(dataStore.run(any)).thenReturn(keys); - when(keys.hasNext()).thenReturn(true); - assertFalse(schemaInfoStore.isUnique(schemaId, tenantId)); - } - - @Test - public void testIsUnique_False_CommomTenant() throws ApplicationException { - Key storageKey = mock(Key.class); - KeyFactory storageKeyFactory = mock(KeyFactory.class); - String schemaId = "schemaId"; - String tenantId = "common"; - Mockito.when(tenantFactory.getTenantInfo("tenant")).thenReturn(tenantInfo); - Mockito.when(tenantFactory.getTenantInfo("common")).thenReturn(tenantInfoCommon); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - 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); - Query any = Mockito.any(Query.class); - when(dataStore.run(any)).thenReturn(keys); - when(keys.hasNext()).thenReturn(true); - assertFalse(schemaInfoStore.isUnique(schemaId, tenantId)); - } - - @Test - public void testUpdateSchemaInfo() throws NotFoundException, ApplicationException, BadRequestException { - String tenantId = "test"; - Mockito.when(headers.getPartitionId()).thenReturn(tenantId); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); - Mockito.when(headers.getUserEmail()).thenReturn("hmadhani@delfi.com"); - Mockito.when(dataStore.put(entity)).thenReturn(entity); - assertNotNull(schemaInfoStore.updateSchemaInfo(getMockSchemaObject_Published())); - } - - @Test - public void testCreateSchemaInfo_WithSupersededBy() - throws NotFoundException, ApplicationException, BadRequestException { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); - Key mockSupsededKey = Mockito.mock(Key.class); - Mockito.when(keyFactory.newKey("os:wks:well.1.2.1")).thenReturn(mockSupsededKey); - Mockito.when(headers.getUserEmail()).thenReturn("hmadhani@delfi.com"); - Mockito.when(dataStore.add(entity)).thenReturn(entity); - Mockito.when(dataStore.get(mockSupsededKey)).thenReturn(entity); - Mockito.when(entity.getKey()).thenReturn(mockSupsededKey); - assertNotNull(schemaInfoStore.createSchemaInfo(getMockSchemaObject_SuperSededBy())); - } - - @Test - public void testUpdateSchemaInfo_SupersededByException() - throws NotFoundException, ApplicationException, BadRequestException { - try { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); - Key mockSupsededKey = Mockito.mock(Key.class); - Mockito.when(keyFactory.newKey("os:wks:well.1.2.1")).thenReturn(mockSupsededKey); - Mockito.when(dataStore.add(entity)).thenReturn(entity); - Mockito.when(dataStore.get(mockSupsededKey)).thenReturn(null); - Mockito.when(entity.getKey()).thenReturn(mockSupsededKey); - schemaInfoStore.updateSchemaInfo(getMockSchemaObject_SuperSededBy()); - fail("Should not succeed"); - } catch (BadRequestException e) { - assertEquals("Invalid SuperSededBy id", e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - - @Test - public void testUpdateInfo_SupersededByWithoutIdException() - throws NotFoundException, ApplicationException, BadRequestException { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); - SchemaRequest schemaRequest = getMockSchemaObject_SuperSededByWithoutId(); - expectedException.expect(BadRequestException.class); - expectedException.expectMessage(SchemaConstants.INVALID_SUPERSEDEDBY_ID); - schemaInfoStore.updateSchemaInfo(schemaRequest); - } - - @Test - public void testCreateSchemaInfo_BadRequestException() - throws NotFoundException, ApplicationException, BadRequestException { - - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(headers.getUserEmail()).thenReturn("dummy-user"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); - Mockito.when(dataStore.add(Mockito.any(Entity.class))) - .thenThrow(new DatastoreException(400, SchemaConstants.ALREADY_EXISTS, SchemaConstants.ALREADY_EXISTS)); - try { - schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published()); - fail("Should not succeed"); - } catch (BadRequestException e) { - assertEquals("Schema already registered with Id: os:wks:well.1.1.1", e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - - @Test - public void testCreateSchemaInfo_ApplicationException() - throws NotFoundException, ApplicationException, BadRequestException { - - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(headers.getUserEmail()).thenReturn("dummy-user"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); - Mockito.when(dataStore.add(Mockito.any(Entity.class))).thenThrow(DatastoreException.class); - try { - schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published()); - fail("Should not succeed"); - } catch (ApplicationException e) { - assertEquals(SchemaConstants.SCHEMA_CREATION_FAILED_INVALID_OBJECT, e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - - @Test - public void testUpdateSchemaInfo_ApplicationException() - throws NotFoundException, ApplicationException, BadRequestException { - - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(headers.getUserEmail()).thenReturn("dummy-user"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(keyFactory.newKey("os:wks:well.1.1.1")).thenReturn(key); - Mockito.when(dataStore.put(Mockito.any(Entity.class))).thenThrow(DatastoreException.class); - try { - schemaInfoStore.updateSchemaInfo(getMockSchemaObject_Published()); - fail("Should not succeed"); - } catch (ApplicationException e) { - assertEquals("Invalid object, updation failed", e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - - @Test - public void testGetLatestMinorVersion_Entity() throws NotFoundException, ApplicationException { - when(headers.getPartitionId()).thenReturn("tenant"); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(headers.getUserEmail()).thenReturn("hmadhani@delfi.com"); - entity = Entity.newBuilder(getMockEntityObject()).set(SchemaConstants.SCHEMA, blob).build(); - Mockito.when(dataStore.add(entity)).thenReturn(getMockEntityObject()); - Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); - Mockito.when(queryResult.hasNext()).thenReturn(true, false); - Mockito.when(queryResult.next()).thenReturn(entity); - Mockito.when(blob.toByteArray()).thenReturn("{}".getBytes()); - assertEquals("{}", schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo())); - } - - @Test - public void testGetSchemaInfoList_withoutqueryparam() - throws NotFoundException, ApplicationException, BadRequestException { - - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); - Mockito.when(queryResult.hasNext()).thenReturn(true, false); - Mockito.when(queryResult.next()).thenReturn(getMockEntityObject()); - assertEquals(1, - schemaInfoStore.getSchemaInfoList(QueryParams.builder().limit(100).offset(0).build(), "test").size()); - } - - @Test - public void testGetSchemaInfoList_withqueryparam() - throws NotFoundException, ApplicationException, BadRequestException { - - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); - Mockito.when(queryResult.hasNext()).thenReturn(true, false); - Mockito.when(queryResult.next()).thenReturn(getMockEntityObject()); - assertEquals(1, - schemaInfoStore.getSchemaInfoList(QueryParams.builder().authority("test").source("test").entityType("test") - .schemaVersionMajor(1l).schemaVersionMinor(1l).scope("test").status("test").latestVersion(false) - .limit(100).offset(0).build(), "test").size()); - } - - @Test - public void testGetSchemaInfoList_latestVersionTrue_NoSchemaMatchScenario() - throws NotFoundException, ApplicationException, BadRequestException { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); - Mockito.when(queryResult.hasNext()).thenReturn(false); - assertEquals(0, - schemaInfoStore - .getSchemaInfoList(QueryParams.builder().authority("test").source("test").entityType("test") - .scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), "test") - .size()); - } - - @Test - public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentMajorVersion() - throws NotFoundException, ApplicationException, BadRequestException { - - Entity entity = Entity.newBuilder(key).set("createdBy", "subham") - .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") - .set("status", "PUBLISHED").setKey(key).set("authority", "os1").set("source", "techlog1") - .set("entityType", "wellbore1").set("majorVersion", 1).set("minorVersion", 1024).set("patchVersion", 2) - .build(); - Entity latestEntity = getMockEntityObject(); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); - Mockito.when(queryResult.hasNext()).thenReturn(true, true, false); - Mockito.when(queryResult.next()).thenReturn(entity, latestEntity); - assertEquals(1, schemaInfoStore.getSchemaInfoList( - QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), - "test").size()); - } - - @Test - public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentMinorVersion() - throws NotFoundException, ApplicationException, BadRequestException { - - Entity entity = Entity.newBuilder(key).set("createdBy", "subham") - .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") - .set("status", "PUBLISHED").setKey(key).set("authority", "os1").set("source", "techlog1") - .set("entityType", "wellbore1").set("majorVersion", 2).set("minorVersion", 102).set("patchVersion", 2) - .build(); - Entity latestEntity = getMockEntityObject(); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); - Mockito.when(queryResult.hasNext()).thenReturn(true, true, false); - Mockito.when(queryResult.next()).thenReturn(entity, latestEntity); - assertEquals(1, schemaInfoStore.getSchemaInfoList( - QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), - "test").size()); - } - - @Test - public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentSource() - throws NotFoundException, ApplicationException, BadRequestException { - - Entity entity = Entity.newBuilder(key).set("createdBy", "subham") - .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") - .set("status", "PUBLISHED").setKey(key).set("authority", "os1").set("source", "differenttechlog1") - .set("entityType", "wellbore1").set("majorVersion", 2).set("minorVersion", 1023).set("patchVersion", 2) - .build(); - Entity latestEntity = getMockEntityObject(); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); - Mockito.when(queryResult.hasNext()).thenReturn(true, true, false); - Mockito.when(queryResult.next()).thenReturn(entity, latestEntity); - assertEquals(2, schemaInfoStore.getSchemaInfoList( - QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), - "test").size()); - } - - @Test - public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentAuthority() - throws NotFoundException, ApplicationException, BadRequestException { - - Entity entity = Entity.newBuilder(key).set("createdBy", "subham") - .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") - .set("status", "PUBLISHED").setKey(key).set("authority", "differentos1").set("source", "techlog1") - .set("entityType", "wellbore1").set("majorVersion", 2).set("minorVersion", 1023).set("patchVersion", 2) - .build(); - Entity latestEntity = getMockEntityObject(); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); - Mockito.when(queryResult.hasNext()).thenReturn(true, true, false); - Mockito.when(queryResult.next()).thenReturn(entity, latestEntity); - assertEquals(2, schemaInfoStore.getSchemaInfoList( - QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), - "test").size()); - } - - @Test - public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentEntity() - throws NotFoundException, ApplicationException, BadRequestException { - - Entity entity = Entity.newBuilder(key).set("createdBy", "subham") - .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") - .set("status", "PUBLISHED").setKey(key).set("authority", "os1").set("source", "techlog1") - .set("entityType", "differentwellbore1").set("majorVersion", 2).set("minorVersion", 1023) - .set("patchVersion", 2).build(); - Entity latestEntity = getMockEntityObject(); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).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); - Mockito.when(dataStore.run(Mockito.any())).thenReturn(queryResult); - Mockito.when(queryResult.hasNext()).thenReturn(true, true, false); - Mockito.when(queryResult.next()).thenReturn(entity, latestEntity); - assertEquals(2, schemaInfoStore.getSchemaInfoList( - QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), - "test").size()); - } - - @Test - public void testCleanSchema_Success() throws ApplicationException { - String schemaId = "schemaId"; - String dataPartitionId = "tenant1"; - when(headers.getPartitionId()).thenReturn(dataPartitionId); - Mockito.when(dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId()))) - .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); - Key key = mock(Key.class); - Mockito.when(keyFactory.newKey(schemaId)).thenReturn(key); - assertEquals(true, schemaInfoStore.cleanSchema(schemaId)); - } - - @Test - public void testCleanSchema_Failure() throws ApplicationException { - String schemaId = "schemaId"; - String dataPartitionId = "tenant1"; - when(headers.getPartitionId()).thenReturn(dataPartitionId); - Mockito.when(dataStoreFactory.getDatastore(tenantFactory.getTenantInfo(headers.getPartitionId()))) - .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); - Key key = mock(Key.class); - Mockito.when(keyFactory.newKey(schemaId)).thenReturn(key); - doThrow(DatastoreException.class).when(dataStore).delete(key); - assertEquals(false, schemaInfoStore.cleanSchema(schemaId)); - } - - private SchemaRequest getMockSchemaObject_Published() { - return SchemaRequest.builder().schema("{}") - .schemaInfo(SchemaInfo.builder() - .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") - .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) - .id("os:wks:well.1.1.1").build()) - .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).createdBy("subham").build()) - .build(); - - } - - private SchemaInfo getMockSchemaInfo() { - return SchemaInfo.builder().schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") - .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L).id("os:wks:well.1.1.1").build()) - .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).createdBy("subham") - - .build(); - - } - - private SchemaRequest getMockSchemaObject_SuperSededBy() { - return SchemaRequest.builder().schema("{}") - .schemaInfo(SchemaInfo.builder() - .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") - .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) - .id("os:wks:well.1.1.1").build()) - .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).createdBy("subham") - .supersededBy(SchemaIdentity.builder().authority("os").source("wks").entityType("well") - .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(2L) - .id("os:wks:well.1.2.1").build()) - .build()) - .build(); - - } - - private SchemaRequest getMockSchemaObject_SuperSededByWithoutId() { - return SchemaRequest.builder().schema("{}") - .schemaInfo(SchemaInfo.builder() - .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") - .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) - .id("os:wks:well.1.1.1").build()) - .scope(SchemaScope.SHARED).status(SchemaStatus.DEVELOPMENT).createdBy("subham") - .supersededBy(SchemaIdentity.builder().authority("os").source("wks").entityType("well") - .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L).build()) - .build()) - .build(); - - } - - private Entity getMockEntityObject() { - return Entity.newBuilder(key).set("createdBy", "subham") - .set("dateCreated", TimestampValue.newBuilder(Timestamp.MAX_VALUE).build()).set("scope", "INTERNAL") - .set("status", "PUBLISHED").setKey(key).set("authority", "os1").set("source", "techlog1") - .set("entityType", "wellbore1").set("majorVersion", 2).set("minorVersion", 1024).set("patchVersion", 2) - .build(); - } - -} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java deleted file mode 100644 index 8699aa70..00000000 --- a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java +++ /dev/null @@ -1,162 +0,0 @@ -package org.opengroup.osdu.schema.impl.schemainfostore; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.opengroup.osdu.core.common.model.http.DpsHeaders; -import org.opengroup.osdu.core.common.model.tenant.TenantInfo; -import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.credentials.DatastoreFactory; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.opengroup.osdu.schema.exceptions.BadRequestException; -import org.opengroup.osdu.schema.exceptions.NotFoundException; -import org.opengroup.osdu.schema.model.Source; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.google.cloud.Timestamp; -import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreException; -import com.google.cloud.datastore.Entity; -import com.google.cloud.datastore.Key; -import com.google.cloud.datastore.KeyFactory; - -@RunWith(SpringJUnit4ClassRunner.class) -public class IbmSourceStoreTest { - - @InjectMocks - IbmSourceStore sourceStore; - - @Mock - KeyFactory keyFactory; - - @Mock - Entity entity; - - @Mock - Datastore dataStore; - - @Mock - Source mockSource; - - @Mock - Key key; - - @Mock - DpsHeaders headers; - - @Mock - DatastoreFactory dataStoreFactory; - - @Mock - TenantInfo tenantInfo; - - @Mock - TenantFactory tenantFactory; - - @Test - public void testGet() throws NotFoundException, ApplicationException { - String sourceId = "sourceId"; - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.SOURCE_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey(sourceId)).thenReturn(key); - Mockito.when(dataStore.get(key)).thenReturn(entity); - Mockito.when(entity.getKey()).thenReturn(key); - assertNotNull(sourceStore.get(sourceId)); - } - - @Test - public void testGet_NotFoundException() { - String sourceId = ""; - try { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.SOURCE_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey(sourceId)).thenReturn(key); - Mockito.when(dataStore.get(key)).thenReturn(null); - sourceStore.get(sourceId); - fail("Should not succeed"); - } catch (NotFoundException e) { - assertEquals("bad input parameter", e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - - @Test - public void testCreate() throws NotFoundException, ApplicationException, BadRequestException { - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.SOURCE_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey("wks")).thenReturn(key); - Entity entity = Entity.newBuilder(key).set(SchemaConstants.DATE_CREATED, Timestamp.now()).build(); - Mockito.when(dataStore.add(entity)).thenReturn(entity); - Mockito.when(mockSource.getSourceId()).thenReturn("wks"); - assertNotNull(sourceStore.create(mockSource)); - } - - @Test - public void testCreate_BadRequestException() throws NotFoundException, ApplicationException, BadRequestException { - sourceStore = Mockito.spy(sourceStore); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.SOURCE_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey("wks")).thenReturn(key); - Mockito.when(dataStore.add(Mockito.any(Entity.class))) - .thenThrow(new DatastoreException(400, SchemaConstants.ALREADY_EXISTS, SchemaConstants.ALREADY_EXISTS)); - Mockito.when(mockSource.getSourceId()).thenReturn("wks"); - try { - sourceStore.create(mockSource); - fail("Should not succeed"); - } catch (BadRequestException e) { - assertEquals("Source already registered with Id: wks", e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - - @Test - public void testCreate_ApplicationException() throws NotFoundException, ApplicationException, BadRequestException { - sourceStore = Mockito.spy(sourceStore); - Mockito.when(headers.getPartitionId()).thenReturn("test"); - Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); - Mockito.when(dataStoreFactory.getDatastore(tenantInfo)).thenReturn(dataStore); - Mockito.when(dataStore.newKeyFactory()).thenReturn(keyFactory); - Mockito.when(keyFactory.setKind(SchemaConstants.SOURCE_KIND)).thenReturn(keyFactory); - Mockito.when(keyFactory.setNamespace(SchemaConstants.NAMESPACE)).thenReturn(keyFactory); - Mockito.when(keyFactory.newKey("wks")).thenReturn(key); - Mockito.when(dataStore.add(Mockito.any(Entity.class))).thenThrow(DatastoreException.class); - Mockito.when(mockSource.getSourceId()).thenReturn("wks"); - try { - sourceStore.create(mockSource); - fail("Should not succeed"); - } catch (ApplicationException e) { - assertEquals(SchemaConstants.INVALID_INPUT, e.getMessage()); - - } catch (Exception e) { - fail("Should not get different exception"); - } - } - -} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStoreTest.java deleted file mode 100644 index 3931b360..00000000 --- a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IbmSchemaStoreTest.java +++ /dev/null @@ -1,115 +0,0 @@ -package org.opengroup.osdu.schema.impl.schemastore; - -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.opengroup.osdu.core.common.model.http.DpsHeaders; -import org.opengroup.osdu.core.common.model.tenant.TenantInfo; -import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; -import org.opengroup.osdu.schema.constants.SchemaConstants; -import org.opengroup.osdu.schema.credentials.StorageFactory; -import org.opengroup.osdu.schema.exceptions.ApplicationException; -import org.opengroup.osdu.schema.exceptions.NotFoundException; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.google.cloud.storage.Blob; -import com.google.cloud.storage.BlobId; -import com.google.cloud.storage.BlobInfo; -import com.google.cloud.storage.Storage; -import com.google.cloud.storage.StorageException; - -@RunWith(SpringJUnit4ClassRunner.class) -public class IbmSchemaStoreTest { - - @InjectMocks - IbmSchemaStore schemaStore; - - @Mock - private StorageFactory storageFactory; - - @Mock - Storage storage; - - @Mock - Blob blob; - - @Mock - DpsHeaders headers; - - @Mock - TenantFactory tenantFactory; - - @Mock - TenantInfo TenantInfo; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private static final String BUCKET = "test-schema"; - private static final String dataPartitionId = "dataPartitionId"; - private static final String FILE_PATH = "/test-folder/test-file"; - private static final String CONTENT = "Hello World"; - - @Test - public void testCreateSchema() throws ApplicationException { - - Mockito.when(headers.getPartitionId()).thenReturn(dataPartitionId); - String filepath = FILE_PATH + SchemaConstants.JSON_EXTENSION; - BlobId blobId = Blob.newBuilder(BUCKET, filepath).build().getBlobId(); - BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); - Mockito.when(tenantFactory.getTenantInfo(dataPartitionId)).thenReturn(TenantInfo); - Mockito.when(TenantInfo.getProjectId()).thenReturn("test"); - Mockito.when(storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId))).thenReturn(storage); - Mockito.when(storage.create(blobInfo, CONTENT.getBytes())).thenReturn(blob); - Mockito.when(blob.getName()).thenReturn(BUCKET + filepath); - - String blobName = schemaStore.createSchema(FILE_PATH, CONTENT); - - Assert.assertEquals((BUCKET + filepath), blobName); - } - - @Test - public void testCreateSchema_Failure() throws ApplicationException { - expectedException.expect(ApplicationException.class); - expectedException.expectMessage(SchemaConstants.INTERNAL_SERVER_ERROR); - Mockito.when(headers.getPartitionId()).thenReturn(dataPartitionId); - String filepath = FILE_PATH + SchemaConstants.JSON_EXTENSION; - BlobId blobId = Blob.newBuilder(BUCKET, filepath).build().getBlobId(); - BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); - Mockito.when(tenantFactory.getTenantInfo(dataPartitionId)).thenReturn(TenantInfo); - Mockito.when(TenantInfo.getProjectId()).thenReturn("test"); - Mockito.when(storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId))).thenReturn(storage); - Mockito.when(storage.create(blobInfo, CONTENT.getBytes())).thenThrow(StorageException.class); - schemaStore.createSchema(FILE_PATH, CONTENT); - } - - @Test - public void testGetSchema() throws ApplicationException, NotFoundException { - String filePath = FILE_PATH + SchemaConstants.JSON_EXTENSION; - Mockito.when(tenantFactory.getTenantInfo(dataPartitionId)).thenReturn(TenantInfo); - Mockito.when(TenantInfo.getProjectId()).thenReturn("test"); - Mockito.when(storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId))).thenReturn(storage); - Mockito.when(storage.get(BUCKET, filePath)).thenReturn(blob); - Mockito.when(blob.getContent()).thenReturn(CONTENT.getBytes()); - Assert.assertEquals(CONTENT, schemaStore.getSchema(dataPartitionId, FILE_PATH)); - } - - @Test - public void testGetSchema_NotFound() throws ApplicationException, NotFoundException { - - expectedException.expect(NotFoundException.class); - expectedException.expectMessage(SchemaConstants.SCHEMA_NOT_PRESENT); - String filePath = FILE_PATH + SchemaConstants.JSON_EXTENSION; - Mockito.when(tenantFactory.getTenantInfo(dataPartitionId)).thenReturn(TenantInfo); - Mockito.when(TenantInfo.getProjectId()).thenReturn("test"); - Mockito.when(storageFactory.getStorage(tenantFactory.getTenantInfo(dataPartitionId))).thenReturn(storage); - Mockito.when(storage.get(BUCKET, filePath)).thenReturn(null); - schemaStore.getSchema(dataPartitionId, FILE_PATH); - } - -} diff --git a/provider/schema-ibm/src/test/resources/application.properties b/provider/schema-ibm/src/test/resources/application.properties deleted file mode 100644 index e69de29b..00000000 diff --git a/provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker deleted file mode 100644 index ca6ee9ce..00000000 --- a/provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker +++ /dev/null @@ -1 +0,0 @@ -mock-maker-inline \ No newline at end of file -- GitLab From 53d6e75e0bc96722abb9fbecdc8509ca6dd2235f Mon Sep 17 00:00:00 2001 From: Anuj gupta Date: Mon, 22 Jun 2020 19:40:10 +0530 Subject: [PATCH 4/8] Revert "IBM provider project created from master" This reverts commit 6ee872d1435ef496faf265daa4856b21e88e4be6. --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index fa359424..42345eb7 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,6 @@ schema-core provider/schema-gcp - provider/schema-ibm -- GitLab From fd1335f40ca02a082cee9a21e0d3149dc9b121a3 Mon Sep 17 00:00:00 2001 From: alanbraz Date: Wed, 8 Jul 2020 17:50:50 +0000 Subject: [PATCH 5/8] IBM implementation --- pom.xml | 1 + provider/schema-ibm/pom.xml | 124 ++++ .../schemainfostore/IbmAuthorityStore.java | 62 ++ .../schemainfostore/IbmEntityTypeStore.java | 64 +++ .../schemainfostore/IbmSchemaInfoStore.java | 392 +++++++++++++ .../impl/schemainfostore/IbmSourceStore.java | 65 +++ .../util/IbmDocumentStore.java | 52 ++ .../impl/schemastore/IBMSchemaStore.java | 143 +++++ .../schema/provider/ibm/AuthorityDoc.java | 56 ++ .../schema/provider/ibm/EntityTypeDoc.java | 56 ++ .../osdu/schema/provider/ibm/SchemaDoc.java | 88 +++ .../osdu/schema/provider/ibm/SourceDoc.java | 56 ++ .../ibm/app/SchemaIBMApplication.java | 36 ++ .../provider/ibm/security/SecurityConfig.java | 26 + .../ibm/security/WhoamiController.java | 40 ++ .../src/main/resources/application.properties | 22 + .../IbmAuthorityStoreTest.java | 137 +++++ .../IbmEntityTypeStoreTest.java | 144 +++++ .../IbmSchemaInfoStoreTest.java | 528 ++++++++++++++++++ .../schemainfostore/IbmSourceStoreTest.java | 134 +++++ .../impl/schemastore/IBMSchemaStoreTest.java | 101 ++++ .../src/test/resources/application.properties | 0 .../org.mockito.plugins.MockMaker | 1 + schema-core/pom.xml | 3 +- 24 files changed, 2330 insertions(+), 1 deletion(-) create mode 100644 provider/schema-ibm/pom.xml create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/util/IbmDocumentStore.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IBMSchemaStore.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/AuthorityDoc.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/EntityTypeDoc.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/SchemaDoc.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/SourceDoc.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/app/SchemaIBMApplication.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/security/SecurityConfig.java create mode 100644 provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/security/WhoamiController.java create mode 100644 provider/schema-ibm/src/main/resources/application.properties create mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java create mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java create mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java create mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java create mode 100644 provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IBMSchemaStoreTest.java create mode 100644 provider/schema-ibm/src/test/resources/application.properties create mode 100644 provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/pom.xml b/pom.xml index 42345eb7..fa359424 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ schema-core provider/schema-gcp + provider/schema-ibm diff --git a/provider/schema-ibm/pom.xml b/provider/schema-ibm/pom.xml new file mode 100644 index 00000000..f05d3933 --- /dev/null +++ b/provider/schema-ibm/pom.xml @@ -0,0 +1,124 @@ + + 4.0.0 + + org.opengroup.osdu + os-schema + 0.0.1 + ../../ + + os-schema-ibm + IBM cloud related implementation staff. + jar + + + org.opengroup.osdu.schema.provider.ibm.app.SchemaIBMApplication + 0.0.19-SNAPSHOT + + + + + + + org.opengroup.osdu + os-schema-core + 0.0.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.mockito + mockito-all + 1.10.19 + test + + + + org.opengroup.osdu + os-core-lib-ibm + ${os-core-lib-ibm.version} + + + + io.cucumber + cucumber-java8 + 5.4.0 + test + + + + io.cucumber + cucumber-junit + 5.4.0 + test + + + + + + + + ${gitlab-server} + https://community.opengroup.org/api/v4/groups/17/-/packages/maven + + + + + + ${gitlab-server} + https://community.opengroup.org/api/v4/projects/26/packages/maven + + + ${gitlab-server} + https://community.opengroup.org/api/v4/projects/26/packages/maven + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + spring-boot + ${start-class} + + + + + + + maven-war-plugin + + false + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M3 + + ${skipUnitTests} + 3 + true + -Xmx1024m -XX:MaxPermSize=256m + + + + + + + + \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java new file mode 100644 index 00000000..8517cb8f --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStore.java @@ -0,0 +1,62 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import java.net.MalformedURLException; +import java.text.MessageFormat; + +import javax.annotation.PostConstruct; + +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.impl.schemainfostore.util.IbmDocumentStore; +import org.opengroup.osdu.schema.model.Authority; +import org.opengroup.osdu.schema.provider.ibm.AuthorityDoc; +import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.IAuthorityStore; +import org.springframework.stereotype.Repository; +import org.springframework.web.context.annotation.RequestScope; + +/** + * Repository class to to register authority in IBM store. + * + */ +@Repository +@RequestScope +public class IbmAuthorityStore extends IbmDocumentStore implements IAuthorityStore { + + @PostConstruct + public void init() throws MalformedURLException { + initFactory(SchemaConstants.AUTHORITY); + } + + @Override + public Authority get(String authorityId) throws NotFoundException, ApplicationException { + + if (db.contains(authorityId)) { + AuthorityDoc sd = db.find(AuthorityDoc.class, authorityId); + return sd.getAuthority(); + } else { + throw new NotFoundException(SchemaConstants.INVALID_INPUT); + } + } + + @Override + public Authority create(Authority authority) throws ApplicationException, BadRequestException { + + if (db.contains(authority.getAuthorityId())) { + logger.warning(SchemaConstants.AUTHORITY_EXISTS_ALREADY_REGISTERED); + throw new BadRequestException( + MessageFormat.format(SchemaConstants.AUTHORITY_EXISTS_EXCEPTION, authority.getAuthorityId())); + } + AuthorityDoc sd = new AuthorityDoc(authority); + try { + db.save(sd); + } catch (Exception ex) { + logger.error(SchemaConstants.OBJECT_INVALID, ex); + throw new ApplicationException(SchemaConstants.INVALID_INPUT); + } + logger.info(SchemaConstants.AUTHORITY_CREATED); + return sd.getAuthority(); + } + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java new file mode 100644 index 00000000..8f0b2dda --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStore.java @@ -0,0 +1,64 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import java.net.MalformedURLException; +import java.text.MessageFormat; + +import javax.annotation.PostConstruct; + +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.impl.schemainfostore.util.IbmDocumentStore; +import org.opengroup.osdu.schema.model.EntityType; +import org.opengroup.osdu.schema.provider.ibm.EntityTypeDoc; +import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.IEntityTypeStore; +import org.springframework.stereotype.Repository; +import org.springframework.web.context.annotation.RequestScope; + +/** + * Repository class to register Entity type in IBM store. + * + * + */ +@Repository +@RequestScope +public class IbmEntityTypeStore extends IbmDocumentStore implements IEntityTypeStore { + + @PostConstruct + public void init() throws MalformedURLException { + initFactory(SchemaConstants.ENTITYTYPE_KIND); + } + + @Override + public EntityType get(String entityTypeId) throws NotFoundException, ApplicationException { + if (db.contains(entityTypeId)) { + EntityTypeDoc sd = db.find(EntityTypeDoc.class, entityTypeId); + return sd.getEntityType(); + } else { + throw new NotFoundException(SchemaConstants.INVALID_INPUT); + } + } + + @Override + public EntityType create(EntityType entityType) throws BadRequestException, ApplicationException { + + if (db.contains(entityType.getEntityTypeId())) { + logger.warning(SchemaConstants.ENTITY_TYPE_EXISTS); + throw new BadRequestException( + MessageFormat.format(SchemaConstants.ENTITY_TYPE_EXISTS_EXCEPTION, entityType.getEntityTypeId())); + } + EntityTypeDoc sd = new EntityTypeDoc(entityType); + + try { + db.save(sd); + } catch (Exception ex) { + logger.error(SchemaConstants.OBJECT_INVALID, ex); + throw new ApplicationException(SchemaConstants.INVALID_INPUT); + } + logger.info(SchemaConstants.ENTITY_TYPE_CREATED); + return sd.getEntityType(); + } + + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java new file mode 100644 index 00000000..88e01ba2 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStore.java @@ -0,0 +1,392 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import static com.cloudant.client.api.query.Expression.eq; +import static com.cloudant.client.api.query.Operation.and; + +import java.net.MalformedURLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeMap; +import java.util.stream.Collectors; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; + +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.impl.schemainfostore.util.IbmDocumentStore; +import org.opengroup.osdu.schema.model.QueryParams; +import org.opengroup.osdu.schema.model.SchemaIdentity; +import org.opengroup.osdu.schema.model.SchemaInfo; +import org.opengroup.osdu.schema.model.SchemaRequest; +import org.opengroup.osdu.schema.provider.ibm.SchemaDoc; +import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.ISchemaInfoStore; +import org.opengroup.osdu.schema.provider.interfaces.schemastore.ISchemaStore; +import org.opengroup.osdu.schema.util.VersionHierarchyUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; +import org.springframework.web.context.annotation.RequestScope; + +import com.cloudant.client.api.Database; +import com.cloudant.client.api.query.Expression; +import com.cloudant.client.api.query.QueryBuilder; +import com.cloudant.client.api.query.QueryResult; +import com.cloudant.client.api.query.Selector; +import com.google.gson.Gson; + +/** + * Repository class to to register Schema in IBM Document Store. + * + */ +@Repository +@RequestScope +public class IbmSchemaInfoStore extends IbmDocumentStore implements ISchemaInfoStore { + + private final static String AUTHORITY = "schemaIdentity.authority"; + private final static String SOURCE = "schemaIdentity.source"; + private final static String ENTITY_TYPE = "schemaIdentity.entityType"; + private final static String SCHEMA_VERSION_MAJOR = "schemaIdentity.schemaVersionMajor"; + private final static String SCHEMA_VERSION_MINOR = "schemaIdentity.schemaVersionMinor"; + private final static String SCHEMA_VERSION_PATCH = "schemaIdentity.schemaVersionPatch"; + private final static String SCOPE = "scope"; + private final static String STATUS = "status"; + + @Inject + private TenantFactory tenantFactory; + + @Autowired + private ISchemaStore schemaStore; + + + @PostConstruct + public void init() throws MalformedURLException { + initFactory(SCHEMA_DATABASE); + } + + + @Override + public boolean isUnique(String schemaId, String tenantId) throws ApplicationException { + Set tenantList = new HashSet<>(); + tenantList.add(SchemaConstants.ACCOUNT_ID_COMMON_PROJECT); + tenantList.add(tenantId); + + // code to call check uniqueness + if (tenantId.equalsIgnoreCase(SchemaConstants.ACCOUNT_ID_COMMON_PROJECT)) { + List privateTenantList = tenantFactory.listTenantInfo().stream().map(TenantInfo::getDataPartitionId) + .collect(Collectors.toList()); + tenantList.addAll(privateTenantList); + + } + + for (String tenant : tenantList) { + try { + if (getDatabaseForTenant(tenant, SCHEMA_DATABASE).contains(schemaId)) { + return false; // DB exists and contains schemaId, so is not unique + } + } catch (MalformedURLException e) { + // DB doesn't exist, so its unique + throw new ApplicationException("Unable to find database for tenant " + tenant); + } + } + + return true; + } + + + /** + * Method to get schemaInfo from IBM store + * + * @param schemaId + * @return schemaInfo object + * @throws ApplicationException + * @throws NotFoundException + */ + @Override + public SchemaInfo getSchemaInfo(String schemaId) throws ApplicationException, NotFoundException { + if (db.contains(schemaId)) { + SchemaDoc sd = db.find(SchemaDoc.class, schemaId); + return sd.getSchemaInfo(); + } else { + throw new NotFoundException(SchemaConstants.SCHEMA_NOT_PRESENT); + } + + } + + /** + * Method to Create schema in IBM store + * + * @param schemaRequest + * @return schemaInfo object + * @throws ApplicationException + * @throws BadRequestException + */ + @Override + public SchemaInfo createSchemaInfo(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException { + + String schemaId = schemaRequest.getSchemaInfo().getSchemaIdentity().getId(); + + if (db.contains(schemaId)) { + logger.warning("Schema " + schemaId + " already exist. Can't create again."); + throw new BadRequestException("Schema " + schemaId + " already exist. Can't create again."); + } + validateSupercededBy(schemaRequest); + SchemaDoc sd = new SchemaDoc(schemaRequest.getSchemaInfo()); + try { + Date date = new Date(); + long now = date.getTime(); + sd.setCreatedDate(now); + db.save(sd); + } catch (Exception ex) { + logger.error(SchemaConstants.OBJECT_INVALID); + throw new ApplicationException(SchemaConstants.SCHEMA_CREATION_FAILED_INVALID_OBJECT); + } + logger.info(SchemaConstants.SCHEMA_CREATED); + return sd.getSchemaInfo(); + } + + /** + * Method to update schema in IBM store + * + * @param schemaRequest + * @return schemaInfo object + * @throws ApplicationException + * @throws BadRequestException + */ + @Override + public SchemaInfo updateSchemaInfo(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException { + String schemaId = schemaRequest.getSchemaInfo().getSchemaIdentity().getId(); + validateSupercededBy(schemaRequest); + SchemaDoc sdStored = db.find(SchemaDoc.class, schemaId); + SchemaDoc sd = new SchemaDoc(schemaRequest.getSchemaInfo()); + sd.setRev(sdStored.getRev()); + + try { + db.update(sd); + } catch (Exception ex) { + logger.error(SchemaConstants.OBJECT_INVALID, ex); + throw new ApplicationException("Invalid object, updation failed"); + } + return sd.getSchemaInfo(); + } + + /** + * Method to clean schemaInfo in IBM datastore of tenantId + * + * @param schemaId + * @return status + * @throws ApplicationException + */ + @Override + public boolean cleanSchema(String schemaId) throws ApplicationException { + + try { + if (db.contains(schemaId)) { + SchemaDoc sd = db.find(SchemaDoc.class, schemaId); + db.remove(sd); + return true; + } + return false; + + } catch (Exception ex) { + logger.error(SchemaConstants.OBJECT_INVALID, ex); + return false; + } + + } + + @Override + public String getLatestMinorVerSchema(SchemaInfo schemaInfo) throws ApplicationException { + + QueryResult results = db.query(new QueryBuilder(getSelectorForMinorVerSchema(schemaInfo)).build(), + SchemaDoc.class); + TreeMap sortedMap = new TreeMap<>(Collections.reverseOrder()); + + for (SchemaDoc doc : results.getDocs()) { + sortedMap.put(doc.getSchemaInfo().getSchemaIdentity().getSchemaVersionMinor(), doc); + } + + if (sortedMap.size() != 0) { + Entry sd = sortedMap.firstEntry(); + + SchemaDoc doc = sd.getValue(); + try { + return schemaStore.getSchema(null, doc.getId()); + } catch (NotFoundException e) { + return new String(); + } + } + + return new String(); + } + + + private void validateSupercededBy(SchemaRequest schemaRequest) throws BadRequestException{ + if (schemaRequest.getSchemaInfo().getSupersededBy() != null) { + if (schemaRequest.getSchemaInfo().getSupersededBy().getId() == null + || !db.contains(schemaRequest.getSchemaInfo().getSupersededBy().getId())) { + logger.error(SchemaConstants.INVALID_SUPERSEDEDBY_ID); + throw new BadRequestException(SchemaConstants.INVALID_SUPERSEDEDBY_ID); + } + + } + } + + private Selector getSelectorForMinorVerSchema(SchemaInfo schemaInfo) { + return and( + eq(AUTHORITY, schemaInfo.getSchemaIdentity().getAuthority()), + eq(ENTITY_TYPE, schemaInfo.getSchemaIdentity().getEntityType()), + eq(SCHEMA_VERSION_MAJOR, schemaInfo.getSchemaIdentity().getSchemaVersionMajor()), + eq(SOURCE, schemaInfo.getSchemaIdentity().getSource()) + ); + } + + @Override + public List getSchemaInfoList(QueryParams queryParams, String tenantId) throws ApplicationException { + + long numRecords = LIMIT_SIZE; + if (Long.valueOf(queryParams.getLimit()) != null) { + numRecords = Long.valueOf(queryParams.getLimit()); + } + + String selectorWithoutFilter = "{ \"selector\": {} }"; + String finalQuery = null; + Selector selector = getSelector(queryParams); + + if(selector == null) + finalQuery = selectorWithoutFilter; + else + finalQuery = new QueryBuilder(getSelector(queryParams)).limit(numRecords).build(); + Database dbWithTenant = null; + try { + dbWithTenant = getDatabaseForTenant(tenantId, SCHEMA_DATABASE); + } catch (MalformedURLException e) { + throw new ApplicationException("Unable to find database for tenant " + tenantId); + } + + QueryResult results = dbWithTenant.query(finalQuery, SchemaDoc.class); + List schemaList = new LinkedList<>(); + + for (SchemaDoc doc : results.getDocs()) { + schemaList.add(doc.getSchemaInfo()); + } + + if (queryParams.getLatestVersion() != null && queryParams.getLatestVersion()) { + return getLatestVersionSchemaList(schemaList); + } + + return schemaList; + } + + private Selector getSelector(QueryParams queryParams) throws ApplicationException { + + HashMap filterHashMap = getFilters(queryParams); + + List expressionList = new ArrayList(); + + for (String filter : filterHashMap.keySet()) { + try { + expressionList.add(eq(filter, (Long) filterHashMap.get(filter))); + } catch (ClassCastException e) { + expressionList.add(eq(filter, filterHashMap.get(filter))); + } + } + + Selector finalSelector = null; + if (expressionList.size() == 1) + finalSelector = expressionList.get(0); + else if (expressionList.size() > 1) { + Expression[] expressionArray = expressionList.toArray(new Expression[0]); + finalSelector = and(expressionArray); + } + return finalSelector; + + } + + private HashMap getFilters(QueryParams queryParams) { + HashMap filterHashMap = new HashMap(); + if (queryParams.getAuthority() != null) { + filterHashMap.put(AUTHORITY, queryParams.getAuthority()); + } + if (queryParams.getSource() != null) { + filterHashMap.put(SOURCE, queryParams.getSource()); + } + if (queryParams.getEntityType() != null) { + filterHashMap.put(ENTITY_TYPE, queryParams.getEntityType()); + } + if (queryParams.getSchemaVersionMajor() != null) { + filterHashMap.put(SCHEMA_VERSION_MAJOR, + queryParams.getSchemaVersionMajor()); + } + if (queryParams.getSchemaVersionMinor() != null) { + filterHashMap.put(SCHEMA_VERSION_MINOR, + queryParams.getSchemaVersionMinor()); + } + if (queryParams.getSchemaVersionPatch() != null) { + filterHashMap.put(SCHEMA_VERSION_PATCH, queryParams.getSchemaVersionPatch()); + } + if (queryParams.getScope() != null) { + filterHashMap.put(SCOPE, queryParams.getScope()); + } + if (queryParams.getStatus() != null) { + filterHashMap.put(STATUS, queryParams.getStatus()); + } + return filterHashMap; + } + + private List getLatestVersionSchemaList(List filteredSchemaList) { + List latestSchemaList = new LinkedList<>(); + SchemaInfo previousSchemaInfo = null; + TreeMap sortedMap = new TreeMap<>( + new VersionHierarchyUtil.SortingVersionComparator()); + + for (SchemaInfo schemaInfoObject : filteredSchemaList) { + if ((previousSchemaInfo != null) && !(checkAuthorityMatch(previousSchemaInfo, schemaInfoObject) + && checkSourceMatch(previousSchemaInfo, schemaInfoObject) + && checkEntityMatch(previousSchemaInfo, schemaInfoObject))) { + Entry latestVersionEntry = sortedMap.firstEntry(); + latestSchemaList.add(latestVersionEntry.getValue()); + sortedMap.clear(); + } + previousSchemaInfo = schemaInfoObject; + SchemaIdentity schemaIdentity = schemaInfoObject.getSchemaIdentity(); + VersionHierarchyUtil version = new VersionHierarchyUtil(schemaIdentity.getSchemaVersionMajor(), + schemaIdentity.getSchemaVersionMinor(), schemaIdentity.getSchemaVersionPatch()); + sortedMap.put(version, schemaInfoObject); + } + if (sortedMap.size() != 0) { + Entry latestVersionEntry = sortedMap.firstEntry(); + latestSchemaList.add(latestVersionEntry.getValue()); + } + + return latestSchemaList; + } + + private boolean checkEntityMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) { + return schemaInfoObject.getSchemaIdentity().getEntityType() + .equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getEntityType()); + } + + private boolean checkSourceMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) { + return schemaInfoObject.getSchemaIdentity().getSource() + .equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getSource()); + } + + private boolean checkAuthorityMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) { + return schemaInfoObject.getSchemaIdentity().getAuthority() + .equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getAuthority()); + } + + + +} \ No newline at end of file diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java new file mode 100644 index 00000000..31d24a03 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStore.java @@ -0,0 +1,65 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import java.net.MalformedURLException; +import java.text.MessageFormat; + +import javax.annotation.PostConstruct; + +import org.opengroup.osdu.schema.constants.SchemaConstants; +//import org.opengroup.osdu.schema.credentials.DatastoreFactory; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.impl.schemainfostore.util.IbmDocumentStore; +import org.opengroup.osdu.schema.model.Source; +import org.opengroup.osdu.schema.provider.ibm.SourceDoc; +import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.ISourceStore; +import org.springframework.stereotype.Repository; +import org.springframework.web.context.annotation.RequestScope; + +/** + * Repository class to to register Source in Google store. + * + * + */ +@Repository +@RequestScope +public class IbmSourceStore extends IbmDocumentStore implements ISourceStore { + + @PostConstruct + public void init() throws MalformedURLException { + initFactory(SchemaConstants.SOURCE_KIND); + } + + @Override + public Source get(String sourceId) throws NotFoundException, ApplicationException { + if (db.contains(sourceId)) { + SourceDoc sd = db.find(SourceDoc.class, sourceId); + return sd.getSource(); + } else { + throw new NotFoundException(SchemaConstants.INVALID_INPUT); + } + } + + @Override + public Source create(Source source) throws BadRequestException, ApplicationException { + + if (db.contains(source.getSourceId())) { + logger.warning(SchemaConstants.SOURCE_EXISTS); + throw new BadRequestException( + MessageFormat.format(SchemaConstants.SOURCE_EXISTS_EXCEPTION, source.getSourceId())); + } + SourceDoc sd = new SourceDoc(source); + + try { + db.save(sd); + } catch (Exception ex) { + logger.error(SchemaConstants.OBJECT_INVALID, ex); + throw new ApplicationException(SchemaConstants.INVALID_INPUT); + } + logger.info(SchemaConstants.SOURCE_CREATED); + return sd.getSource(); + } + + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/util/IbmDocumentStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/util/IbmDocumentStore.java new file mode 100644 index 00000000..2711e289 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemainfostore/util/IbmDocumentStore.java @@ -0,0 +1,52 @@ +package org.opengroup.osdu.schema.impl.schemainfostore.util; + +import java.net.MalformedURLException; + +import javax.inject.Inject; + +import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.ibm.auth.ServiceCredentials; +import org.opengroup.osdu.core.ibm.cloudant.IBMCloudantClientFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.springframework.beans.factory.annotation.Value; + +import com.cloudant.client.api.Database; + +/** + * Repository class to to register authority in IBM store. + * + */ +public class IbmDocumentStore { + + public static final String SCHEMA_DATABASE = "schema2"; + public static final long LIMIT_SIZE = 25; + + @Value("${ibm.db.url}") + private String dbUrl; + @Value("${ibm.db.user:#{null}}") + private String dbUser; + @Value("${ibm.db.password:#{null}}") + private String dbPassword; + @Value("${ibm.env.prefix:local-dev}") + private String dbNamePrefix; + + @Inject + private DpsHeaders headers; + + @Inject + protected JaxRsDpsLog logger; + + private IBMCloudantClientFactory cloudantFactory; + protected Database db = null; + + public void initFactory(String dbName) throws MalformedURLException { + cloudantFactory = new IBMCloudantClientFactory(new ServiceCredentials(dbUrl, dbUser, dbPassword)); + db = cloudantFactory.getDatabase(dbNamePrefix, SchemaConstants.NAMESPACE + "-" + headers.getPartitionIdWithFallbackToAccountId() + "-" + dbName); + } + + public Database getDatabaseForTenant(String tenantId, String dbName) throws MalformedURLException { + return cloudantFactory.getDatabase(dbNamePrefix, SchemaConstants.NAMESPACE + "-" + tenantId + "-" + dbName); + } + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IBMSchemaStore.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IBMSchemaStore.java new file mode 100644 index 00000000..54485f67 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/impl/schemastore/IBMSchemaStore.java @@ -0,0 +1,143 @@ +package org.opengroup.osdu.schema.impl.schemastore; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; + +import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.ibm.objectstorage.CloudObjectStorageFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.provider.interfaces.schemastore.ISchemaStore; +import org.springframework.stereotype.Repository; +import org.springframework.web.context.annotation.RequestScope; + +import com.ibm.cloud.objectstorage.services.s3.AmazonS3; +import com.ibm.cloud.objectstorage.services.s3.model.ObjectMetadata; +import com.ibm.cloud.objectstorage.services.s3.model.PutObjectRequest; + +/** + * Repository class to to register resolved Schema in IBM storage. + * + * + */ +@Repository +@RequestScope +public class IBMSchemaStore implements ISchemaStore { + + @Inject + private DpsHeaders headers; + + @Inject + private CloudObjectStorageFactory cosFactory; + + @Inject + private JaxRsDpsLog logger; + + AmazonS3 s3Client; + + @PostConstruct + public void init() { + s3Client = cosFactory.getClient(); + logger.info("COS client initialized"); + } + + /** + * Method to get schema from IBM Storage given Tenant ProjectInfo + * + * @param dataPartitionId + * @param schemaId + * @throws NotFoundException + * @return schema object + * @throws ApplicationException + * @throws NotFoundException + */ + @Override + public String getSchema(String dataPartitionId, String schemaId) throws ApplicationException, NotFoundException { + // dataPartitionId not used b/c getting from header + + String content; + try { + content = getObjectAsString(schemaId); + } catch (Exception e) { + throw new ApplicationException(SchemaConstants.INTERNAL_SERVER_ERROR); + } + if (content != null) + return content; + throw new NotFoundException(SchemaConstants.SCHEMA_NOT_PRESENT); + + } + + /** + * Method to write schema to IBM Storage given Tenant ProjectInfo + * + * @param schemaId + * @param content + * @return schema object + * @throws ApplicationException + */ + + @Override + public String createSchema(String schemaId, String content) throws ApplicationException { + + byte[] bytes = content.getBytes(StandardCharsets.UTF_8); + int bytesSize = bytes.length; + + InputStream newStream = new ByteArrayInputStream(bytes); + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentLength(bytesSize); + + String bucket; + try { + bucket = getSchemaBucketName(); + PutObjectRequest req = new PutObjectRequest(bucket, schemaId, newStream, metadata); + s3Client.putObject(req); + + } catch (Exception e) { + throw new ApplicationException(SchemaConstants.INTERNAL_SERVER_ERROR); + } + + return s3Client.getUrl(bucket, schemaId).toString(); + } + + private String getObjectAsString(String objectName) throws Exception { + return s3Client.getObjectAsString(getSchemaBucketName(), objectName); + } + + private String getSchemaBucketName() throws Exception { + return getSchemaBucketName(headers.getPartitionIdWithFallbackToAccountId()); + } + + private String getSchemaBucketName(String dataPartitionId) throws Exception { + return cosFactory.getBucketName(dataPartitionId, "schema"); + } + + + /** + * Method to clean schema object from IBM Storage given schemaId + * + * @param schemaId + * @return boolean + * @throws ApplicationException + */ + + @Override + public boolean cleanSchemaProject(String schemaId) throws ApplicationException { + logger.info("Delete schema: " + schemaId); + try { + s3Client.deleteObject(getSchemaBucketName(), schemaId); + logger.info("Scehma deleted: " + schemaId); + return true; + } catch (Exception e) { + logger.error("Failed to delete schema " +schemaId); + return false; + } + } + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/AuthorityDoc.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/AuthorityDoc.java new file mode 100644 index 00000000..724c9d01 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/AuthorityDoc.java @@ -0,0 +1,56 @@ +/** + * Copyright 2020 IBM Corp. All Rights Reserved. + * + * 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.schema.provider.ibm; + +import org.opengroup.osdu.schema.model.Authority; + +public class AuthorityDoc { + + private String _id; + private String _rev; + private Authority authority; + + public AuthorityDoc(Authority authority) { + this.setId(authority.getAuthorityId()); + this.setAuthority(authority); + } + + public String getId() { + return _id; + } + + public void setId(String _id) { + this._id = _id; + } + + public String getRev() { + return _rev; + } + + public void setRev(String _rev) { + this._rev = _rev; + } + + public Authority getAuthority() { + return authority; + } + + public void setAuthority(Authority authority) { + this.authority = authority; + } + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/EntityTypeDoc.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/EntityTypeDoc.java new file mode 100644 index 00000000..ac546ef9 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/EntityTypeDoc.java @@ -0,0 +1,56 @@ +/** + * Copyright 2020 IBM Corp. All Rights Reserved. + * + * 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.schema.provider.ibm; + +import org.opengroup.osdu.schema.model.EntityType; + +public class EntityTypeDoc { + + private String _id; + private String _rev; + private EntityType entityType; + + public EntityTypeDoc(EntityType entityType) { + this.setId(entityType.getEntityTypeId()); + this.setEntityType(entityType); + } + + public String getId() { + return _id; + } + + public void setId(String _id) { + this._id = _id; + } + + public String getRev() { + return _rev; + } + + public void setRev(String _rev) { + this._rev = _rev; + } + + public EntityType getEntityType() { + return entityType; + } + + public void setEntityType(EntityType entityType) { + this.entityType = entityType; + } + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/SchemaDoc.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/SchemaDoc.java new file mode 100644 index 00000000..38482217 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/SchemaDoc.java @@ -0,0 +1,88 @@ +/** + * Copyright 2020 IBM Corp. All Rights Reserved. + * + * 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.schema.provider.ibm; + +import java.util.Date; + +import org.opengroup.osdu.schema.model.SchemaIdentity; +import org.opengroup.osdu.schema.model.SchemaInfo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class SchemaDoc extends SchemaInfo { + + private String _id; + private String _rev; + private long createdDate; + + public SchemaDoc(SchemaInfo schemaInfo) { + this.set_id(schemaInfo.getSchemaIdentity().getId()); + super.setCreatedBy(schemaInfo.getCreatedBy()); + if (schemaInfo.getDateCreated() != null) { + this.setCreatedDate(schemaInfo.getDateCreated().getTime()); + super.setDateCreated(null); + } + // sub objects + super.setStatus(schemaInfo.getStatus()); + schemaInfo.getSchemaIdentity().setId(null); + super.setSchemaIdentity(schemaInfo.getSchemaIdentity()); + super.setScope(schemaInfo.getScope()); + super.setSupersededBy(schemaInfo.getSupersededBy()); + } + + public SchemaInfo getSchemaInfo() { + SchemaIdentity schemaIdentity = super.getSchemaIdentity(); + schemaIdentity.setId(this._id); + + SchemaInfo info = new SchemaInfo(); + info.setSchemaIdentity(schemaIdentity); + info.setCreatedBy(super.getCreatedBy()); + info.setDateCreated(new Date(this.createdDate)); + info.setStatus(super.getStatus()); + info.setScope(super.getScope()); + info.setSupersededBy(super.getSupersededBy()); + return info; + + // return SchemaInfo(schemaIdentity, super.getCreatedBy(), (new Date(this.createdDate)), + // super.getStatus(), super.getScope(), super.getSupersededBy()); + } + + public String getRev() { + return this._rev; + } + + public void setRev(String rev) { + this._rev = rev; + } + + public String getId() { + return _id; + } + public String getKind() { + return _id; + } + + + + + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/SourceDoc.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/SourceDoc.java new file mode 100644 index 00000000..0fe08860 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/SourceDoc.java @@ -0,0 +1,56 @@ +/** + * Copyright 2020 IBM Corp. All Rights Reserved. + * + * 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.schema.provider.ibm; + +import org.opengroup.osdu.schema.model.Source; + +public class SourceDoc { + + private String _id; + private String _rev; + private Source source; + + public SourceDoc(Source source) { + this.setId(source.getSourceId()); + this.setSource(source); + } + + public String getId() { + return _id; + } + + public void setId(String _id) { + this._id = _id; + } + + public String getRev() { + return _rev; + } + + public void setRev(String _rev) { + this._rev = _rev; + } + + public Source getSource() { + return source; + } + + public void setSource(Source source) { + this.source = source; + } + +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/app/SchemaIBMApplication.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/app/SchemaIBMApplication.java new file mode 100644 index 00000000..9d283a94 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/app/SchemaIBMApplication.java @@ -0,0 +1,36 @@ +// Copyright 2020 IBM Corp. All Rights Reserved. +// +// 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.schema.provider.ibm.app; + +import javax.annotation.PostConstruct; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan({ "org.opengroup.osdu" }) +public class SchemaIBMApplication { + + @PostConstruct + void f() { + + } + + public static void main(String[] args) { + + SpringApplication.run(SchemaIBMApplication.class, args); + } +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/security/SecurityConfig.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/security/SecurityConfig.java new file mode 100644 index 00000000..6f724df0 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/security/SecurityConfig.java @@ -0,0 +1,26 @@ +package org.opengroup.osdu.schema.provider.ibm.security; + +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@EnableWebSecurity +@EnableGlobalMethodSecurity(prePostEnabled = true) +@Order(Ordered.HIGHEST_PRECEDENCE) +public class SecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + http.csrf().disable() + .authorizeRequests() + .antMatchers("/v1/api-docs", + "/configuration/ui", + "/swagger-resources/**", + "/configuration/security", + "/swagger-ui.html", + "/webjars/**").permitAll() + .anyRequest().authenticated().and().oauth2ResourceServer().jwt(); + } +} diff --git a/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/security/WhoamiController.java b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/security/WhoamiController.java new file mode 100644 index 00000000..569b1a49 --- /dev/null +++ b/provider/schema-ibm/src/main/java/org/opengroup/osdu/schema/provider/ibm/security/WhoamiController.java @@ -0,0 +1,40 @@ +package org.opengroup.osdu.schema.provider.ibm.security; +/** + * Copyright 2020 IBM Corp. All Rights Reserved. + * + * 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. + */ + + +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class WhoamiController { + @RequestMapping(value = {"/", "/whoami"}) + @ResponseBody + public String whoami() { + final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + + String userName = auth.getName(); + String roles = String.valueOf(auth.getAuthorities()); + String details = String.valueOf(auth.getPrincipal()); + + return "user: " + userName + "
" + + "roles: " + roles + "
" + + "details: " + details; + } +} diff --git a/provider/schema-ibm/src/main/resources/application.properties b/provider/schema-ibm/src/main/resources/application.properties new file mode 100644 index 00000000..83acee06 --- /dev/null +++ b/provider/schema-ibm/src/main/resources/application.properties @@ -0,0 +1,22 @@ +LOG_PREFIX=schema +server.servlet.context-path=/api/schema-service/v1 +logging.level.org.springframework.web=DEBUG +spring.security.oauth2.resourceserver.jwt.jwk-set-uri=TBD +AUTHORIZE_API=TBD +AUTHORIZE_API_KEY= + +ibm.env.prefix= +ibm.cos.endpoint_url=TODO +ibm.cos.access_key=TODO +ibm.cos.secret_key=TODO + +#couch db for dev cluster +ibm.db.url=TODO +ibm.db.user=TODO +ibm.db.password=TODO +ibm.tenant.db.url=TODO +ibm.tenant.db.user=TODO +ibm.tenant.db.password=TODO +ibm.tenant.db.name=TODO + +spring.main.allow-bean-definition-overriding=true diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java new file mode 100644 index 00000000..83e47cf5 --- /dev/null +++ b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmAuthorityStoreTest.java @@ -0,0 +1,137 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import static org.mockito.ArgumentMatchers.*; + +import java.net.MalformedURLException; + +import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.ibm.cloudant.IBMCloudantClientFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.model.Authority; +import org.opengroup.osdu.schema.provider.ibm.AuthorityDoc; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.cloudant.client.api.Database; +import com.cloudant.client.org.lightcouch.DocumentConflictException; + +@RunWith(SpringJUnit4ClassRunner.class) +public class IbmAuthorityStoreTest { + + @InjectMocks + IbmAuthorityStore mockIbmAuthorityStore; + + @Mock + protected JaxRsDpsLog logger; + + @Mock + IBMCloudantClientFactory cloudantFactory; + + @Mock + Database db; + + @Mock + Authority mockAuthority; + + @Mock + AuthorityDoc mockAuthorityDoc; + + @Mock + DpsHeaders headers; + + private static final String dataPartitionId = "testPartitionId"; + + @Test + public void testGetAuthority() throws NotFoundException, ApplicationException, MalformedURLException { + String authorityId = "testAuthorityId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(authorityId)).thenReturn(true); + Mockito.when(db.find(AuthorityDoc.class, authorityId)).thenReturn(mockAuthorityDoc); + Mockito.when(mockAuthorityDoc.getAuthority()).thenReturn(mockAuthority); + assertNotNull(mockIbmAuthorityStore.get(authorityId)); + } + + @Test + public void testGetAuthority_NotFoundException() { + String authorityId = "testAuthorityId"; + try { + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(authorityId)).thenReturn(false); + + mockIbmAuthorityStore.get(authorityId); + fail("Should not succeed"); + } catch (NotFoundException e) { + assertEquals(SchemaConstants.INVALID_INPUT, e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreateAuthority() throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(false); + Mockito.when(mockAuthorityDoc.getAuthority()).thenReturn(mockAuthority); + + assertNotNull(mockIbmAuthorityStore.create(mockAuthority)); + } + + @Test + public void testCreateAuthority_BadRequestException() + throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + String authorityId = "testAuthorityId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + mockIbmAuthorityStore = Mockito.spy(mockIbmAuthorityStore); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(true); + + Mockito.when(mockAuthority.getAuthorityId()).thenReturn(authorityId); + try { + mockIbmAuthorityStore.create(mockAuthority); + fail("Should not succeed"); + } catch (BadRequestException e) { + assertEquals("Authority already registered with Id: "+authorityId, e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreateAuthority_ApplicationException() + throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + String authorityId = "testAuthorityId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + mockIbmAuthorityStore = Mockito.spy(mockIbmAuthorityStore); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(false); + Mockito.when(db.save(any(AuthorityDoc.class))).thenThrow(DocumentConflictException.class); + Mockito.when(mockAuthority.getAuthorityId()).thenReturn(authorityId); + try { + mockIbmAuthorityStore.create(mockAuthority); + fail("Should not succeed"); + } catch (ApplicationException e) { + assertEquals(SchemaConstants.INVALID_INPUT, e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + +} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java new file mode 100644 index 00000000..fb78ff75 --- /dev/null +++ b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmEntityTypeStoreTest.java @@ -0,0 +1,144 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +//import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; + +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.mockito.Mock; +import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; +//import org.mockito.Mockito; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.ibm.cloudant.IBMCloudantClientFactory; +import org.opengroup.osdu.schema.model.EntityType; +import org.opengroup.osdu.schema.provider.ibm.EntityTypeDoc; +import org.opengroup.osdu.schema.constants.SchemaConstants; +//import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +//import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; +//import org.opengroup.osdu.schema.constants.SchemaConstants; +//import org.opengroup.osdu.schema.credentials.DatastoreFactory; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.model.Authority; +import org.opengroup.osdu.schema.provider.ibm.AuthorityDoc; + +import java.net.MalformedURLException; +//import org.opengroup.osdu.schema.model.EntityType; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.cloudant.client.api.Database; +import com.cloudant.client.org.lightcouch.DocumentConflictException; + +@RunWith(SpringJUnit4ClassRunner.class) +public class IbmEntityTypeStoreTest { +// + @InjectMocks + IbmEntityTypeStore mockIbmEntityStore; + + @Mock + IBMCloudantClientFactory cloudantFactory; + + @Mock + Database db; + + @Mock + EntityType mockEntityType; + + @Mock + EntityTypeDoc mockEntityTypeDoc; + + @Mock + DpsHeaders headers; + + @Mock + protected JaxRsDpsLog logger; + + private static final String dataPartitionId = "testPartitionId"; + + @Test + public void testGet() throws NotFoundException, ApplicationException, MalformedURLException { + String entityId = "testEntityId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(entityId)).thenReturn(true); + Mockito.when(db.find(EntityTypeDoc.class, entityId)).thenReturn(mockEntityTypeDoc); + Mockito.when(mockEntityTypeDoc.getEntityType()).thenReturn(mockEntityType); + assertNotNull(mockIbmEntityStore.get(entityId)); + } + + @Test + public void testGet_NotFoundException() { + String entityId = "testEntityId"; + try { + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(entityId)).thenReturn(false); + mockIbmEntityStore.get(entityId); + fail("Should not succeed"); + } catch (NotFoundException e) { + assertEquals(SchemaConstants.INVALID_INPUT, e.getMessage()); + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreateEntityType() throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(false); + Mockito.when(mockEntityTypeDoc.getEntityType()).thenReturn(mockEntityType); + + assertNotNull(mockIbmEntityStore.create(mockEntityType)); + } + + @Test + public void testCreate_BadRequestException() throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + String entityId = "testEntityId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + mockIbmEntityStore = Mockito.spy(mockIbmEntityStore); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(true); + Mockito.when(mockEntityType.getEntityTypeId()).thenReturn(entityId); + + try { + mockIbmEntityStore.create(mockEntityType); + fail("Should not succeed"); + } catch (BadRequestException e) { + assertEquals("EntityType already registered with Id: "+entityId, e.getMessage()); + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreate_ApplicationException() throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + String entityId = "testEntityId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + mockIbmEntityStore = Mockito.spy(mockIbmEntityStore); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(false); + Mockito.when(db.save(any(EntityTypeDoc.class))).thenThrow(DocumentConflictException.class); + Mockito.when(mockEntityType.getEntityTypeId()).thenReturn(entityId); + + try { + mockIbmEntityStore.create(mockEntityType); + fail("Should not succeed"); + } catch (ApplicationException e) { + assertEquals(SchemaConstants.INVALID_INPUT, e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + +} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java new file mode 100644 index 00000000..65a786fe --- /dev/null +++ b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSchemaInfoStoreTest.java @@ -0,0 +1,528 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.net.MalformedURLException; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +import static org.mockito.ArgumentMatchers.*; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +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.DpsHeaders; +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.ibm.cloudant.IBMCloudantClientFactory; +import org.opengroup.osdu.core.ibm.multitenancy.TenantFactory; +import org.opengroup.osdu.core.ibm.objectstorage.CloudObjectStorageFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.enums.SchemaScope; +import org.opengroup.osdu.schema.enums.SchemaStatus; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.opengroup.osdu.schema.impl.schemastore.IBMSchemaStore; +import org.opengroup.osdu.schema.model.QueryParams; +import org.opengroup.osdu.schema.model.SchemaIdentity; +import org.opengroup.osdu.schema.model.SchemaInfo; +import org.opengroup.osdu.schema.model.SchemaRequest; +import org.opengroup.osdu.schema.provider.ibm.SchemaDoc; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.cloudant.client.api.Database; +import com.cloudant.client.api.query.QueryResult; +import com.cloudant.client.org.lightcouch.DocumentConflictException; +import com.ibm.cloud.objectstorage.services.s3.AmazonS3; + + +@RunWith(SpringJUnit4ClassRunner.class) +public class IbmSchemaInfoStoreTest { + + @InjectMocks + IbmSchemaInfoStore schemaInfoStore; + + @Mock + JaxRsDpsLog logger; + + @Mock + IBMCloudantClientFactory cloudantFactory; + + @Mock + Database db; + + @Mock + SchemaDoc schemaDoc; + + @Mock + SchemaInfo schemaInfo; + + @Mock + SchemaRequest schemaRequest; + + @Mock + TenantInfo tenantInfo; + + @Mock + TenantInfo tenantInfoCommon; + + @Mock + TenantFactory tenantFactory; + + @Mock + DpsHeaders headers; + + @Mock + QueryResult queryResults; + + @Mock + QueryResult queryResult; + + @Mock + IBMSchemaStore schemaStore; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private static final String dataPartitionId = "testPartitionId"; + + + @Test + public void testGetLatestMinorVersion_ReturnNull() throws NotFoundException, ApplicationException, MalformedURLException { + String query = "{\"selector\": {\"$and\": [{\"schemaIdentity.authority\": {\"$eq\": \"os\"}}, {\"schemaIdentity.entityType\": {\"$eq\": \"well\"}}, {\"schemaIdentity.schemaVersionMajor\": {\"$eq\": 1}}, {\"schemaIdentity.source\": {\"$eq\": \"wks\"}}]}}"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + + Mockito.when(db.query(query, SchemaDoc.class)).thenReturn(queryResults); + assertEquals("", schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo())); + } + + @Test + public void testGetSchemaInfo_NotEmpty() throws NotFoundException, ApplicationException { + String schemaId = "schemaId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(db.contains(schemaId)).thenReturn(true); + Mockito.when(db.find(SchemaDoc.class, schemaId)).thenReturn(schemaDoc); + Mockito.when(schemaDoc.getSchemaInfo()).thenReturn(schemaInfo); + + SchemaInfo schemaInfo = schemaInfoStore.getSchemaInfo(schemaId); + assertNotNull(schemaInfo); + } + + @Test + public void testGetSchemaInfo_Empty() throws NotFoundException, ApplicationException { + + expectedException.expect(NotFoundException.class); + expectedException.expectMessage(SchemaConstants.SCHEMA_NOT_PRESENT); + String schemaId = "schemaId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(db.contains(schemaId)).thenReturn(false); + schemaInfoStore.getSchemaInfo(schemaId); + } + + + + @Test + public void testCreateSchemaInfo_Positive() throws ApplicationException, BadRequestException { + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(db.contains(anyString())).thenReturn(false); + Mockito.when(schemaDoc.getSchemaInfo()).thenReturn(schemaInfo); + assertNotNull(schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published())); + } + + + @Test + public void testIsUnique_True() throws ApplicationException, MalformedURLException { + + String schemaId = "schemaId"; + String tenantId = "tenant"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(false); + assertTrue(schemaInfoStore.isUnique(schemaId, tenantId)); + } + + @Test + public void testIsUnique_False() throws ApplicationException, MalformedURLException { + String schemaId = "schemaId"; + String tenantId = "tenant"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(true); + assertFalse(schemaInfoStore.isUnique(schemaId, tenantId)); + } + + @Test + public void testIsUnique_False_CommomTenant() throws ApplicationException, MalformedURLException { + String schemaId = "schemaId"; + String tenantId = "common"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(tenantFactory.getTenantInfo("tenant")).thenReturn(tenantInfo); + Mockito.when(tenantFactory.getTenantInfo("common")).thenReturn(tenantInfoCommon); + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(true); + assertFalse(schemaInfoStore.isUnique(schemaId, tenantId)); + } + + + @Test + public void testUpdateSchemaInfo() throws NotFoundException, ApplicationException, BadRequestException { + String schemaId = "os:wks:well.1.1.1"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(db.find(SchemaDoc.class, schemaId)).thenReturn(schemaDoc); + Mockito.when(schemaDoc.getSchemaInfo()).thenReturn(schemaInfo); + assertNotNull(schemaInfoStore.updateSchemaInfo(getMockSchemaObject_Published())); + } + + @Test + public void testCreateSchemaInfo_WithSupersededBy() + throws NotFoundException, ApplicationException, BadRequestException { + String schemaId = "os:wks:well.1.1.1"; + String superSededId = "os:wks:well.1.2.1"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(db.contains(schemaId)).thenReturn(false); + Mockito.when(db.contains(superSededId)).thenReturn(true); + Mockito.when(schemaDoc.getSchemaInfo()).thenReturn(schemaInfo); + assertNotNull(schemaInfoStore.createSchemaInfo(getMockSchemaObject_SuperSededBy())); + } + + @Test + public void testUpdateSchemaInfo_SupersededByException() + throws NotFoundException, ApplicationException, BadRequestException { + try { + + String schemaId = "os:wks:well.1.1.1"; + String superSededId = "os:wks:well.1.2.1"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(db.find(SchemaDoc.class, schemaId)).thenReturn(schemaDoc); + Mockito.when(schemaDoc.getSchemaInfo()).thenReturn(schemaInfo); + Mockito.when(db.contains(superSededId)).thenReturn(false); + + schemaInfoStore.updateSchemaInfo(getMockSchemaObject_SuperSededBy()); + fail("Should not succeed"); + } catch (BadRequestException e) { + assertEquals("Invalid SuperSededBy id", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testUpdateInfo_SupersededByWithoutIdException() + throws NotFoundException, ApplicationException, BadRequestException { + String schemaId = "os:wks:well.1.1.1"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(db.find(SchemaDoc.class, schemaId)).thenReturn(schemaDoc); + Mockito.when(schemaDoc.getSchemaInfo()).thenReturn(schemaInfo); + + SchemaRequest schemaRequest = getMockSchemaObject_SuperSededByWithoutId(); + expectedException.expect(BadRequestException.class); + expectedException.expectMessage(SchemaConstants.INVALID_SUPERSEDEDBY_ID); + schemaInfoStore.updateSchemaInfo(schemaRequest); + } + + @Test + public void testCreateSchemaInfo_BadRequestException() + throws NotFoundException, ApplicationException, BadRequestException { + + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(db.contains(anyString())).thenReturn(true); + Mockito.when(schemaDoc.getSchemaInfo()).thenReturn(schemaInfo); + + try { + schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published()); + fail("Should not succeed"); + } catch (BadRequestException e) { + assertEquals("Schema os:wks:well.1.1.1 already exist. Can't create again.", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreateSchemaInfo_ApplicationException() + throws NotFoundException, ApplicationException, BadRequestException { + + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(db.contains(anyString())).thenReturn(false); + Mockito.when(schemaDoc.getSchemaInfo()).thenReturn(schemaInfo); + Mockito.when(db.save(any(SchemaDoc.class))).thenThrow(DocumentConflictException.class); + + try { + schemaInfoStore.createSchemaInfo(getMockSchemaObject_Published()); + fail("Should not succeed"); + } catch (ApplicationException e) { + assertEquals(SchemaConstants.SCHEMA_CREATION_FAILED_INVALID_OBJECT, e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testUpdateSchemaInfo_ApplicationException() + throws NotFoundException, ApplicationException, BadRequestException { + + String schemaId = "os:wks:well.1.1.1"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(tenantFactory.getTenantInfo("test")).thenReturn(tenantInfo); + Mockito.when(db.find(SchemaDoc.class, schemaId)).thenReturn(schemaDoc); + Mockito.when(schemaDoc.getSchemaInfo()).thenReturn(schemaInfo); + Mockito.when(db.update(any(SchemaDoc.class))).thenThrow(DocumentConflictException.class); + + try { + schemaInfoStore.updateSchemaInfo(getMockSchemaObject_Published()); + fail("Should not succeed"); + } catch (ApplicationException e) { + assertEquals("Invalid object, updation failed", e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testGetLatestMinorVersion_Entity() throws NotFoundException, ApplicationException { + List schemaDocsList = new LinkedList<>(); + schemaDocsList.add(getMockSchemaDocObject()); + Mockito.when(db.query(Mockito.any(), Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.getDocs()).thenReturn(schemaDocsList); + Mockito.when(schemaStore.getSchema(any(), anyString())).thenReturn("{}"); + assertEquals("{}", schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo())); + } + + @Test + public void testGetSchemaInfoList_withoutqueryparam() + throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + List schemaDocsList = new LinkedList<>(); + schemaDocsList.add(getMockSchemaDocObject()); + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.query(Mockito.any(),Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.getDocs()).thenReturn(schemaDocsList); + assertEquals(1, + schemaInfoStore.getSchemaInfoList(QueryParams.builder().limit(100).offset(0).build(), "test").size()); + } + + @Test + public void testGetSchemaInfoList_withqueryparam() + throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + List schemaDocsList = new LinkedList<>(); + schemaDocsList.add(getMockSchemaDocObject()); + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.query(Mockito.any(),Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.getDocs()).thenReturn(schemaDocsList); + assertEquals(1, + schemaInfoStore.getSchemaInfoList(QueryParams.builder().authority("test").source("test").entityType("test") + .schemaVersionMajor(1l).schemaVersionMinor(1l).scope("test").status("test").latestVersion(false) + .limit(100).offset(0).build(), "test").size()); + } + + @Test + public void testGetSchemaInfoList_latestVersionTrue_NoSchemaMatchScenario() + throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.query(Mockito.any(),Mockito.any())).thenReturn(queryResult); + assertEquals(0, + schemaInfoStore + .getSchemaInfoList(QueryParams.builder().authority("test").source("test").entityType("test") + .scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), "test") + .size()); + } + + @Test + public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentMajorVersion() + throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + + SchemaIdentity schemaIdentity = new SchemaIdentity("authority","source","entityType",2L,11L,111L,"id"); + SchemaInfo schemaInfo = new SchemaInfo(schemaIdentity,"createdBy", new Date(), SchemaStatus.DEVELOPMENT, SchemaScope.INTERNAL, schemaIdentity); + SchemaRequest schemaRequest = new SchemaRequest(schemaInfo, new Object()); + SchemaDoc schemaDoc = new SchemaDoc(schemaRequest.getSchemaInfo()); + SchemaDoc latestSchemaDoc = getMockSchemaDocObject(); + List schemaDocsList = new LinkedList<>(); + schemaDocsList.add(schemaDoc); + schemaDocsList.add(latestSchemaDoc); + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.query(Mockito.any(),Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.getDocs()).thenReturn(schemaDocsList); + assertEquals(1, schemaInfoStore.getSchemaInfoList( + QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), + "test").size()); + } + + @Test + public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentMinorVersion() + throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + SchemaIdentity schemaIdentity = new SchemaIdentity("authority","source","entityType",1L,22L,111L,"id"); + SchemaInfo schemaInfo = new SchemaInfo(schemaIdentity,"createdBy", new Date(), SchemaStatus.DEVELOPMENT, SchemaScope.INTERNAL, schemaIdentity); + SchemaRequest schemaRequest = new SchemaRequest(schemaInfo, new Object()); + SchemaDoc schemaDoc = new SchemaDoc(schemaRequest.getSchemaInfo()); + SchemaDoc latestSchemaDoc = getMockSchemaDocObject(); + List schemaDocsList = new LinkedList<>(); + schemaDocsList.add(schemaDoc); + schemaDocsList.add(latestSchemaDoc); + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.query(Mockito.any(),Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.getDocs()).thenReturn(schemaDocsList); + assertEquals(1, schemaInfoStore.getSchemaInfoList( + QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), + "test").size()); + } + + @Test + public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentSource() + throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + + SchemaIdentity schemaIdentity = new SchemaIdentity("authority","sourceChanged","entityType",1L,11L,111L,"id"); + SchemaInfo schemaInfo = new SchemaInfo(schemaIdentity,"createdBy", new Date(), SchemaStatus.DEVELOPMENT, SchemaScope.INTERNAL, schemaIdentity); + SchemaRequest schemaRequest = new SchemaRequest(schemaInfo, new Object()); + SchemaDoc schemaDoc = new SchemaDoc(schemaRequest.getSchemaInfo()); + SchemaDoc latestSchemaDoc = getMockSchemaDocObject(); + List schemaDocsList = new LinkedList<>(); + schemaDocsList.add(schemaDoc); + schemaDocsList.add(latestSchemaDoc); + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.query(Mockito.any(),Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.getDocs()).thenReturn(schemaDocsList); + + assertEquals(2, schemaInfoStore.getSchemaInfoList( + QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), + "test").size()); + } + + @Test + public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentAuthority() + throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + + SchemaIdentity schemaIdentity = new SchemaIdentity("authorityChanged","source","entityType",1L,11L,111L,"id"); + SchemaInfo schemaInfo = new SchemaInfo(schemaIdentity,"createdBy", new Date(), SchemaStatus.DEVELOPMENT, SchemaScope.INTERNAL, schemaIdentity); + SchemaRequest schemaRequest = new SchemaRequest(schemaInfo, new Object()); + SchemaDoc schemaDoc = new SchemaDoc(schemaRequest.getSchemaInfo()); + SchemaDoc latestSchemaDoc = getMockSchemaDocObject(); + List schemaDocsList = new LinkedList<>(); + schemaDocsList.add(schemaDoc); + schemaDocsList.add(latestSchemaDoc); + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.query(Mockito.any(),Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.getDocs()).thenReturn(schemaDocsList); + + assertEquals(2, schemaInfoStore.getSchemaInfoList( + QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), + "test").size()); + } + + @Test + public void testGetSchemaInfoList_LatestVersionFunctionalityTest_SchemasWithDifferentEntity() + throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + SchemaIdentity schemaIdentity = new SchemaIdentity("authority","source","entityTypeChanged",1L,11L,111L,"id"); + SchemaInfo schemaInfo = new SchemaInfo(schemaIdentity,"createdBy", new Date(), SchemaStatus.DEVELOPMENT, SchemaScope.INTERNAL, schemaIdentity); + SchemaRequest schemaRequest = new SchemaRequest(schemaInfo, new Object()); + + + SchemaDoc schemaDoc = new SchemaDoc(schemaRequest.getSchemaInfo()); + SchemaDoc latestSchemaDoc = getMockSchemaDocObject(); + List schemaDocsList = new LinkedList<>(); + schemaDocsList.add(schemaDoc); + schemaDocsList.add(latestSchemaDoc); + Mockito.when(cloudantFactory.getDatabase(any(),anyString())).thenReturn(db); + Mockito.when(db.query(Mockito.any(),Mockito.any())).thenReturn(queryResult); + Mockito.when(queryResult.getDocs()).thenReturn(schemaDocsList); + + assertEquals(2, schemaInfoStore.getSchemaInfoList( + QueryParams.builder().scope("test").status("test").latestVersion(true).limit(100).offset(0).build(), + "test").size()); + } + + + @Test + public void testCleanSchema_Success() throws ApplicationException, MalformedURLException { + String schemaId = "schemaId"; + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(schemaId)).thenReturn(true); + Mockito.when(db.find(SchemaDoc.class, schemaId)).thenReturn(schemaDoc); + assertEquals(true, schemaInfoStore.cleanSchema(schemaId)); + } + + @Test + public void testCleanSchema_Failure() throws ApplicationException, MalformedURLException { + String schemaId = "schemaId"; + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(schemaId)).thenReturn(false); + Mockito.when(db.find(SchemaDoc.class, schemaId)).thenReturn(schemaDoc); + assertEquals(false, schemaInfoStore.cleanSchema(schemaId)); + } + + private SchemaRequest getMockSchemaObject_Published() { + return SchemaRequest.builder().schema("{}") + .schemaInfo(SchemaInfo.builder() + .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) + .id("os:wks:well.1.1.1").build()) + .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).createdBy("ibm").build()) + .build(); + + } + + private SchemaInfo getMockSchemaInfo() { + return SchemaInfo.builder().schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L).id("os:wks:well.1.1.1").build()) + .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).createdBy("ibm") + + .build(); + + } + + private SchemaRequest getMockSchemaObject_SuperSededBy() { + return SchemaRequest.builder().schema("{}") + .schemaInfo(SchemaInfo.builder() + .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) + .id("os:wks:well.1.1.1").build()) + .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).createdBy("ibm") + .supersededBy(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(2L) + .id("os:wks:well.1.2.1").build()) + .build()) + .build(); + + } + + private SchemaRequest getMockSchemaObject_SuperSededByWithoutId() { + return SchemaRequest.builder().schema("{}") + .schemaInfo(SchemaInfo.builder() + .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) + .id("os:wks:well.1.1.1").build()) + .scope(SchemaScope.SHARED).status(SchemaStatus.DEVELOPMENT).createdBy("ibm") + .supersededBy(SchemaIdentity.builder().authority("os").source("wks").entityType("well") + .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L).build()) + .build()) + .build(); + + } + + private SchemaDoc getMockSchemaDocObject() + { + SchemaIdentity schemaIdentity = new SchemaIdentity("authority","source","entityType",1L,11L,111L,"id"); + SchemaInfo schemaInfo = new SchemaInfo(schemaIdentity,"createdBy", new Date(), SchemaStatus.DEVELOPMENT, SchemaScope.INTERNAL, schemaIdentity); + SchemaRequest schemaRequest = new SchemaRequest(schemaInfo, new Object()); + SchemaDoc schemaDoc = new SchemaDoc(schemaRequest.getSchemaInfo()); + + return schemaDoc; + } + +} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java new file mode 100644 index 00000000..dd9a67c1 --- /dev/null +++ b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemainfostore/IbmSourceStoreTest.java @@ -0,0 +1,134 @@ +package org.opengroup.osdu.schema.impl.schemainfostore; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.ibm.cloudant.IBMCloudantClientFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.BadRequestException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import java.net.MalformedURLException; +import org.opengroup.osdu.schema.model.Source; +import org.opengroup.osdu.schema.provider.ibm.SourceDoc; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +import com.cloudant.client.api.Database; +import com.cloudant.client.org.lightcouch.DocumentConflictException; + +@RunWith(SpringJUnit4ClassRunner.class) +public class IbmSourceStoreTest { + + @InjectMocks + IbmSourceStore sourceStore; + + @Mock + IBMCloudantClientFactory cloudantFactory; + + @Mock + Database db; + + @Mock + Source mockSource; + + @Mock + SourceDoc mockSourceDoc; + + + @Mock + DpsHeaders headers; + + @Mock + protected JaxRsDpsLog logger; + + private static final String dataPartitionId = "testPartitionId"; + + @Test + public void testGet() throws NotFoundException, ApplicationException, MalformedURLException { + String sourceId = "testSourceId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(sourceId)).thenReturn(true); + Mockito.when(db.find(SourceDoc.class, sourceId)).thenReturn(mockSourceDoc); + Mockito.when(mockSourceDoc.getSource()).thenReturn(mockSource); + assertNotNull(sourceStore.get(sourceId)); + } + + @Test + public void testGet_NotFoundException() { + String sourceId = "testSourceId"; + try { + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(sourceId)).thenReturn(false); + sourceStore.get(sourceId); + fail("Should not succeed"); + } catch (NotFoundException e) { + assertEquals(SchemaConstants.INVALID_INPUT, e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreateSource() throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(false); + Mockito.when(mockSourceDoc.getSource()).thenReturn(mockSource); + assertNotNull(sourceStore.create(mockSource)); + } + + @Test + public void testCreate_BadRequestException() throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + String sourceId = "testSourceId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + sourceStore = Mockito.spy(sourceStore); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(true); + Mockito.when(mockSource.getSourceId()).thenReturn(sourceId); + + try { + sourceStore.create(mockSource); + fail("Should not succeed"); + } catch (BadRequestException e) { + assertEquals("Source already registered with Id: "+sourceId, e.getMessage()); + } catch (Exception e) { + fail("Should not get different exception"); + } + } + + @Test + public void testCreate_ApplicationException() throws NotFoundException, ApplicationException, BadRequestException, MalformedURLException { + String sourceId = "testSourceId"; + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + sourceStore = Mockito.spy(sourceStore); + Mockito.when(cloudantFactory.getDatabase(anyString(),anyString())).thenReturn(db); + Mockito.when(db.contains(anyString())).thenReturn(false); + Mockito.when(db.save(any(SourceDoc.class))).thenThrow(DocumentConflictException.class); + Mockito.when(mockSource.getSourceId()).thenReturn(sourceId); + + try { + sourceStore.create(mockSource); + fail("Should not succeed"); + } catch (ApplicationException e) { + assertEquals(SchemaConstants.INVALID_INPUT, e.getMessage()); + + } catch (Exception e) { + fail("Should not get different exception"); + } + } + +} diff --git a/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IBMSchemaStoreTest.java b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IBMSchemaStoreTest.java new file mode 100644 index 00000000..3ff75701 --- /dev/null +++ b/provider/schema-ibm/src/test/java/org/opengroup/osdu/schema/impl/schemastore/IBMSchemaStoreTest.java @@ -0,0 +1,101 @@ +package org.opengroup.osdu.schema.impl.schemastore; + + +import static org.mockito.ArgumentMatchers.*; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; + +import org.opengroup.osdu.core.ibm.objectstorage.CloudObjectStorageFactory; +import org.opengroup.osdu.schema.constants.SchemaConstants; +import org.opengroup.osdu.schema.exceptions.ApplicationException; +import org.opengroup.osdu.schema.exceptions.NotFoundException; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.ibm.cloud.objectstorage.AmazonServiceException; +import com.ibm.cloud.objectstorage.services.s3.AmazonS3; + +import java.net.MalformedURLException; +import java.net.URL; + +@RunWith(SpringJUnit4ClassRunner.class) +public class IBMSchemaStoreTest { + + @InjectMocks + IBMSchemaStore schemaStore; + + @Mock + DpsHeaders headers; + + @Mock + CloudObjectStorageFactory cosFactory; + + @Mock + AmazonS3 s3Client; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private static final String BUCKET = "schema"; + private static final String dataPartitionId = "testPartitionId"; + private static final String SCHEMA_ID = "test-schema-id"; + private static final String BUCKET_NAME_PREFIX = "sandy-local-dev"; + + private static final String CONTENT = "Hello World"; + + @Test + public void testCreateSchema() throws ApplicationException, MalformedURLException { + String bucketName = String.format("%s-dataecosystem-%s-%s", BUCKET_NAME_PREFIX, dataPartitionId, BUCKET); + URL url = new URL("http://schema/" + SCHEMA_ID); + + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cosFactory.getClient()).thenReturn(s3Client); + Mockito.when(cosFactory.getBucketName(dataPartitionId, BUCKET)).thenReturn(bucketName); + Mockito.when(s3Client.getUrl(bucketName, SCHEMA_ID)).thenReturn(url); + + String objectURL = schemaStore.createSchema(SCHEMA_ID, CONTENT); + Assert.assertEquals(("http://schema/" + SCHEMA_ID), objectURL); + } + + @Test + public void testCreateSchema_Failure() throws ApplicationException, MalformedURLException { + expectedException.expect(ApplicationException.class); + expectedException.expectMessage(SchemaConstants.INTERNAL_SERVER_ERROR); + + String bucketName = String.format("%s-dataecosystem-%s-%s", BUCKET_NAME_PREFIX, dataPartitionId, BUCKET); + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cosFactory.getClient()).thenReturn(s3Client); + Mockito.when(cosFactory.getBucketName(dataPartitionId, BUCKET)).thenReturn(bucketName); + Mockito.when(s3Client.putObject(any())).thenThrow(AmazonServiceException.class); + schemaStore.createSchema(SCHEMA_ID, CONTENT); + } + + @Test + public void testGetSchema() throws ApplicationException, NotFoundException { + String bucketName = String.format("%s-dataecosystem-%s-%s", BUCKET_NAME_PREFIX, dataPartitionId, BUCKET); + + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(cosFactory.getClient()).thenReturn(s3Client); + Mockito.when(cosFactory.getBucketName(dataPartitionId, BUCKET)).thenReturn(bucketName); + Mockito.when(s3Client.getObjectAsString(bucketName, SCHEMA_ID)).thenReturn(CONTENT); + + Assert.assertEquals(CONTENT, schemaStore.getSchema(dataPartitionId, SCHEMA_ID)); + } + + @Test + public void testGetSchema_NotFound() throws ApplicationException, NotFoundException { + expectedException.expect(NotFoundException.class); + expectedException.expectMessage(SchemaConstants.SCHEMA_NOT_PRESENT); + Mockito.when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn(dataPartitionId); + Mockito.when(s3Client.getObjectAsString(dataPartitionId, SCHEMA_ID)).thenReturn(null); + schemaStore.getSchema(dataPartitionId, SCHEMA_ID); + } + +} diff --git a/provider/schema-ibm/src/test/resources/application.properties b/provider/schema-ibm/src/test/resources/application.properties new file mode 100644 index 00000000..e69de29b diff --git a/provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 00000000..ca6ee9ce --- /dev/null +++ b/provider/schema-ibm/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline \ No newline at end of file diff --git a/schema-core/pom.xml b/schema-core/pom.xml index a1215945..82beefba 100644 --- a/schema-core/pom.xml +++ b/schema-core/pom.xml @@ -15,6 +15,7 @@ 2.7.0 + 0.0.18 @@ -89,7 +90,7 @@ org.opengroup.osdu os-core-common - 0.0.11 + ${os-core-common.version} -- GitLab From ff8b1454d616b75b5b33c9d99b85672814fdfc1a Mon Sep 17 00:00:00 2001 From: Alan Braz Date: Wed, 8 Jul 2020 15:03:40 -0300 Subject: [PATCH 6/8] added ibm --- .gitlab-ci.yml | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6a24a6a8..40bac5b4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,6 +8,9 @@ variables: GCP_DEPLOY_ENV: p4d GCP_DOMAIN: cloud.slb-ds.com + IBM_BUILD_SUBDIR: provider/schema-ibm + #IBM_INT_TEST_SUBDIR: testing/schema-test-ibm + include: - project: "osdu/platform/ci-cd-pipelines" file: "standard-setup.yml" @@ -15,9 +18,20 @@ include: - project: "osdu/platform/ci-cd-pipelines" file: "build/maven.yml" - - project: "osdu/platform/ci-cd-pipelines" - file: "scanners/fossa.yml" + # - project: "osdu/platform/ci-cd-pipelines" + # file: "scanners/fossa.yml" + + # - project: "osdu/platform/ci-cd-pipelines" + # file: "cloud-providers/gcp.yml" + + # - project: 'osdu/platform/ci-cd-pipelines' + # file: 'cloud-providers/aws.yml' - project: "osdu/platform/ci-cd-pipelines" - file: "scanners/gitlab-ultimate.yml" - \ No newline at end of file + file: "cloud-providers/ibm.yml" + + # - project: "osdu/platform/ci-cd-pipelines" + # file: "scanners/gitlab-ultimate.yml" + + # - project: "osdu/platform/ci-cd-pipelines" + # file: "cloud-providers/azure.yml" -- GitLab From 9c0c76180ce69c7c1332b5cb1f0c871a4db2f508 Mon Sep 17 00:00:00 2001 From: Alan Braz Date: Wed, 8 Jul 2020 15:04:51 -0300 Subject: [PATCH 7/8] ignore local props --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 83e0b5aa..cc703377 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ target/ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +*local.properties -- GitLab