diff options
Diffstat (limited to 'vid-app-common/src/test/java/org/onap/vid/testUtils')
3 files changed, 198 insertions, 0 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/testUtils/IntersectionTest.java b/vid-app-common/src/test/java/org/onap/vid/testUtils/IntersectionTest.java new file mode 100644 index 000000000..c766d26f3 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/testUtils/IntersectionTest.java @@ -0,0 +1,101 @@ +package org.onap.vid.testUtils; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.vid.utils.Intersection; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by moriya1 on 10/10/2017. + */ +public class IntersectionTest { + + @Test + public void testFourArrays(){ + List<String> l1 = new ArrayList<String>(); + l1.add("1"); + l1.add("2"); + + List<String> l2 = new ArrayList<String>(); + l2.add("2"); + l2.add("3"); + + List<String> l3 = new ArrayList<String>(); + l3.add("2"); + l3.add("4"); + + List<String> l4 = new ArrayList<String>(); + l4.add("2"); + l4.add("5"); + + List<List<String>> all = new ArrayList<>(); + all.add(l1); + all.add(l2); + all.add(l3); + all.add(l4); + Intersection<String> m = new Intersection<>(); + List<String> ans = m.intersectMultipileArray(all); + Assert.assertEquals(1,ans.size()); + Assert.assertEquals(ans.get(0),"2"); + + } + + + + @Test + public void testTwoArrays(){ + List<String> l1 = new ArrayList<String>(); + l1.add("1"); + l1.add("2"); + + List<String> l2 = new ArrayList<String>(); + l2.add("2"); + l2.add("3"); + + List<List<String>> all = new ArrayList<>(); + all.add(l1); + all.add(l2); + Intersection<String> m = new Intersection<>(); + List<String> l3 = m.intersectMultipileArray(all); + Assert.assertEquals(l3.size(),1); + Assert.assertEquals(l3.get(0),"2"); + + } + + + @Test + public void testNoIntersection(){ + List<String> l1 = new ArrayList<String>(); + l1.add("1"); + l1.add("2"); + + List<String> l2 = new ArrayList<String>(); + l2.add("3"); + l2.add("4"); + + List<List<String>> all = new ArrayList<>(); + all.add(l1); + all.add(l2); + Intersection<String> m = new Intersection<>(); + List<String> l3 = m.intersectMultipileArray(all); + Assert.assertEquals(l3.size(),0); + + } + + @Test + public void testOneArrays(){ + List<String> l1 = new ArrayList<String>(); + l1.add("1"); + l1.add("2"); + List<List<String>> all = new ArrayList<>(); + all.add(l1); + Intersection<String> m = new Intersection<>(); + List<String> l3 = m.intersectMultipileArray(all); + Assert.assertEquals(l3.size(),2); + Assert.assertEquals(l3.get(0),"1"); + Assert.assertEquals(l3.get(1),"2"); + + } +} diff --git a/vid-app-common/src/test/java/org/onap/vid/testUtils/RegExMatcher.java b/vid-app-common/src/test/java/org/onap/vid/testUtils/RegExMatcher.java new file mode 100644 index 000000000..c88c8a835 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/testUtils/RegExMatcher.java @@ -0,0 +1,29 @@ +package org.onap.vid.testUtils; + +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; + +public class RegExMatcher extends TypeSafeMatcher<String> { + + private final String regEx; + + public RegExMatcher(String regEx) { + this.regEx = regEx; + } + + + @Override + public void describeTo(Description description) { + description.appendText("matches regEx="+regEx); + } + + + @Override + protected boolean matchesSafely(String item) { + return item.matches(regEx); + } + + public static RegExMatcher matchesRegEx(final String regEx) { + return new RegExMatcher(regEx); + } +} diff --git a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java new file mode 100644 index 000000000..73a79cd21 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java @@ -0,0 +1,68 @@ +package org.onap.vid.testUtils; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Assert; + +import java.util.Iterator; + +import static fj.parser.Parser.fail; + +/** + * Created by Oren on 6/7/17. + */ +public class TestUtils { + + /** + * The method compares between two jsons. the function assert that the actual object does not reduce or change the functionallity/parsing of the expected json. + * This means that if the expected JSON has a key which is null or the JSON doesn't have a key which contained in the expected JSON the assert will succeed and the will pass. + * For example : For JSON expected = {a:null} and actual {a:3} the test will pass + * Other example : For JSON expected = {a:3} and actual {a:null} the test will fail + * + * @param expected JSON + * @param actual JSON + */ + public static void assertJsonStringEqualsIgnoreNulls(String expected, String actual) { + if (expected == null || expected == JSONObject.NULL) {return;} + + JSONObject expectedJSON = new JSONObject(expected); + JSONObject actualJSON = new JSONObject(actual); + Iterator<?> keys = expectedJSON.keys(); + + while( keys.hasNext() ) { + String key = (String)keys.next(); + Object expectedValue = expectedJSON.get(key); + if (expectedValue == JSONObject.NULL){ + continue; + } + + Object actualValue = actualJSON.get(key); + + if (expectedValue instanceof JSONObject) { + String expectedVal = expectedValue.toString(); + String actualVal = actualValue.toString(); + assertJsonStringEqualsIgnoreNulls(expectedVal, actualVal); + } + else if (expectedValue instanceof JSONArray) { + if (actualValue instanceof JSONArray) { + JSONArray expectedJSONArray = (JSONArray)expectedValue; + JSONArray actualJSONArray = (JSONArray)actualValue; + for (int i = 0; i < expectedJSONArray.length(); i++) { + String expectedItem = expectedJSONArray.get(i).toString(); + String actualItem = actualJSONArray.get(i).toString(); + if (expectedValue instanceof JSONObject) + assertJsonStringEqualsIgnoreNulls(expectedItem, actualItem); + } + } + else { + fail("expected: " + expectedValue + " got:" + actualValue); + } + } + else { + Assert.assertEquals(expectedValue, actualValue); + } + } + } + + +} |