Commit 63b584e6 authored by Nikita Ruzhevsky's avatar Nikita Ruzhevsky
Browse files

changed behaviour - instead of throwing exceptions, now we return null

parent 072a9db7
Pipeline #62344 failed with stage
in 10 seconds
......@@ -5,8 +5,11 @@ import org.opengroup.osdu.core.mongodb.schema.dto.SchemaInfoDto;
import org.opengroup.osdu.core.mongodb.schema.entity.EntityId;
import org.opengroup.osdu.core.mongodb.schema.entity.SchemaIdentityDoc;
import org.opengroup.osdu.core.mongodb.schema.entity.SchemaInfoDoc;
import org.opengroup.osdu.core.mongodb.schema.util.VersionUtils;
import org.springframework.stereotype.Component;
import java.util.Optional;
@SuppressWarnings("DuplicatedCode")
@Component
public class SchemaInfoConverter {
......@@ -19,7 +22,7 @@ public class SchemaInfoConverter {
dto.setDateCreated(doc.getDateCreated());
dto.setScope(doc.getScope());
dto.setStatus(doc.getStatus());
dto.setSupersededBy(idToDto(doc.getSupersededBy()));
Optional.ofNullable(doc.getSupersededBy()).map(this::idToDto).ifPresent(dto::setSupersededBy);
return dto;
}
......@@ -29,13 +32,14 @@ public class SchemaInfoConverter {
doc.setId(new EntityId(dto.getSchemaIdentity().getId(), tenant));
doc.setTenant(tenant);
doc.setKind(VersionUtils.getKindFromId(dto.getSchemaIdentity().getId()));
doc.setSchemaIdentity(idToDoc(dto.getSchemaIdentity()));
doc.setCreatedBy(dto.getCreatedBy());
doc.setDateCreated(dto.getDateCreated());
doc.setScope(dto.getScope());
doc.setStatus(dto.getStatus());
doc.setSupersededBy(idToDoc(dto.getSupersededBy()));
Optional.ofNullable(dto.getSupersededBy()).map(this::idToDoc).ifPresent(doc::setSupersededBy);
return doc;
}
......
......@@ -19,4 +19,22 @@ public class QueryParamsDto {
private String scope;
private Boolean latestVersion;
public QueryParamsDto() {
}
public QueryParamsDto(String authority, String source, String entityType, Long schemaVersionMajor,
Long schemaVersionMinor, Long schemaVersionPatch, int limit, int offset, String status,
String scope, Boolean latestVersion) {
this.authority = authority;
this.source = source;
this.entityType = entityType;
this.schemaVersionMajor = schemaVersionMajor;
this.schemaVersionMinor = schemaVersionMinor;
this.schemaVersionPatch = schemaVersionPatch;
this.limit = limit;
this.offset = offset;
this.status = status;
this.scope = scope;
this.latestVersion = latestVersion;
}
}
......@@ -12,6 +12,8 @@ public class SchemaInfoDoc {
private EntityId id;
// this is used in getSchemaInfoList method
private String tenant;
// this field is used to filter entity by it's kind
private String kind;
private SchemaIdentityDoc schemaIdentity;
private String createdBy;
......
......@@ -7,6 +7,9 @@ import org.opengroup.osdu.core.mongodb.schema.entity.EntityId;
import org.opengroup.osdu.core.mongodb.schema.util.ExceptionGenerator;
import org.springframework.stereotype.Component;
import javax.annotation.Nullable;
import java.util.Optional;
@Component
public class AuthorityStoreHelper extends SchemaStoreHelperParent {
......@@ -19,20 +22,29 @@ public class AuthorityStoreHelper extends SchemaStoreHelperParent {
this.authorityConverter = authorityConverter;
}
/**
* Gets authority record
*
* @return authority by requested params or null if not found
*/
@Nullable
public AuthorityDto get(String authorityId, String tenant) {
EntityId id = new EntityId(authorityId, tenant);
AuthorityDoc doc = mongoDBHelper.get(ID, id, CLASS);
if (doc == null) {
throw ExceptionGenerator.notFound(String.format("%s %s", authorityId, tenant));
}
return authorityConverter.toDto(doc);
return Optional.ofNullable(doc).map(authorityConverter::toDto).orElse(null);
}
public AuthorityDto create(AuthorityDto authorityDto, String tenant) {
/**
* Creates an authority
*
* @return created authority
* @throws IllegalArgumentException if authority already exists
*/
public AuthorityDto create(AuthorityDto authorityDto, String tenant) throws IllegalArgumentException {
AuthorityDoc doc = authorityConverter.toDoc(authorityDto, tenant);
EntityId id = doc.getId();
if (mongoDBHelper.existsByField(ID, id, CLASS)) {
throw ExceptionGenerator.alreadyExists(String.format("%s %s", id, tenant));
throw ExceptionGenerator.alreadyExists(String.format("id=%s, tenant=%s", id, tenant));
}
mongoDBHelper.save(doc);
return authorityConverter.toDto(doc);
......
......@@ -7,34 +7,44 @@ import org.opengroup.osdu.core.mongodb.schema.entity.EntityTypeDoc;
import org.opengroup.osdu.core.mongodb.schema.util.ExceptionGenerator;
import org.springframework.stereotype.Component;
import javax.annotation.Nullable;
import java.util.Optional;
@Component
public class EntityTypeStoreHelper extends SchemaStoreHelperParent {
private static final String ID = "_id";
private static final Class<EntityTypeDoc> CLASS = EntityTypeDoc.class;
private final EntityTypeConverter converter;
private final EntityTypeConverter entityTypeConverter;
public EntityTypeStoreHelper(EntityTypeConverter converter) {
this.converter = converter;
public EntityTypeStoreHelper(EntityTypeConverter entityTypeConverter) {
this.entityTypeConverter = entityTypeConverter;
}
/**
* Gets entity type record
* @return entity type by requested params or null if not found
*/
@Nullable
public EntityTypeDto get(String entityId, String tenant) {
EntityId id = new EntityId(entityId, tenant);
EntityTypeDoc doc = mongoDBHelper.get(ID, id, CLASS);
if (doc == null) {
throw ExceptionGenerator.notFound(String.format("%s %s", entityId, tenant));
}
return converter.toDto(doc);
return Optional.ofNullable(doc).map(entityTypeConverter::toDto).orElse(null);
}
public EntityTypeDto create(EntityTypeDto dto, String tenant) {
EntityTypeDoc doc = converter.toDoc(dto, tenant);
/**
* Creates an entity type
* @throws IllegalArgumentException if entity type already exists
* @return created entity type
*/
public EntityTypeDto create(EntityTypeDto dto, String tenant) throws IllegalArgumentException {
EntityTypeDoc doc = entityTypeConverter.toDoc(dto, tenant);
EntityId id = doc.getId();
if (mongoDBHelper.existsByField(ID, id, CLASS)) {
throw ExceptionGenerator.alreadyExists(String.format("id=%s, tenant=%s", dto, tenant));
throw ExceptionGenerator.alreadyExists(String.format("id=%s, tenant=%s", id, tenant));
}
mongoDBHelper.save(doc);
return converter.toDto(doc);
return entityTypeConverter.toDto(doc);
}
}
package org.opengroup.osdu.core.mongodb.schema.helper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.opengroup.osdu.core.mongodb.schema.converter.SchemaInfoConverter;
import org.opengroup.osdu.core.mongodb.schema.dto.QueryParamsDto;
......@@ -8,16 +7,18 @@ import org.opengroup.osdu.core.mongodb.schema.dto.SchemaInfoDto;
import org.opengroup.osdu.core.mongodb.schema.entity.EntityId;
import org.opengroup.osdu.core.mongodb.schema.entity.SchemaInfoDoc;
import org.opengroup.osdu.core.mongodb.schema.util.ExceptionGenerator;
import org.opengroup.osdu.core.mongodb.schema.util.VersionHierarchyUtil;
import org.opengroup.osdu.core.mongodb.schema.util.VersionUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import java.util.Comparator;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
public class SchemaInfoStoreHelper extends SchemaStoreHelperParent {
......@@ -25,6 +26,7 @@ public class SchemaInfoStoreHelper extends SchemaStoreHelperParent {
private static final Class<SchemaInfoDoc> CLASS = SchemaInfoDoc.class;
private static final String ID = "_id";
private static final String TENANT = "tenant";
private static final String KIND = "kind";
private final SchemaInfoConverter schemaInfoConverter;
private final ObjectMapper objectMapper;
......@@ -34,19 +36,23 @@ public class SchemaInfoStoreHelper extends SchemaStoreHelperParent {
this.objectMapper = objectMapper;
}
/**
* Gets schema info record
* @return schema info by requested params or null if not found
*/
@Nullable
public SchemaInfoDto getSchemaInfo(String schemaId, String tenant) {
EntityId id = new EntityId(schemaId, tenant);
SchemaInfoDoc doc = mongoDBHelper.get(ID, id, CLASS);
if (doc == null) {
throw ExceptionGenerator.notFound(id.toString());
}
return schemaInfoConverter.toDto(doc);
return Optional.ofNullable(doc).map(schemaInfoConverter::toDto).orElse(null);
}
/**
* Actually, according to other provider's implementations, only schema info is used for this method and some others
* Creates a schema info
* @throws IllegalArgumentException if schema info type already exists
* @return created schema info type
*/
public SchemaInfoDto createSchemaInfo(SchemaInfoDto dto, String tenant) {
public SchemaInfoDto createSchemaInfo(SchemaInfoDto dto, String tenant) throws IllegalArgumentException {
SchemaInfoDoc doc = schemaInfoConverter.toDoc(dto, tenant);
EntityId id = doc.getId();
if (mongoDBHelper.existsByField(ID, id, CLASS)) {
......@@ -56,7 +62,11 @@ public class SchemaInfoStoreHelper extends SchemaStoreHelperParent {
return schemaInfoConverter.toDto(doc);
}
public SchemaInfoDto updateSchemaInfo(SchemaInfoDto dto, String tenant) {
/**
* Updates schema info
* @throws IllegalArgumentException if schema info wasn't found
*/
public SchemaInfoDto updateSchemaInfo(SchemaInfoDto dto, String tenant) throws IllegalArgumentException {
SchemaInfoDoc doc = schemaInfoConverter.toDoc(dto, tenant);
EntityId id = doc.getId();
if (!mongoDBHelper.existsByField(ID, id, CLASS)) {
......@@ -91,46 +101,30 @@ public class SchemaInfoStoreHelper extends SchemaStoreHelperParent {
}
/**
* According to implementations of other providers, this method should return the latest version of the schema
* based on the information supplied. And it should be serialized to JSON.
* I am absolutely not sure about my guesses, but no adequate references has been provided,
* neither the API documentation. Hope we will find the integration tests that will show
* that my guesses are right or wrong.
*
* @return json in case if we have fresher version of schema and an empty string if not or in case if request
* was empty. new String() construction is used for this responses to have different memory address (and
* everybody does that).
* @return if there is more fresh version of schema, than in schema info, returns it, otherwise returns version in
* schema info instead.
*/
public String getLatestMinorVerSchema(SchemaInfoDto schemaInfo, String tenant) {
SchemaInfoDoc doc = schemaInfoConverter.toDoc(schemaInfo, tenant);
EntityId id = doc.getId();
SchemaInfoDoc dbDoc = mongoDBHelper.get(ID, id, CLASS);
VersionHierarchyUtil requestedVersion = new VersionHierarchyUtil(
doc.getSchemaIdentity().getSchemaVersionMajor(),
doc.getSchemaIdentity().getSchemaVersionMinor(),
doc.getSchemaIdentity().getSchemaVersionPatch()
);
VersionHierarchyUtil databaseVersion = new VersionHierarchyUtil(
dbDoc.getSchemaIdentity().getSchemaVersionMajor(),
dbDoc.getSchemaIdentity().getSchemaVersionMinor(),
dbDoc.getSchemaIdentity().getSchemaVersionPatch()
);
Comparator<VersionHierarchyUtil> comparator = new VersionHierarchyUtil.SortingVersionComparator();
if (comparator.compare(requestedVersion, databaseVersion) > 0) {
SchemaInfoDto dto = schemaInfoConverter.toDto(dbDoc);
try {
return objectMapper.writeValueAsString(dto);
} catch (JsonProcessingException e) {
e.printStackTrace();
return new String();
}
} else {
return new String();
}
String kind = VersionUtils.getKindFromId(schemaInfo.getSchemaIdentity().getId());
List<SchemaInfoDoc> docsOfKind = mongoDBHelper.find(Query.query(
Criteria.where(KIND).is(kind).and(TENANT).is(tenant)), CLASS);
SchemaInfoDoc request = schemaInfoConverter.toDoc(schemaInfo, tenant);
VersionUtils.VersionInfo latest =
Stream.concat(
Stream.of(request),
docsOfKind.stream()
).map(doc -> new VersionUtils.VersionInfo(
doc.getSchemaIdentity().getSchemaVersionMajor(),
doc.getSchemaIdentity().getSchemaVersionMinor(),
doc.getSchemaIdentity().getSchemaVersionPatch())
).max(
new VersionUtils.SortingVersionComparator()
).orElseThrow(IllegalArgumentException::new);
return String.format("%d.%d.%d", latest.getSchemaVersionMajor(), latest.getSchemaVersionMinor(), latest.getSchemaVersionPatch());
}
public boolean cleanSchema(String schemaId, String tenant) {
......@@ -167,10 +161,4 @@ public class SchemaInfoStoreHelper extends SchemaStoreHelperParent {
return query;
}
/*
TODO figure out how this method should work
String getLatestMinorVerSchema(SchemaInfo schemaInfo) throws ApplicationException;
*/
}
......@@ -7,6 +7,9 @@ import org.opengroup.osdu.core.mongodb.schema.entity.SourceDoc;
import org.opengroup.osdu.core.mongodb.schema.util.ExceptionGenerator;
import org.springframework.stereotype.Component;
import javax.annotation.Nullable;
import java.util.Optional;
@Component
public class SourceStoreHelper extends SchemaStoreHelperParent {
......@@ -19,15 +22,22 @@ public class SourceStoreHelper extends SchemaStoreHelperParent {
this.sourceConverter = sourceConverter;
}
/**
* Gets source record
* @return source by requested params or null if not found
*/
@Nullable
public SourceDto get(String sourceId, String tenant) {
EntityId id = new EntityId(sourceId, tenant);
SourceDoc doc = mongoDBHelper.get(ID, id, CLASS);
if (doc == null) {
throw ExceptionGenerator.notFound(String.format("%s %s", sourceId, tenant));
}
return sourceConverter.toDto(doc);
return Optional.ofNullable(doc).map(sourceConverter::toDto).orElse(null);
}
/**
* Creates a source
* @throws IllegalArgumentException if source already exists
* @return created source
*/
public SourceDto create(SourceDto sourceDto, String tenant) {
SourceDoc doc = sourceConverter.toDoc(sourceDto, tenant);
EntityId id = doc.getId();
......
package org.opengroup.osdu.core.mongodb.schema.util;
import lombok.Data;
import java.util.Comparator;
public class VersionHierarchyUtil {
private Long schemaVersionMajor;
private Long schemaVersionMinor;
private Long schemaVersionPatch;
public class VersionUtils {
public static String getKindFromId(String id) {
int index = id.lastIndexOf(':');
if (index < 0) {
throw new IllegalArgumentException("Cannot determine kind from " + id);
}
return id.substring(0, index);
}
@Data
public static class VersionInfo {
private Long schemaVersionMajor;
private Long schemaVersionMinor;
private Long schemaVersionPatch;
public VersionHierarchyUtil(Long schemaVersionMajor, Long schemaVersionMinor, Long schemaVersionPatch) {
this.schemaVersionMajor = schemaVersionMajor;
this.schemaVersionMinor = schemaVersionMinor;
this.schemaVersionPatch = schemaVersionPatch;
public VersionInfo(Long schemaVersionMajor, Long schemaVersionMinor, Long schemaVersionPatch) {
this.schemaVersionMajor = schemaVersionMajor;
this.schemaVersionMinor = schemaVersionMinor;
this.schemaVersionPatch = schemaVersionPatch;
}
}
public static class SortingVersionComparator implements Comparator<VersionHierarchyUtil> {
public static class SortingVersionComparator implements Comparator<VersionInfo> {
@Override
public int compare(VersionHierarchyUtil v1, VersionHierarchyUtil v2) {
public int compare(VersionInfo v1, VersionInfo v2) {
int compareMajor = v1.schemaVersionMajor.compareTo(v2.schemaVersionMajor);
if (compareMajor != 0)
return compareMajor * (-1);
......
......@@ -92,8 +92,10 @@ public class AuthorityStoreTests {
assertThat(dto).isNotNull();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void get_notFound() {
authorityStoreHelper.get("authority", "tenant");
AuthorityDto dto = authorityStoreHelper.get("authority", "tenant");
assertThat(dto).isNull();
}
}
......@@ -110,8 +110,9 @@ public class EntityTypeStoreTests {
assertThat(dto.getStatus()).isEqualTo(doc.getStatus());
}
@Test(expected = IllegalArgumentException.class)
public void get_notFound() {
entityTypeStoreHelper.get("fail-id", "tenant");
EntityTypeDto dto = entityTypeStoreHelper.get("fail-id", "tenant");
assertThat(dto).isNull();
}
}
......@@ -64,15 +64,16 @@ public class SchemaInfoStoreTests {
assertThatDtoFieldsNotNull(dto);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void getInfo_fails_notFound() {
schemaInfoStoreHelper.getSchemaInfo("wrong-id", TENANT);
SchemaInfoDto dto = schemaInfoStoreHelper.getSchemaInfo("wrong-id", TENANT);
assertThat(dto).isNull();
}
@Test
public void createSchemaInfo() {
// given
String id = "test-schema";
String id = "test-schema:1.0.0";
SchemaInfoDto dto = createSchemaDto(id);
// when
......@@ -95,7 +96,7 @@ public class SchemaInfoStoreTests {
@Test
public void updateSchemaInfo() {
String id = "test-update-id";
String id = "test-update-id:1.0.0";
SchemaInfoDoc doc = createSchemaDoc(id, TENANT);
SchemaInfoDto dto = createSchemaDto(id);
dto.setCreatedBy("new-user");
......
......@@ -92,9 +92,10 @@ public class SourceStoreTests {
assertThat(dto).isNotNull();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void get_notFound() {
sourceStoreHelper.get("source", "tenant");
SourceDto dto = sourceStoreHelper.get("source", "tenant");
assertThat(dto).isNull();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment