Commit 072a9db7 authored by Nikita Ruzhevsky's avatar Nikita Ruzhevsky
Browse files

done tests for schema info store helper

parent 590ae6bb
Pipeline #60816 passed with stages
in 4 minutes and 35 seconds
......@@ -82,16 +82,12 @@ public class SchemaInfoStoreHelper extends SchemaStoreHelperParent {
* this method
*
* @param tenants tenant list to find the non-unique entry
* @return true if any of the combination tenant-id exists in DB
* @return true if none of the combinations tenant+id exists in DB
*/
public boolean isUnique(String schemaId, List<String> tenants) {
for (String tenant : tenants) {
EntityId id = new EntityId(schemaId, tenant);
if (mongoDBHelper.existsByField(ID, id, CLASS)) {
return false;
}
}
return true;
List<EntityId> ids = tenants.stream().map(tenant -> new EntityId(schemaId, tenant)).collect(Collectors.toList());
List<SchemaInfoDoc> docs = mongoDBHelper.getList("_id", ids, CLASS);
return docs.isEmpty();
}
/**
......@@ -142,7 +138,8 @@ public class SchemaInfoStoreHelper extends SchemaStoreHelperParent {
return mongoDBHelper.delete(ID, id, CLASS);
}
private Query transformQueryParamsToQuery(QueryParamsDto qp, String tenant) {
// public modifier is used for test query creation
public Query transformQueryParamsToQuery(QueryParamsDto qp, String tenant) {
AtomicReference<Criteria> criteria = new AtomicReference<>(Criteria.where(TENANT).is(tenant));
BiConsumer<String, Object> paramAdder = (parameterName, value) -> {
......
......@@ -6,6 +6,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.opengroup.osdu.core.mongodb.schema.config.MongoEmbeddedConfig;
import org.opengroup.osdu.core.mongodb.schema.config.SchemaTestConfig;
import org.opengroup.osdu.core.mongodb.schema.dto.QueryParamsDto;
import org.opengroup.osdu.core.mongodb.schema.dto.SchemaIdentityDto;
import org.opengroup.osdu.core.mongodb.schema.dto.SchemaInfoDto;
import org.opengroup.osdu.core.mongodb.schema.entity.EntityId;
......@@ -20,7 +21,9 @@ import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -92,7 +95,136 @@ public class SchemaInfoStoreTests {
@Test
public void updateSchemaInfo() {
String id = "test-update-id";
SchemaInfoDoc doc = createSchemaDoc(id, TENANT);
SchemaInfoDto dto = createSchemaDto(id);
dto.setCreatedBy("new-user");
mongoTemplate.save(doc);
SchemaInfoDto response = schemaInfoStoreHelper.updateSchemaInfo(dto, TENANT);
assertThatDtoFieldsNotNull(response);
assertThat(response.getCreatedBy()).isEqualTo(dto.getCreatedBy());
}
@Test(expected = IllegalArgumentException.class)
public void updateSchemaInfo_fail_wrongId() {
String id = "test-update-id";
SchemaInfoDoc doc = createSchemaDoc(id, TENANT);
SchemaInfoDto dto = createSchemaDto(id + "other-id");
mongoTemplate.save(doc);
schemaInfoStoreHelper.updateSchemaInfo(dto, TENANT);
}
@Test
public void transformQueryParamsToQueryTest() {
// given
String authority = "authority",
source = "source",
status = "status",
scope = "scope",
entityType = "entityType";
long[] schemaVersion = {1, 2, 3};
int limit = 10, offset = 10;
QueryParamsDto dto = QueryParamsDto.builder()
.authority(authority)
.source(source)
.entityType(entityType)
.limit(limit)
.offset(offset)
.schemaVersionMajor(schemaVersion[0])
.schemaVersionMinor(schemaVersion[1])
.schemaVersionPatch(schemaVersion[2])
.status(status)
.scope(scope)
.latestVersion(false)
.build();
Query query = schemaInfoStoreHelper.transformQueryParamsToQuery(dto, TENANT);
assertThat(query.getQueryObject().get("schemaIdentity.authority")).isEqualTo(authority);
assertThat(query.getQueryObject().get("schemaIdentity.source")).isEqualTo(source);
assertThat(query.getQueryObject().get("schemaIdentity.entityType")).isEqualTo(entityType);
assertThat(query.getQueryObject().get("status")).isEqualTo(status);
assertThat(query.getQueryObject().get("scope")).isEqualTo(scope);
assertThat(query.getQueryObject().get("schemaIdentity.schemaVersionMajor")).isEqualTo(schemaVersion[0]);
assertThat(query.getQueryObject().get("schemaIdentity.schemaVersionMinor")).isEqualTo(schemaVersion[1]);
assertThat(query.getQueryObject().get("schemaIdentity.schemaVersionPatch")).isEqualTo(schemaVersion[2]);
assertThat(query.getSkip()).isEqualTo(offset);
assertThat(query.getLimit()).isEqualTo(limit);
}
@Test
public void queryForSchemaInfoList() {
// given
SchemaInfoDoc validDoc = createSchemaDoc("test-1", TENANT);
SchemaInfoDoc otherDoc = createSchemaDoc("test-2", TENANT);
String authority = "test-authority-1";
validDoc.getSchemaIdentity().setAuthority(authority);
mongoTemplate.insert(validDoc);
mongoTemplate.insert(otherDoc);
QueryParamsDto query = QueryParamsDto.builder()
.authority(authority)
.build();
// when
List<SchemaInfoDto> list = schemaInfoStoreHelper.getSchemaInfoList(query, TENANT);
// then
assertThat(list).singleElement().matches(dto -> dto.getSchemaIdentity().getAuthority().equals(authority));
assertThat(list).noneMatch(dto -> dto.getSchemaIdentity().getId().equals(otherDoc.getSchemaIdentity().getId()));
}
@Test
public void testUnique_isUnique() {
List<String> tenants = Arrays.asList("a", "b", "c", TENANT);
boolean result = schemaInfoStoreHelper.isUnique("some-id", tenants);
assertThat(result).isTrue();
}
@Test
public void testUnique_alreadyExists() {
// given
String id = "test-schema";
List<String> tenants = Arrays.asList("a", "b", "c", TENANT);
SchemaInfoDoc doc = createSchemaDoc(id, TENANT);
mongoTemplate.save(doc);
// when
boolean result = schemaInfoStoreHelper.isUnique(id, tenants);
// then
assertThat(result).isFalse();
}
@Test
public void cleanSchema() {
String id = "test-schema";
SchemaInfoDoc doc = createSchemaDoc(id, TENANT);
mongoTemplate.save(doc);
boolean status = schemaInfoStoreHelper.cleanSchema(id, TENANT);
assertThat(status).isTrue();
}
@Test
public void cleanSchema_notPresent() {
boolean status = schemaInfoStoreHelper.cleanSchema("fake-id", TENANT);
assertThat(status).isFalse();
}
......
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