Commit 355135ea authored by Danijel Dragicevic's avatar Danijel Dragicevic

Implemented JUnit tests as in Mule 3 Connector.

parent 7933dc0f
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.codecentric.mule.modules</groupId> <groupId>de.codecentric.mule.modules</groupId>
<artifactId>assert-object-equals-module</artifactId> <artifactId>assert-object-equals-module</artifactId>
<version>1.0.175-SNAPSHOT</version> <version>1.0.180-SNAPSHOT</version>
<packaging>mule-extension</packaging> <packaging>mule-extension</packaging>
<name>Assert Object Equals Extension</name> <name>Assert Object Equals Extension</name>
......
package de.codecentric.mule.modules.assertobjectequals.internal;
import org.mule.runtime.extension.api.annotation.Configurations;
import org.mule.runtime.extension.api.annotation.Extension;
import org.mule.runtime.extension.api.annotation.dsl.xml.Xml;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
/**
* This is the main class of an extension, is the entry point from which configurations, connection providers, operations
* and sources are going to be declared.
*/
@Xml(prefix = "assertobjectequals")
@Extension(name = "AssertObjectEquals")
@Configurations(AssertObjectEqualsConfiguration.class)
public class AssertObjectEqualsExtension {
/**
* This method is used for calling implementation logic ONLY in testing/development purposes.
* Once we reach "production" stage this method should be completely removed.
*/
public static void main(String[] args) throws Exception{
//This is the expected file from my local disk. I'm imitating sending of real file from srt/test/resources directory to the mule component.
File expectedFile = new File("/Users/ddanijel/AnypointStudio/studio-workspace/test-aoe-module/src/test/resources/expected-file.xml");
FileInputStream expectedFileInputStream = new FileInputStream(expectedFile);
BufferedInputStream expectedFileBuffered = new BufferedInputStream(expectedFileInputStream);
//This is the actual file from my local disk. I'm imitating sending of real file from srt/test/resources directory to the mule component.
File actualFile = new File("/Users/ddanijel/AnypointStudio/studio-workspace/test-aoe-module/src/test/resources/actual-file.xml");
FileInputStream actualFileInputStream = new FileInputStream(actualFile);
BufferedInputStream actualFileBuffered = new BufferedInputStream(actualFileInputStream);
List<String> additionalOptions = new ArrayList<>();
additionalOptions.add("['addresses'][#]['timestamp'] ignore");
AssertObjectEqualsOperations aoe = new AssertObjectEqualsOperations();
//aoe.compareObjects(expectedFileBuffered, actualFileBuffered, additionalOptions);
aoe.compareXml(expectedFileBuffered, actualFileBuffered, "IGNORE_COMMENTS");
}
}
package de.codecentric.mule.modules.assertobjectequals.internal; package de.codecentric.mule.modules.assertobjectequals.internal;
import org.mule.runtime.extension.api.annotation.Operations;
import org.mule.runtime.extension.api.annotation.connectivity.ConnectionProviders; import org.mule.runtime.extension.api.annotation.connectivity.ConnectionProviders;
import org.mule.runtime.extension.api.annotation.param.Parameter; import org.mule.runtime.extension.api.annotation.param.Parameter;
...@@ -8,9 +7,9 @@ import org.mule.runtime.extension.api.annotation.param.Parameter; ...@@ -8,9 +7,9 @@ import org.mule.runtime.extension.api.annotation.param.Parameter;
* This class represents an extension configuration, values set in this class are commonly used across multiple * This class represents an extension configuration, values set in this class are commonly used across multiple
* operations since they represent something core from the extension. * operations since they represent something core from the extension.
*/ */
@Operations(AssertObjectEqualsOperations.class) @org.mule.runtime.extension.api.annotation.Operations(Operations.class)
@ConnectionProviders(AssertObjectEqualsConnectionProvider.class) @ConnectionProviders(ConnectionProvider.class)
public class AssertObjectEqualsConfiguration { public class Configuration {
@Parameter @Parameter
private String configId; private String configId;
......
...@@ -4,11 +4,11 @@ package de.codecentric.mule.modules.assertobjectequals.internal; ...@@ -4,11 +4,11 @@ package de.codecentric.mule.modules.assertobjectequals.internal;
/** /**
* This class represents an extension connection just as example (there is no real connection with anything here c:). * This class represents an extension connection just as example (there is no real connection with anything here c:).
*/ */
public final class AssertObjectEqualsConnection { public final class Connection {
private final String id; private final String id;
public AssertObjectEqualsConnection(String id) { public Connection(String id) {
this.id = id; this.id = id;
} }
......
...@@ -5,7 +5,6 @@ import org.mule.runtime.extension.api.annotation.param.Parameter; ...@@ -5,7 +5,6 @@ import org.mule.runtime.extension.api.annotation.param.Parameter;
import org.mule.runtime.extension.api.annotation.param.Optional; import org.mule.runtime.extension.api.annotation.param.Optional;
import org.mule.runtime.api.connection.ConnectionValidationResult; import org.mule.runtime.api.connection.ConnectionValidationResult;
import org.mule.runtime.api.connection.PoolingConnectionProvider; import org.mule.runtime.api.connection.PoolingConnectionProvider;
import org.mule.runtime.api.connection.ConnectionProvider;
import org.mule.runtime.api.connection.CachedConnectionProvider; import org.mule.runtime.api.connection.CachedConnectionProvider;
import org.mule.runtime.extension.api.annotation.param.display.DisplayName; import org.mule.runtime.extension.api.annotation.param.display.DisplayName;
...@@ -22,11 +21,11 @@ import org.slf4j.LoggerFactory; ...@@ -22,11 +21,11 @@ import org.slf4j.LoggerFactory;
* <p> * <p>
* This particular example is a {@link PoolingConnectionProvider} which declares that connections resolved by this provider * This particular example is a {@link PoolingConnectionProvider} which declares that connections resolved by this provider
* will be pooled and reused. There are other implementations like {@link CachedConnectionProvider} which lazily creates and * will be pooled and reused. There are other implementations like {@link CachedConnectionProvider} which lazily creates and
* caches connections or simply {@link ConnectionProvider} if you want a new connection each time something requires one. * caches connections or simply {@link org.mule.runtime.api.connection.ConnectionProvider} if you want a new connection each time something requires one.
*/ */
public class AssertObjectEqualsConnectionProvider implements PoolingConnectionProvider<AssertObjectEqualsConnection> { public class ConnectionProvider implements PoolingConnectionProvider<Connection> {
private final Logger LOGGER = LoggerFactory.getLogger(AssertObjectEqualsConnectionProvider.class); private final Logger LOGGER = LoggerFactory.getLogger(ConnectionProvider.class);
/** /**
* A parameter that is always required to be configured. * A parameter that is always required to be configured.
...@@ -43,12 +42,12 @@ public class AssertObjectEqualsConnectionProvider implements PoolingConnectionPr ...@@ -43,12 +42,12 @@ public class AssertObjectEqualsConnectionProvider implements PoolingConnectionPr
private int optionalParameter; private int optionalParameter;
@Override @Override
public AssertObjectEqualsConnection connect() throws ConnectionException { public Connection connect() throws ConnectionException {
return new AssertObjectEqualsConnection(requiredParameter + ":" + optionalParameter); return new Connection(requiredParameter + ":" + optionalParameter);
} }
@Override @Override
public void disconnect(AssertObjectEqualsConnection connection) { public void disconnect(Connection connection) {
try { try {
connection.invalidate(); connection.invalidate();
} catch (Exception e) { } catch (Exception e) {
...@@ -57,7 +56,7 @@ public class AssertObjectEqualsConnectionProvider implements PoolingConnectionPr ...@@ -57,7 +56,7 @@ public class AssertObjectEqualsConnectionProvider implements PoolingConnectionPr
} }
@Override @Override
public ConnectionValidationResult validate(AssertObjectEqualsConnection connection) { public ConnectionValidationResult validate(Connection connection) {
return ConnectionValidationResult.success(); return ConnectionValidationResult.success();
} }
} }
package de.codecentric.mule.modules.assertobjectequals.internal;
import org.mule.runtime.extension.api.annotation.Configurations;
import org.mule.runtime.extension.api.annotation.dsl.xml.Xml;
/**
* This is the main class of an extension, is the entry point from which configurations, connection providers, operations
* and sources are going to be declared.
*/
@Xml(prefix = "assertobjectequals")
@org.mule.runtime.extension.api.annotation.Extension(name = "AssertObjectEquals")
@Configurations(Configuration.class)
public class ModuleExtension {
}
...@@ -17,7 +17,7 @@ import java.util.*; ...@@ -17,7 +17,7 @@ import java.util.*;
/** /**
* This class is a container for operations, every public method in this class will be taken as an extension operation. * This class is a container for operations, every public method in this class will be taken as an extension operation.
*/ */
public class AssertObjectEqualsOperations { public class Operations {
/** /**
* Compare two objects. Drill down into {@link Map} and {@link List}, use {@link Object#equals(Object)} for all other * Compare two objects. Drill down into {@link Map} and {@link List}, use {@link Object#equals(Object)} for all other
...@@ -89,7 +89,7 @@ public class AssertObjectEqualsOperations { ...@@ -89,7 +89,7 @@ public class AssertObjectEqualsOperations {
} }
messageBuilder.append(s); messageBuilder.append(s);
} }
throw new AssertionError("\n" + messageBuilder); throw new AssertionError(messageBuilder);
} }
} }
......
package de.codecentric.mule.modules.assertobjectequals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import org.mule.functional.junit4.MuleArtifactFunctionalTestCase;
import org.junit.Test;
public class AssertObjectEqualsOperationsTestCase extends MuleArtifactFunctionalTestCase {
/**
* Specifies the mule config xml with the flows that are going to be executed in the tests, this file lives in the test resources.
*/
@Override
protected String getConfigFile() {
return "test-mule-config.xml";
}
@Test
public void executeSayHiOperation() throws Exception {
String payloadValue = ((String) flowRunner("sayHiFlow").run()
.getMessage()
.getPayload()
.getValue());
assertThat(payloadValue, is("Hello Mariano Gonzalez!!!"));
}
@Test
public void executeRetrieveInfoOperation() throws Exception {
String payloadValue = ((String) flowRunner("retrieveInfoFlow")
.run()
.getMessage()
.getPayload()
.getValue());
assertThat(payloadValue, is("Using Configuration [configId] with Connection id [aValue:100]"));
}
}
package de.codecentric.mule.modules.assertobjectequals.internal;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.mule.runtime.api.lifecycle.InitialisationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class AbstractConnectorTest {
protected Operations aoec;
@Before
public void before() throws InitialisationException {
aoec = new Operations();
}
@After
public void after() {
aoec = null;
}
protected InputStream string2Stream(String str) {
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
return new ByteArrayInputStream(bytes);
}
protected String stream2String(InputStream is) throws IOException {
byte[] bytes = IOUtils.toByteArray(is);
is.close();
return new String(bytes, StandardCharsets.UTF_8);
}
}
package de.codecentric.mule.modules.assertobjectequals.internal;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ObjectComparatorTest {
private ObjectComparator ocEqualMapsOrdered;
private ObjectComparator ocContainsMapsOrdered;
private ObjectComparator ocEqualMapsUnordered;
private ObjectComparator ocContainsMapsUnordered;
private ObjectComparator ocIgnore;
@Before
public void setUp() {
ocEqualMapsOrdered = new ObjectComparator(createFactory(EnumSet.of(PathOption.CHECK_MAP_ORDER)));
ocContainsMapsOrdered = new ObjectComparator(
createFactory(EnumSet.of(PathOption.CONTAINS_ONLY_ON_MAPS, PathOption.CHECK_MAP_ORDER)));
ocEqualMapsUnordered = new ObjectComparator(createFactory(EnumSet.noneOf(PathOption.class)));
ocContainsMapsUnordered = new ObjectComparator(createFactory(EnumSet.of(PathOption.CONTAINS_ONLY_ON_MAPS)));
ocIgnore = new ObjectComparator(createFactory(EnumSet.of(PathOption.IGNORE)));
}
@After
public void tearDown() {
ocEqualMapsOrdered = null;
ocContainsMapsOrdered = null;
ocEqualMapsUnordered = null;
ocContainsMapsUnordered = null;
ocIgnore = null;
}
private ObjectCompareOptionsFactory createFactory(EnumSet<PathOption> options) {
final EnumSet<PathOption> o = EnumSet.copyOf(options);
return new ObjectCompareOptionsFactory() {
@Override
public EnumSet<PathOption> createOptions(EnumSet<PathOption> inherited, Path path) {
return o;
}
};
}
@Test
public void testNullNull() {
assertEmpty(ocEqualMapsOrdered.compare(null, null));
}
@Test
public void testStringStringEqual() {
assertEmpty(ocEqualMapsOrdered.compare("foo", "foo"));
}
@Test
public void testNullString() {
Collection<String> diffs = ocEqualMapsOrdered.compare(null, "foo");
assertCollectionEquals(diffs, "at '', expected is null, actual foo");
}
@Test
public void testStringNull() {
Collection<String> diffs = ocEqualMapsOrdered.compare("foo", null);
assertCollectionEquals(diffs, "at '', expected foo, actual is null");
}
@Test
public void testStringStringNotEqual() {
Collection<String> diffs = ocEqualMapsOrdered.compare("foo", "bar");
assertCollectionEquals(diffs, "at '', expected foo, but found bar");
}
@Test
public void testStringStringNotEqualIgnore() {
assertEmpty(ocIgnore.compare("foo", "bar"));
}
@Test
public void testEmptyListListEqual() {
List<String> list = Arrays.asList();
assertEmpty(ocEqualMapsOrdered.compare(list, list));
}
@Test
public void testListListEqual() {
List<String> list = Arrays.asList("a", "b", "c");
assertEmpty(ocEqualMapsOrdered.compare(list, list));
}
@Test
public void testListSizeNotEqual() {
List<String> listA = Arrays.asList("a", "b", "c");
List<String> listB = Arrays.asList("a", "b");
Collection<String> diffs = ocEqualMapsOrdered.compare(listA, listB);
assertCollectionEquals(diffs, "at '', expected size 3, actual 2");
}
@Test
public void testListsNotEqual() {
List<String> listA = Arrays.asList("a", "b", "c");
List<String> listB = Arrays.asList("a", "c", "d");
Collection<String> diffs = ocEqualMapsOrdered.compare(listA, listB);
assertCollectionEquals(diffs, "at '[1]', expected b, but found c", "at '[2]', expected c, but found d");
}
@Test
public void testListsNotEqualMap() {
List<String> list = Arrays.asList("a", "b", "c");
Map<String, String> map = new LinkedHashMap<>();
Collection<String> diffs = ocEqualMapsOrdered.compare(list, map);
assertCollectionEquals(diffs, "at '', expected List, but found java.util.LinkedHashMap");
}
@Test
public void testMapNotEqualList() {
List<String> list = Arrays.asList("a", "b", "c");
Map<String, String> map = new LinkedHashMap<>();
Collection<String> diffs = ocEqualMapsOrdered.compare(map, list);
assertCollectionEquals(diffs, "at '', expected Map, but found java.util.Arrays$ArrayList");
}
@Test
public void testEmptyMapsEqual() {
Map<String, String> mapA = new LinkedHashMap<>();
Map<String, String> mapB = new LinkedHashMap<>();
assertEmpty(ocEqualMapsOrdered.compare(mapA, mapB));
assertEmpty(ocEqualMapsUnordered.compare(mapA, mapB));
assertEmpty(ocContainsMapsOrdered.compare(mapA, mapB));
assertEmpty(ocContainsMapsUnordered.compare(mapA, mapB));
}
@Test
public void testMapsEqual() {
Map<String, String> mapA = new LinkedHashMap<>();
mapA.put("a", "A");
mapA.put("b", "B");
mapA.put("c", "C");
Map<String, String> mapB = new LinkedHashMap<>();
mapB.put("a", "A");
mapB.put("b", "B");
mapB.put("c", "C");
assertEmpty(ocEqualMapsOrdered.compare(mapA, mapB));
assertEmpty(ocContainsMapsOrdered.compare(mapA, mapB));
}
@Test
public void testMapsOrderChangedEqual() {
Map<String, String> mapA = new LinkedHashMap<>();
mapA.put("a", "A");
mapA.put("b", "B");
mapA.put("c", "C");
Map<String, String> mapB = new LinkedHashMap<>();
mapB.put("a", "A");
mapB.put("c", "C");
mapB.put("b", "B");
assertEmpty(ocContainsMapsUnordered.compare(mapA, mapB));
Collection<String> diffs = ocEqualMapsOrdered.compare(mapA, mapB);
assertCollectionEquals(diffs, "at '', expect key b, actual c");
}
@Test
public void testMapsMissingActual() {
Map<String, String> mapA = new LinkedHashMap<>();
mapA.put("a", "A");
mapA.put("b", "B");
mapA.put("c", "C");
Map<String, String> mapB = new LinkedHashMap<>();
mapB.put("a", "A");
mapB.put("b", "B");
Collection<String> diffs = ocEqualMapsUnordered.compare(mapA, mapB);
assertCollectionEquals(diffs, "at '', objects missing in actual: c");
diffs = ocContainsMapsUnordered.compare(mapA, mapB);
assertCollectionEquals(diffs, "at '', objects missing in actual: c");
diffs = ocEqualMapsOrdered.compare(mapA, mapB);
assertCollectionEquals(diffs, "at '', objects missing in actual: c");
}
@Test
public void testMapsMissingExpected() {
Map<String, String> mapA = new LinkedHashMap<>();
mapA.put("a", "A");
mapA.put("b", "B");
Map<String, String> mapB = new LinkedHashMap<>();
mapB.put("a", "A");
mapB.put("b", "B");
mapB.put("c", "C");
Collection<String> diffs = ocEqualMapsOrdered.compare(mapA, mapB);
assertCollectionEquals(diffs, "at '', objects missing in expected: c");
diffs = ocEqualMapsUnordered.compare(mapA, mapB);
assertCollectionEquals(diffs, "at '', objects missing in expected: c");
}
@Test
public void testMapsContains() {
Map<String, String> mapA = new LinkedHashMap<>();
mapA.put("a", "A");
mapA.put("b", "B");
Map<String, String> mapB = new LinkedHashMap<>();
mapB.put("a", "A");
mapB.put("b", "B");
mapB.put("c", "C");
assertEmpty(ocContainsMapsOrdered.compare(mapA, mapB));
assertEmpty(ocContainsMapsUnordered.compare(mapA, mapB));
}
@Test
public void testMapsMoreExpectedThanActual() {
Map<String, String> mapA = new LinkedHashMap<>();
mapA.put("a", "A");
mapA.put("b", "B");
mapA.put("c", "C");
Map<String, String> mapB = new LinkedHashMap<>();
mapB.put("a", "A");
mapB.put("b", "B");
Collection<String> diffs = ocContainsMapsUnordered.compare(mapA, mapB);
assertCollectionEquals(diffs, "at '', objects missing in actual: c");
}
@Test
public void testMapsValuesNotEqual() {
Map<String, String> mapA = new LinkedHashMap<>();
mapA.put("a", "A");
mapA.put("b", "B");
Map<String, String> mapB = new LinkedHashMap<>();
mapB.put("a", "A");
mapB.put("b", "CC");
Collection<String> diffs = ocEqualMapsOrdered.compare(mapA, mapB);
assertCollectionEquals(diffs, "at '['b']', expected B, but found CC");
diffs = ocContainsMapsUnordered.compare(mapA, mapB);
assertCollectionEquals(diffs, "at '['b']', expected B, but found CC");
}
private void assertEmpty(Collection<?> c) {
assertTrue("collection not empty", c.isEmpty());
}
private void assertCollectionEquals(Collection<String> c, String... entries) {
assertEquals("Collection length", entries.length, c.size());
Iterator<String> iter = c.iterator();
for (String expected : entries) {
if (iter.hasNext()) {
String actual = iter.next();
assertEquals(expected, actual);
}
}
}
}
package de.codecentric.mule.modules.assertobjectequals.internal;
import org.junit.Assert;
import org.junit.Test;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class ObjectTests extends AbstractConnectorTest {
@Test
public void allNull() throws Exception {
aoec.compareObjects(null, null, false, false, null);
}
@Test
public void streamListEmptyOptions() throws Exception {
InputStream expected = string2Stream("[\"a\", \"b\", \"c\"]");
List<String> actual = list("a", "b", "c");
aoec.compareObjects(expected, actual, false, false, list());
}
@Test
public void streamObjectEmptyOptions() throws Exception {
InputStream expected = string2Stream("{\"a\": 1, \"b\": 2}");
Map<Object, Object> actual = new LinkedHashMap<>();
actual.put("b", 2);
actual.put("a", 1);
aoec.compareObjects(expected, actual, false, false, list());
}
@Test
public void streamObjectCheckMapOrder() throws Exception {
String expected = "{\"a\": 1, \"b\": 2}";
Map<Object, Object> actual = new LinkedHashMap<>();
actual.put("b", 2);
actual.put("a", 1);
expectNotEqual(string2Stream(expected), actual, false, true, list(), "at '', expect key a, actual b");
expectNotEqual(expected, actual, false, false, list("CHECK_MAP_ORDER"), "at '', expect key a, actual b");
}
@Test
public void streamObjectContainsOnly() throws Exception {
String expected = "{\"a\": 1}";
Map<Object, Object> actual = new LinkedHashMap<>();
actual.put("b", 2);
actual.put("a", 1);
aoec.compareObjects(string2Stream(expected), actual, true, false, list());
aoec.compareObjects(string2Stream(expected), actual, false, false, list("CONTAINS_ONLY_ON_MAPS"));
}
@Test
public void jsonStringListEmptyOptions() throws Exception {
String expected = "[\"a\", \"b\", \"c\"]";
Object actual = list("a", "b", "c");
aoec.compareObjects(expected, actual, false, false, list());
}
@Test
public void jsonByteArrayObjectEmptyOptions() throws Exception {
byte[] expected = "[\"a\", \"b\",