From df1b663c47999493fe4bafc95f503dd77f83d79c Mon Sep 17 00:00:00 2001 From: Ruslan Lavrov Date: Mon, 11 Feb 2019 15:29:16 +0200 Subject: [PATCH] Was added api test for test mark records endpoint and fixed code smells --- mod-source-record-storage-client/pom.xml | 2 - mod-source-record-storage-server/pom.xml | 6 - .../rest/impl/TestMarcRecordsApiTest.java | 114 ++++++++++++++++++ pom.xml | 4 - 4 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 mod-source-record-storage-server/src/test/java/org/folio/rest/impl/TestMarcRecordsApiTest.java diff --git a/mod-source-record-storage-client/pom.xml b/mod-source-record-storage-client/pom.xml index 76ae11fb2..055fa75b9 100644 --- a/mod-source-record-storage-client/pom.xml +++ b/mod-source-record-storage-client/pom.xml @@ -97,7 +97,6 @@ org.folio.rest.tools.GenerateRunner - false @@ -119,7 +118,6 @@ org.folio.rest.tools.ClientGenerator - false diff --git a/mod-source-record-storage-server/pom.xml b/mod-source-record-storage-server/pom.xml index 0351ed6c0..ff72713de 100644 --- a/mod-source-record-storage-server/pom.xml +++ b/mod-source-record-storage-server/pom.xml @@ -134,7 +134,6 @@ org.folio.rest.tools.GenerateRunner - false @@ -156,7 +155,6 @@ org.folio.rest.tools.ClientGenerator - false @@ -341,10 +339,6 @@ maven-surefire-plugin 2.22.1 - false diff --git a/mod-source-record-storage-server/src/test/java/org/folio/rest/impl/TestMarcRecordsApiTest.java b/mod-source-record-storage-server/src/test/java/org/folio/rest/impl/TestMarcRecordsApiTest.java new file mode 100644 index 000000000..4364f78e4 --- /dev/null +++ b/mod-source-record-storage-server/src/test/java/org/folio/rest/impl/TestMarcRecordsApiTest.java @@ -0,0 +1,114 @@ +package org.folio.rest.impl; + +import io.restassured.RestAssured; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.unit.TestContext; +import io.vertx.ext.unit.junit.VertxUnitRunner; +import org.apache.http.HttpStatus; +import org.folio.rest.jaxrs.model.SourceRecord; +import org.folio.rest.jaxrs.model.TestMarcRecordsCollection; +import org.folio.rest.persist.Criteria.Criterion; +import org.folio.rest.persist.PostgresClient; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.lang.reflect.Field; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +@RunWith(VertxUnitRunner.class) +public class TestMarcRecordsApiTest extends AbstractRestVerticleTest { + + private static final String POPULATE_TEST_MARK_RECORDS_PATH = "/source-storage/populate-test-marc-records"; + private static final String RECORDS_TABLE_NAME = "records"; + private static final String SOURCE_RECORDS_TABLE_NAME = "source_records"; + private static final String ERROR_RECORDS_TABLE_NAME = "error_records"; + private static final String MARC_RECORDS_TABLE_NAME = "marc_records"; + + private static JsonObject sourceRecord_1 = new JsonObject() + .put("source", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + private static JsonObject record_1 = new JsonObject() + .put("snapshotId", "11dfac11-1caf-4470-9ad1-d533f6360bdd") + .put("recordType", "MARC") + .put("sourceRecord", sourceRecord_1); + private static JsonObject recordCollection = new JsonObject() + .put("records", record_1) + .put("totalRecords", 1); + + @Override + public void clearTables(TestContext context) { + PostgresClient pgClient = PostgresClient.getInstance(vertx, TENANT_ID); + pgClient.delete(RECORDS_TABLE_NAME, new Criterion(), event -> { + pgClient.delete(SOURCE_RECORDS_TABLE_NAME, new Criterion(), event1 -> { + pgClient.delete(ERROR_RECORDS_TABLE_NAME, new Criterion(), event2 -> { + pgClient.delete(MARC_RECORDS_TABLE_NAME, new Criterion(), event3 -> { + if (event3.failed()) { + context.fail(event3.cause()); + } + }); + }); + }); + }); + } + + @Before + public void setUp(TestContext context) throws Exception { + HashMap newenv = new HashMap<>(); + newenv.put("test.mode", "true"); + setEnvVariables(newenv); + } + + private void setEnvVariables(HashMap newEnvVariables) throws ReflectiveOperationException { + try { + Class processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); + Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment"); + theEnvironmentField.setAccessible(true); + Map env = (Map) theEnvironmentField.get(null); + env.putAll(newEnvVariables); + Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment"); + theCaseInsensitiveEnvironmentField.setAccessible(true); + Map cienv = (Map) theCaseInsensitiveEnvironmentField.get(null); + cienv.putAll(newEnvVariables); + } catch (NoSuchFieldException e) { + Class[] classes = Collections.class.getDeclaredClasses(); + Map env = System.getenv(); + for(Class cl : classes) { + if("java.util.Collections$UnmodifiableMap".equals(cl.getName())) { + Field field = cl.getDeclaredField("m"); + field.setAccessible(true); + Object obj = field.get(env); + Map map = (Map) obj; + map.clear(); + map.putAll(newEnvVariables); + } + } + } + } + + @Test + public void shouldReturnNoContentOnPostRecordCollectionPassedInBody() { + RestAssured.given() + .spec(spec) + .body(recordCollection) + .when() + .post(POPULATE_TEST_MARK_RECORDS_PATH) + .then() + .statusCode(HttpStatus.SC_NO_CONTENT); + } + + @Test + public void shouldReturnUnprocessableEntityOnPostWhenNoRecordCollectionPassedInBody() { + TestMarcRecordsCollection testMarcRecordsCollection = new TestMarcRecordsCollection(); + testMarcRecordsCollection.setSourceRecords(Collections.singletonList(new SourceRecord())); + + RestAssured.given() + .spec(spec) + .body(testMarcRecordsCollection) + .when() + .post(POPULATE_TEST_MARK_RECORDS_PATH) + .then() + .statusCode(HttpStatus.SC_UNPROCESSABLE_ENTITY); + } +} diff --git a/pom.xml b/pom.xml index bdc84f110..7e4d4543e 100644 --- a/pom.xml +++ b/pom.xml @@ -52,10 +52,6 @@ maven-surefire-plugin 2.22.1 - false