Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Was added api test for test mark records endpoint and fixed code smells #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions mod-source-record-storage-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
</goals>
<configuration>
<mainClass>org.folio.rest.tools.GenerateRunner</mainClass>
<!-- <executable>java</executable> -->
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<systemProperties>
<systemProperty>
Expand All @@ -119,7 +118,6 @@
</goals>
<configuration>
<mainClass>org.folio.rest.tools.ClientGenerator</mainClass>
<!-- <executable>java</executable> -->
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<systemProperties>
<systemProperty>
Expand Down
6 changes: 0 additions & 6 deletions mod-source-record-storage-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@
</goals>
<configuration>
<mainClass>org.folio.rest.tools.GenerateRunner</mainClass>
<!-- <executable>java</executable> -->
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<systemProperties>
<systemProperty>
Expand All @@ -156,7 +155,6 @@
</goals>
<configuration>
<mainClass>org.folio.rest.tools.ClientGenerator</mainClass>
<!-- <executable>java</executable> -->
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<systemProperties>
<systemProperty>
Expand Down Expand Up @@ -341,10 +339,6 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<!-- TODO: update to version 3.0.0 and remove useSystemClassLoader
https://issues.folio.org/browse/FOLIO-1609
https://issues.apache.org/jira/browse/SUREFIRE-1588
-->
<useSystemClassLoader>false</useSystemClassLoader>
<systemPropertyVariables>
<vertx.logger-delegate-factory-class-name>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> newenv = new HashMap<>();
newenv.put("test.mode", "true");
setEnvVariables(newenv);
}

private void setEnvVariables(HashMap<String, String> newEnvVariables) throws ReflectiveOperationException {
try {
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.putAll(newEnvVariables);
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
theCaseInsensitiveEnvironmentField.setAccessible(true);
Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
cienv.putAll(newEnvVariables);
} catch (NoSuchFieldException e) {
Class[] classes = Collections.class.getDeclaredClasses();
Map<String, String> 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<String, String> map = (Map<String, String>) 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);
}
}
4 changes: 0 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<!-- TODO: update to version 3.0.0 and remove useSystemClassLoader
https://issues.folio.org/browse/FOLIO-1609
https://issues.apache.org/jira/browse/SUREFIRE-1588
-->
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
Expand Down