Unverified Commit cf1ea7fd authored by Jay Jay Billings's avatar Jay Jay Billings Committed by GitHub
Browse files

Merge pull request #444 from dbluhm/persistence-int-test

Integration tests and test utilities
parents 2764d514 679dc22f
......@@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -36,6 +37,7 @@ import org.eclipse.ice.data.JavascriptValidator;
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ${class} implements ${interface}, Serializable {
/**
......
......@@ -6,6 +6,7 @@
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestImplementation implements Test, Serializable {
public int shouldBePublic;
protected int shouldBeProtected;
......
......@@ -6,6 +6,7 @@
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestImplementation implements Test, Serializable {
public int testInt = 42;
}
\ No newline at end of file
......@@ -6,6 +6,7 @@
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestImplementation implements Test, Serializable {
@NonNull public java.lang.String test = "A String Value";
}
\ No newline at end of file
......@@ -6,6 +6,7 @@
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestImplementation implements Test, Serializable {
@NonNull
@Getter(AccessLevel.NONE)
......
......@@ -6,6 +6,7 @@
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestImplementation implements Test, Serializable {
public byte testByte;
public short testShort;
......
......@@ -6,6 +6,7 @@
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestImplementation implements Test, Serializable {
@NonNull public java.util.UUID testUuid;
@NonNull public java.lang.String testString;
......
......@@ -6,6 +6,7 @@
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestImplementation implements Test, Serializable {
public int testInt;
}
\ No newline at end of file
......@@ -6,6 +6,7 @@
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestImplementation implements Test, Serializable {
@NonNull public java.util.UUID testUuid;
}
\ No newline at end of file
**/target/
**/bin/
.factorypath
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.ice.tests.integration</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-test-sources/test-annotations"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.ice.tests.integration.dev.annotation</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Persistence Handler Integration Tests
=====================================
This module implements integration tests for Persistence Handlers generated
using the `@Persisted` annotation.
**Test Prerequisites:**
- MongoDB instance running locally or accessible by host name and port to the
test runner.
### Configuration
**Property file location:** `$TEST_DATA_DIR/mongo.properties`
**Properties:**
- `host` - Host name of MongoDB instance. Defaults to `localhost` if unset.
- `port` - Port MongoDB is running on on host. Defaults to `27017` if unset.
- `database` - The database to use for testing. Defaults to `test` if unset.
## Running tests
### Running with local MongoDB
To run these tests, you will need a running instance of MongoDB. A quick method
to get a running instance is to launch a container using docker or podman (if
using a RHEL based Linux distribution):
```sh
$ podman run -d -p 27017:27017 mongo
```
OR
```sh
$ sudo docker run -d -p 27017:27017 mongo
```
Then you can execute tests from this directory with the standard:
```sh
$ mvn clean test
```
### Running with remote MongoDB
If there is an already running instance of MongoDB available, place a property
file like the following in your test data directory with your values filled in:
```properties
host=mymongoserver
port=27017
database=testing_db
```
If your test data directory is not `$HOME/ICETests`, you can set it using the
`TEST_DATA_PATH` environment variable:
```sh
$ TEST_DATA_PATH=/tmp/ICETests \
mvn clean test
```
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.ice</groupId>
<artifactId>org.eclipse.ice.tests.integration</artifactId>
<version>3.0.0-SNAPSHOT</version>
</parent>
<artifactId>org.eclipse.ice.tests.integration.dev.annotation</artifactId>
<name>ICE Dev Annoations Integration Tests</name>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.ice</groupId>
<artifactId>org.eclipse.ice.data</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.ice.dev</groupId>
<artifactId>org.eclipse.ice.dev.annotations</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.ice</groupId>
<artifactId>org.eclipse.ice.tests.data</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*******************************************************************************
* Copyright (c) 2020- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Daniel Bluhm - Initial implementation
*******************************************************************************/
package org.eclipse.ice.tests.integration.dev.annotation;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Iterator;
import org.bson.Document;
import org.eclipse.ice.tests.data.TestConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
/**
* Test Generated Persistence Handler works as intended against MongoDB.
* @author Daniel Bluhm
*/
class TestElementPersistenceHandlerTest {
/**
* Logger.
*/
public final Logger logger = LoggerFactory.getLogger(
TestElementPersistenceHandlerTest.class
);
/**
* The name of the collection to use in testing.
*/
public static final String COLLECTION = "test";
/**
* Config file name.
*/
public static final String CONFIG = "mongo.properties";
/**
* Default hostname of MongoDB instance.
*/
public static final String DEFAULT_HOST = "localhost";
/**
* Default port of MongoDB instance.
*/
public static final String DEFAULT_PORT = "27017";
/**
* Default database to use in testing.
*/
public static final String DEFAULT_DB = "test";
/**
* Connection to MongoDB.
*/
private MongoDatabase db;
/**
* The collection used during testing.
*/
private MongoCollection<Document> collection;
/**
* Set up our test environment.
*/
public TestElementPersistenceHandlerTest() {
TestConfig config = TestConfig.from(CONFIG);
String host = config.getProperty("host", DEFAULT_HOST);
String port = config.getProperty("port", DEFAULT_PORT);
String database = config.getProperty("database", DEFAULT_DB);
this.db = MongoClients.create(
String.format("mongodb://%s:%s", host, port)
).getDatabase(database);
this.collection = this.db.getCollection(COLLECTION);
}
/**
* Prepare the database with one doc corresponding to the returned element.
* @param handler to use to insert the document
* @return The element that was saved to the collection.
* @throws Exception on failure
*/
public TestElement setupOneDoc(TestElementPersistenceHandler handler)
throws Exception
{
TestElement element = new TestElementImplementation();
element.setTest("unique string for testing");
handler.save(element);
return element;
}
/**
* Clear out collection after every test.
*/
@AfterEach
public void cleanup() {
this.collection.drop();
}
/**
* Test that we can insert an element.
* @throws Exception on failure
*/
@Test
void testCanInsert() throws Exception {
TestElementPersistenceHandler handler = new TestElementPersistenceHandler(db);
setupOneDoc(handler);
assertEquals(1, this.collection.countDocuments());
}
/**
* Test that we can insert more than one element.
* @throws Exception on failure
*/
@Test
void testCanInsertMany() throws Exception {
TestElementPersistenceHandler handler = new TestElementPersistenceHandler(db);
TestElement element1 = new TestElementImplementation();
element1.setTest("unique string for testing");
handler.save(element1);
TestElement element2 = new TestElementImplementation();
element2.setTest("Element 2 string");
handler.save(element2);
assertEquals(2, this.collection.countDocuments());
}
/**
* Test that we can clear the collection.
* @throws Exception on failure
*/
@Test
void testCanClear() throws Exception {
TestElementPersistenceHandler handler = new TestElementPersistenceHandler(db);
TestElement element1 = new TestElementImplementation();
element1.setTest("unique string for testing");
handler.save(element1);
TestElement element2 = new TestElementImplementation();
element2.setTest("Element 2 string");
handler.save(element2);
assertEquals(2, this.collection.countDocuments());
handler.clear();
assertEquals(0, this.collection.countDocuments());
}
/**
* Test that we can retrieve all TestElements from the collection.
* @throws Exception on failure
*/
@Test
void testCanFindAll() throws Exception {
TestElementPersistenceHandler handler = new TestElementPersistenceHandler(db);
TestElement element1 = new TestElementImplementation();
element1.setTest("unique string for testing");
handler.save(element1);
TestElement element2 = new TestElementImplementation();
element2.setTest("Element 2 string");
handler.save(element2);
Iterator<TestElement> results = handler.findAll().iterator();
assertEquals(element1, results.next());
assertEquals(element2, results.next());
}
/**
* Test all the findBy methods.
* @throws Exception on failure
*/
@Test
void testCanFindByMethods() throws Exception {
TestElementPersistenceHandler handler = new TestElementPersistenceHandler(db);
TestElement element = setupOneDoc(handler);
assertEquals(element, handler.findByUUID(element.getUUID()));
assertEquals(element, handler.findById(
element.getId()).iterator().next()
);
assertEquals(element, handler.findByName(
element.getName()).iterator().next()
);
assertEquals(element, handler.findByDescription(
element.getDescription()).iterator().next()
);
assertEquals(element, handler.findByComment(
element.getComment()).iterator().next()
);
assertEquals(element, handler.findByContext(
element.getContext()).iterator().next()
);
assertEquals(element, handler.findByRequired(
element.isRequired()).iterator().next()
);
assertEquals(element, handler.findBySecret(
element.isSecret()).iterator().next()
);
assertEquals(element, handler.findByTest(
element.getTest()).iterator().next()
);
}
}
/*******************************************************************************
* Copyright (c) 2020- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Daniel Bluhm - Initial implementation
*******************************************************************************/
package org.eclipse.ice.tests.integration.dev.annotation;
import org.eclipse.ice.dev.annotations.DataElement;
import org.eclipse.ice.dev.annotations.DataField;
import org.eclipse.ice.dev.annotations.Persisted;
/**
* Test DataElement to be used for testing generated PersistenceHandlers.
* @author Daniel Bluhm
*/
@DataElement(name = "TestElement")
@Persisted(collection = TestElementPersistenceHandlerTest.COLLECTION)
public class TestElementSpec {
@DataField private String test;
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.ice</groupId>
<artifactId>org.eclipse.ice.tests.integration</artifactId>
<versio