diff options
author | 2018-11-06 12:02:46 +0000 | |
---|---|---|
committer | 2018-11-08 17:54:13 +0000 | |
commit | cad452cdb6d999f822fe11dc4fdd232cce30ff6a (patch) | |
tree | a2fdfb5a689d7c2a6f045424a9f078a44ea36f4d /services/services-engine/src/test/java | |
parent | ad6efc439440e991df30694ef2da17d640f20bdf (diff) |
Refactor unit test data
There were many copies of test policies and examples
strewn through the Apex unit tests. This change cleans
up the unit tests so that a single version of all example
policies is used in all tests.
Also added a new relative file root command line parameter
to Apex to allow the root of relative paths in configuration
files to be set.
Apologies for the size of this review but unfortunately
all of this must be done in one shot.
Issue-ID: POLICY-1252
Change-Id: Ibbb18fbf18e3897a1c61301d0a65e62bc643a0e9
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
(cherry picked from commit 53d8916cc60d97e2ce7ae345f8cc25f5602567da)
Diffstat (limited to 'services/services-engine/src/test/java')
8 files changed, 302 insertions, 413 deletions
diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventGenerator.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventGenerator.java index 1ba901090..c2dbd48d6 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventGenerator.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventGenerator.java @@ -20,60 +20,27 @@ package org.onap.policy.apex.service.engine.event; -import java.util.Random; - /** * This class generates JSON event used for the test cases. * * @author Liam Fallon (liam.fallon@ericsson.com) */ public class JsonEventGenerator { - private static int nextEventNo = 0; - - /** - * Json events. - * - * @param eventCount the event count - * @return the string - */ - public static String jsonEvents(final int eventCount) { - final StringBuilder builder = new StringBuilder(); - - for (int i = 0; i < eventCount; i++) { - if (i > 0) { - builder.append("\n"); - } - builder.append(jsonEvent()); - } - - return builder.toString(); - } - /** * Json event. * * @return the string */ public static String jsonEvent() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12345\n"); builder.append("}"); return builder.toString(); @@ -85,25 +52,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventNoName() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"namez\": \"" + eventName + "\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"namez\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12346\n"); builder.append("}"); return builder.toString(); @@ -115,24 +72,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventBadName() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); builder.append(" \"name\": \"%%%%\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12347\n"); builder.append("}"); return builder.toString(); @@ -144,23 +92,14 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventNoExName() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); builder.append(" \"name\": \"I_DONT_EXIST\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12348\n"); builder.append("}"); return builder.toString(); @@ -172,25 +111,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventNoVersion() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"versiion\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12349\n"); builder.append("}"); return builder.toString(); @@ -202,25 +131,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventBadVersion() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"#####\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12350\n"); builder.append("}"); return builder.toString(); @@ -232,24 +151,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventNoExVersion() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"Event0000\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"1.2.3\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12351\n"); builder.append("}"); return builder.toString(); @@ -261,25 +171,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventNoNamespace() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpacee\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"nameSpacee\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12352\n"); builder.append("}"); return builder.toString(); @@ -291,25 +191,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventBadNamespace() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); builder.append(" \"nameSpace\": \"hello.&&&&\",\n"); - builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12353\n"); builder.append("}"); return builder.toString(); @@ -321,24 +211,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventNoExNamespace() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); builder.append(" \"nameSpace\": \"pie.in.the.sky\",\n"); - builder.append(" \"name\": \"Event0000\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12354\n"); builder.append("}"); return builder.toString(); @@ -350,25 +231,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventNoSource() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"sourcee\": \"test\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12355\n"); builder.append("}"); return builder.toString(); @@ -380,25 +251,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventBadSource() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"%!@**@!\",\n"); builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12356\n"); builder.append("}"); return builder.toString(); @@ -410,25 +271,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventNoTarget() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"targett\": \"apex\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12357\n"); builder.append("}"); return builder.toString(); @@ -440,25 +291,15 @@ public class JsonEventGenerator { * @return the string */ public static String jsonEventBadTarget() { - final Random rand = new Random(); - final StringBuilder builder = new StringBuilder(); - int nextEventNo = rand.nextInt(2); - final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); - final int nextMatchCase = rand.nextInt(4); - final float nextTestTemperature = rand.nextFloat() * 10000; - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"KNIO(*S)A(S)D\",\n"); - builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); - builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); - builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); - builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append(" \"intPar\": 12358\n"); builder.append("}"); return builder.toString(); @@ -473,8 +314,8 @@ public class JsonEventGenerator { final StringBuilder builder = new StringBuilder(); builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"Event0000\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"apex\"\n"); @@ -492,49 +333,14 @@ public class JsonEventGenerator { final StringBuilder builder = new StringBuilder(); builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); - builder.append(" \"name\": \"Event0000\",\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); builder.append(" \"version\": \"0.0.1\",\n"); builder.append(" \"source\": \"test\",\n"); builder.append(" \"target\": \"Apex\",\n"); - builder.append(" \"TestSlogan\": null,\n"); - builder.append(" \"TestMatchCase\": -1,\n"); - builder.append(" \"TestTimestamp\": -1,\n"); - builder.append(" \"TestTemperature\": -1.0\n"); + builder.append(" \"intPar\": -1\n"); builder.append("}"); return builder.toString(); } - - /** - * The main method. - * - * @param args the arguments - */ - public static void main(final String[] args) { - if (args.length != 1) { - System.err.println("usage EventGenerator #events"); - return; - } - - int eventCount = 0; - try { - eventCount = Integer.parseInt(args[0]); - } catch (final Exception e) { - System.err.println("usage EventGenerator #events"); - e.printStackTrace(); - return; - } - - System.out.println(jsonEvents(eventCount)); - } - - /** - * Gets the next event no. - * - * @return the next event no - */ - public static int getNextEventNo() { - return nextEventNo; - } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventHandler.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventHandler.java index 6e462a3e1..fa399e8c0 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventHandler.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventHandler.java @@ -22,12 +22,12 @@ package org.onap.policy.apex.service.engine.event; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.IOException; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -68,7 +68,7 @@ public class TestJsonEventHandler { @BeforeClass public static void setupEventModel() throws IOException, ApexModelException { final String policyModelString = TextFileUtils - .getTextFileAsString("src/test/resources/policymodels/SamplePolicyModelMVEL.json"); + .getTextFileAsString("src/test/resources/policymodels/SmallModel.json"); final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<AxPolicyModel>(AxPolicyModel.class); final AxPolicyModel apexPolicyModel = modelReader.read(new ByteArrayInputStream(policyModelString.getBytes())); @@ -118,15 +118,15 @@ public class TestJsonEventHandler { logger.debug(apexEvent.toString()); - assertTrue(apexEvent.getName().equals("Event0000") || apexEvent.getName().equals("Event0100")); - assertTrue(apexEvent.getVersion().equals("0.0.1")); - assertTrue(apexEvent.getNameSpace().equals("org.onap.policy.apex.sample.events")); - assertTrue(apexEvent.getSource().equals("test")); - assertTrue(apexEvent.getTarget().equals("apex")); - assertTrue(apexEvent.get("TestSlogan").toString().startsWith("Test slogan for External Event")); + assertEquals("BasicEvent", apexEvent.getName()); + assertEquals("0.0.1", apexEvent.getVersion()); + assertEquals("org.onap.policy.apex.events", apexEvent.getNameSpace()); + assertEquals("test", apexEvent.getSource()); + assertEquals("apex", apexEvent.getTarget()); + assertEquals(12345, apexEvent.get("intPar")); final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected"); - assertTrue(testMatchCaseSelected == null); + assertNull(testMatchCaseSelected); } } catch (final Exception e) { e.printStackTrace(); @@ -162,8 +162,8 @@ public class TestJsonEventHandler { jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); fail("Test should throw an exception here"); } catch (final ApexEventException e) { - assertTrue(e.getMessage().startsWith( - "Failed to unmarshal JSON event: field \"name\" with value \"%%%%\" is invalid")); + assertEquals("Failed to unmarshal JSON event: field \"name\" with value \"%%%%\" is invalid", + e.getMessage().substring(0, 73)); } try { @@ -184,8 +184,8 @@ public class TestJsonEventHandler { jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); fail("Test should throw an exception here"); } catch (final ApexEventException e) { - assertTrue(e.getMessage().startsWith( - "Failed to unmarshal JSON event: field \"version\" with value \"#####\" is invalid")); + assertEquals("Failed to unmarshal JSON event: field \"version\" with value \"#####\" is invalid", + e.getMessage().substring(0, 77)); } try { @@ -193,22 +193,23 @@ public class TestJsonEventHandler { jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); fail("Test should throw an exception here"); } catch (final ApexEventException e) { - assertTrue(e.getMessage() - .startsWith("Failed to unmarshal JSON event: an event definition for an event named " - + "\"Event0000\" with version \"1.2.3\" not found in Apex model")); + assertEquals("Failed to unmarshal JSON event: an event definition for an event named " + + "\"BasicEvent\" with version \"1.2.3\" not found in Apex model", + e.getMessage().substring(0, 128)); } apexEventJsonStringIn = JsonEventGenerator.jsonEventNoNamespace(); event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals("org.onap.policy.apex.sample.events", event.getNameSpace()); + assertEquals("org.onap.policy.apex.events", event.getNameSpace()); try { apexEventJsonStringIn = JsonEventGenerator.jsonEventBadNamespace(); jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); fail("Test should throw an exception here"); } catch (final ApexEventException e) { - assertTrue(e.getMessage().startsWith("Failed to unmarshal JSON event: field \"nameSpace\" " - + "with value \"hello.&&&&\" is invalid")); + assertEquals("Failed to unmarshal JSON event: " + + "field \"nameSpace\" with value \"hello.&&&&\" is invalid", + e.getMessage().substring(0, 84)); } try { @@ -216,37 +217,36 @@ public class TestJsonEventHandler { jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); fail("Test should throw an exception here"); } catch (final ApexEventException e) { - assertTrue(e.getMessage() - .startsWith("Failed to unmarshal JSON event: namespace \"pie.in.the.sky\" " - + "on event \"Event0000\" does not" - + " match namespace \"org.onap.policy.apex.sample.events\" " - + "for that event in the Apex model")); + assertEquals("Failed to unmarshal JSON event: namespace \"pie.in.the.sky\" " + + "on event \"BasicEvent\" does not" + + " match namespace \"org.onap.policy.apex.events\" " + + "for that event in the Apex model", e.getMessage().substring(0, 168)); } apexEventJsonStringIn = JsonEventGenerator.jsonEventNoSource(); event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals("Outside", event.getSource()); + assertEquals("source", event.getSource()); try { apexEventJsonStringIn = JsonEventGenerator.jsonEventBadSource(); jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); fail("Test should throw an exception here"); } catch (final ApexEventException e) { - assertTrue(e.getMessage().startsWith( - "Failed to unmarshal JSON event: field \"source\" with value \"%!@**@!\" is invalid")); + assertEquals("Failed to unmarshal JSON event: field \"source\" with value \"%!@**@!\" is invalid", + e.getMessage().substring(0, 78)); } apexEventJsonStringIn = JsonEventGenerator.jsonEventNoTarget(); event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals("Match", event.getTarget()); + assertEquals("target", event.getTarget()); try { apexEventJsonStringIn = JsonEventGenerator.jsonEventBadTarget(); jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); fail("Test should throw an exception here"); } catch (final ApexEventException e) { - assertTrue(e.getMessage().startsWith("Failed to unmarshal JSON event: field \"target\" " - + "with value \"KNIO(*S)A(S)D\" is invalid")); + assertEquals("Failed to unmarshal JSON event: field \"target\" with value \"KNIO(*S)A(S)D\" is invalid", + e.getMessage().substring(0, 84)); } try { @@ -254,30 +254,24 @@ public class TestJsonEventHandler { jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); fail("Test should throw an exception here"); } catch (final ApexEventException e) { - assertTrue(e.getMessage().startsWith("Failed to unmarshal JSON event: error parsing Event0000:0.0.1 " - + "event from Json. Field \"TestMatchCase\" is missing, but is mandatory.")); + assertEquals("Failed to unmarshal JSON event: error parsing BasicEvent:0.0.1 " + + "event from Json. Field \"intPar\" is missing, but is mandatory.", + e.getMessage().substring(0, 124)); } apexEventJsonStringIn = JsonEventGenerator.jsonEventNullFields(); event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); assertEquals(null, event.get("TestSlogan")); - assertEquals((byte) -1, event.get("TestMatchCase")); - assertEquals((long) -1, event.get("TestTimestamp")); - assertEquals(-1.0, event.get("TestTemperature")); + assertEquals(-1, event.get("intPar")); // Set the missing fields as optional in the model - final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get("Event0000"); - eventDefinition.getParameterMap().get("TestSlogan").setOptional(true); - eventDefinition.getParameterMap().get("TestMatchCase").setOptional(true); - eventDefinition.getParameterMap().get("TestTimestamp").setOptional(true); - eventDefinition.getParameterMap().get("TestTemperature").setOptional(true); + final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get("BasicEvent"); + eventDefinition.getParameterMap().get("intPar").setOptional(true); apexEventJsonStringIn = JsonEventGenerator.jsonEventMissingFields(); event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); assertEquals(null, event.get("TestSlogan")); - assertEquals(null, event.get("TestMatchCase")); - assertEquals(null, event.get("TestTimestamp")); - assertEquals(null, event.get("TestTemperature")); + assertEquals(null, event.get("intPar")); } catch (final Exception e) { e.printStackTrace(); throw new ApexException("Exception reading Apex event JSON file", e); @@ -296,62 +290,23 @@ public class TestJsonEventHandler { jsonEventConverter.init(new JsonEventProtocolParameters()); assertNotNull(jsonEventConverter); - final Date event0000StartTime = new Date(); - final Map<String, Object> event0000DataMap = new HashMap<String, Object>(); - event0000DataMap.put("TestSlogan", "This is a test slogan"); - event0000DataMap.put("TestMatchCase", 12345); - event0000DataMap.put("TestTimestamp", event0000StartTime.getTime()); - event0000DataMap.put("TestTemperature", 34.5445667); + final Map<String, Object> basicEventMap = new HashMap<String, Object>(); + basicEventMap.put("intPar", 12345); - final ApexEvent apexEvent0000 = new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.sample.events", - "test", "apex"); - apexEvent0000.putAll(event0000DataMap); + final ApexEvent basicEvent = new ApexEvent("BasicEvent", "0.0.1", "org.onap.policy.apex.events", "test", + "apex"); + basicEvent.putAll(basicEventMap); - final String apexEvent0000JsonString = (String) jsonEventConverter.fromApexEvent(apexEvent0000); + final String apexEvent0000JsonString = (String) jsonEventConverter.fromApexEvent(basicEvent); logger.debug(apexEvent0000JsonString); - assertTrue(apexEvent0000JsonString.contains("\"name\": \"Event0000\"")); + assertTrue(apexEvent0000JsonString.contains("\"name\": \"BasicEvent\"")); assertTrue(apexEvent0000JsonString.contains("\"version\": \"0.0.1\"")); - assertTrue(apexEvent0000JsonString.contains("\"nameSpace\": \"org.onap.policy.apex.sample.events\"")); + assertTrue(apexEvent0000JsonString.contains("\"nameSpace\": \"org.onap.policy.apex.events\"")); assertTrue(apexEvent0000JsonString.contains("\"source\": \"test\"")); assertTrue(apexEvent0000JsonString.contains("\"target\": \"apex\"")); - assertTrue(apexEvent0000JsonString.contains("\"TestSlogan\": \"This is a test slogan\"")); - assertTrue(apexEvent0000JsonString.contains("\"TestMatchCase\": 12345")); - assertTrue(apexEvent0000JsonString.contains("\"TestTimestamp\": " + event0000StartTime.getTime())); - assertTrue(apexEvent0000JsonString.contains("\"TestTemperature\": 34.5445667")); - - final Date event0004StartTime = new Date(1434363272000L); - final Map<String, Object> event0004DataMap = new HashMap<String, Object>(); - event0004DataMap.put("TestSlogan", "Test slogan for External Event"); - event0004DataMap.put("TestMatchCase", new Integer(2)); - event0004DataMap.put("TestTimestamp", new Long(event0004StartTime.getTime())); - event0004DataMap.put("TestTemperature", new Double(1064.43)); - event0004DataMap.put("TestMatchCaseSelected", new Integer(2)); - event0004DataMap.put("TestMatchStateTime", new Long(1434370506078L)); - event0004DataMap.put("TestEstablishCaseSelected", new Integer(0)); - event0004DataMap.put("TestEstablishStateTime", new Long(1434370506085L)); - event0004DataMap.put("TestDecideCaseSelected", new Integer(3)); - event0004DataMap.put("TestDecideStateTime", new Long(1434370506092L)); - event0004DataMap.put("TestActCaseSelected", new Integer(2)); - event0004DataMap.put("TestActStateTime", new Long(1434370506095L)); - - final ApexEvent apexEvent0004 = new ApexEvent("Event0004", "0.0.1", "org.onap.policy.apex.sample.events", - "test", "apex"); - apexEvent0004.putAll(event0004DataMap); - - final String apexEvent0004JsonString = (String) jsonEventConverter.fromApexEvent(apexEvent0004); - - logger.debug(apexEvent0004JsonString); - - assertTrue(apexEvent0004JsonString.contains("\"name\": \"Event0004\"")); - assertTrue(apexEvent0004JsonString.contains("\"version\": \"0.0.1\"")); - assertTrue(apexEvent0004JsonString.contains("\"nameSpace\": \"org.onap.policy.apex.sample.events\"")); - assertTrue(apexEvent0004JsonString.contains("\"source\": \"test\"")); - assertTrue(apexEvent0004JsonString.contains("\"target\": \"apex\"")); - assertTrue(apexEvent0004JsonString.contains("\"TestSlogan\": \"Test slogan for External Event\"")); - assertTrue(apexEvent0004JsonString.contains("1434370506078")); - assertTrue(apexEvent0004JsonString.contains("1064.43")); + assertTrue(apexEvent0000JsonString.contains("\"intPar\": 12345")); } catch (final Exception e) { e.printStackTrace(); throw new ApexException("Exception reading Apex event JSON file", e); diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/TestApexCommandLineArguments.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/TestApexCommandLineArguments.java index 2b8bd6771..c26e4fcb7 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/TestApexCommandLineArguments.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/TestApexCommandLineArguments.java @@ -24,20 +24,27 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import org.junit.After; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; /** * Test Apex Command Line Arguments. + * * @author Liam Fallon (liam.fallon@ericsson.com) */ public class TestApexCommandLineArguments { + @After + public void clearRelativeFileRoot() { + System.clearProperty("APEX_RELATIVE_FILE_ROOT"); + } @Test public void testCommandLineArguments() { final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments(); - final String[] args00 = {""}; + final String[] args00 = + { "" }; try { apexArguments.parse(args00); apexArguments.validate(); @@ -46,7 +53,8 @@ public class TestApexCommandLineArguments { assertEquals("Apex configuration file was not specified as an argument", e.getMessage()); } - final String[] args01 = {"-h"}; + final String[] args01 = + { "-h" }; try { final String result = apexArguments.parse(args01); assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); @@ -55,7 +63,8 @@ public class TestApexCommandLineArguments { fail("Test should not throw an exception"); } - final String[] args02 = {"-v"}; + final String[] args02 = + { "-v" }; try { final String result = apexArguments.parse(args02); assertTrue(result.startsWith("Apex Adaptive Policy Engine")); @@ -64,7 +73,8 @@ public class TestApexCommandLineArguments { fail("Test should not throw an exception"); } - final String[] args03 = {"-v", "-h"}; + final String[] args03 = + { "-v", "-h" }; try { final String result = apexArguments.parse(args03); assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); @@ -73,7 +83,8 @@ public class TestApexCommandLineArguments { fail("Test should not throw an exception"); } - final String[] args04 = {"-h", "-v"}; + final String[] args04 = + { "-h", "-v" }; try { final String result = apexArguments.parse(args04); assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); @@ -82,14 +93,16 @@ public class TestApexCommandLineArguments { fail("Test should not throw an exception"); } - final String[] args05 = {"-a"}; + final String[] args05 = + { "-a" }; try { apexArguments.parse(args05); } catch (final ApexException e) { assertEquals("invalid command line arguments specified : Unrecognized option: -a", e.getMessage()); } - final String[] args06 = {"-c", "hello", "-m", "goodbye", "-h", "-v"}; + final String[] args06 = + { "-c", "hello", "-m", "goodbye", "-h", "-v" }; try { final String result = apexArguments.parse(args06); assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); @@ -97,13 +110,14 @@ public class TestApexCommandLineArguments { assertEquals("invalid command line arguments specified : Unrecognized option: -a", e.getMessage()); } - final String[] args07 = {"-c", "hello", "-m", "goodbye", "-h", "aaa"}; + final String[] args07 = + { "-c", "hello", "-m", "goodbye", "-h", "aaa" }; try { final String result = apexArguments.parse(args07); assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); } catch (final ApexException e) { assertEquals("too many command line arguments specified : [-c, hello, -m, goodbye, -h, aaa]", - e.getMessage()); + e.getMessage()); } } @@ -111,7 +125,8 @@ public class TestApexCommandLineArguments { public void testCommandLineFileParameters() { final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments(); - final String[] args00 = {"-c", "zooby"}; + final String[] args00 = + { "-c", "zooby" }; try { apexArguments.parse(args00); apexArguments.validate(); @@ -120,7 +135,8 @@ public class TestApexCommandLineArguments { assertEquals("Apex configuration file \"zooby\" does not exist", e.getMessage()); } - final String[] args01 = {"-c"}; + final String[] args01 = + { "-c" }; try { apexArguments.parse(args01); apexArguments.validate(); @@ -129,7 +145,8 @@ public class TestApexCommandLineArguments { assertEquals("invalid command line arguments specified : Missing argument for option: c", e.getMessage()); } - final String[] args02 = {"-c", "src/test/resources/parameters/goodParams.json"}; + final String[] args02 = + { "-c", "src/test/resources/parameters/goodParams.json" }; try { apexArguments.parse(args02); apexArguments.validate(); @@ -138,7 +155,8 @@ public class TestApexCommandLineArguments { fail("Test should not throw an exception"); } - final String[] args03 = {"-c", "src/test/resources/parameters/goodParams.json", "-m", "zooby"}; + final String[] args03 = + { "-c", "src/test/resources/parameters/goodParams.json", "-m", "zooby" }; try { apexArguments.parse(args03); apexArguments.validate(); @@ -147,7 +165,8 @@ public class TestApexCommandLineArguments { assertEquals("Apex model file \"zooby\" does not exist", e.getMessage()); } - final String[] args04 = {"-m"}; + final String[] args04 = + { "-m" }; try { apexArguments.parse(args04); apexArguments.validate(); @@ -156,7 +175,8 @@ public class TestApexCommandLineArguments { assertEquals("invalid command line arguments specified : Missing argument for option: m", e.getMessage()); } - final String[] args05 = {"-c", "src/test/resources/parameters/goodParams.json", "-m"}; + final String[] args05 = + { "-c", "src/test/resources/parameters/goodParams.json", "-m" }; try { apexArguments.parse(args05); apexArguments.validate(); @@ -165,8 +185,9 @@ public class TestApexCommandLineArguments { assertEquals("invalid command line arguments specified : Missing argument for option: m", e.getMessage()); } - final String[] args06 = {"-c", "src/test/resources/parameters/goodParams.json", "-m", - "src/test/resources/main/DummyModelFile.json"}; + final String[] args06 = + { "-c", "src/test/resources/parameters/goodParams.json", "-m", + "src/test/resources/main/DummyModelFile.json" }; try { apexArguments.parse(args06); apexArguments.validate(); @@ -175,7 +196,8 @@ public class TestApexCommandLineArguments { fail("Test should not throw an exception"); } - final String[] args07 = {"-c", "parameters/goodParams.json", "-m", "main/DummyModelFile.json"}; + final String[] args07 = + { "-c", "parameters/goodParams.json", "-m", "main/DummyModelFile.json" }; try { apexArguments.parse(args07); apexArguments.validate(); @@ -184,4 +206,57 @@ public class TestApexCommandLineArguments { fail("Test should not throw an exception"); } } + + @Test + public void testCommandLineRelativeRootParameters() { + final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments(); + + final String[] args00 = + { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "zooby" }; + try { + apexArguments.parse(args00); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertTrue(e.getMessage().contains("zooby\" does not exist or is not a directory")); + } + + final String[] args01 = + { "-rfr" }; + try { + apexArguments.parse(args01); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertEquals("invalid command line arguments specified : Missing argument for option: rfr", e.getMessage()); + } + + final String[] args02 = + { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "pom.xml" }; + try { + apexArguments.parse(args02); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertTrue(e.getMessage().contains("pom.xml\" does not exist or is not a directory")); + } + + final String[] args03 = + { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "target" }; + try { + apexArguments.parse(args03); + apexArguments.validate(); + } catch (final ApexException e) { + fail("Test should not throw an exception here"); + } + + final String[] args04 = + { "-c", "src/test/resources/parameters/goodParamsRelative.json", "-rfr", "src/test/resources" }; + try { + apexArguments.parse(args04); + apexArguments.validate(); + } catch (final ApexException e) { + fail("Test should not throw an exception here"); + } + } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java index 5e42ede1a..e77767d44 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java @@ -21,8 +21,10 @@ package org.onap.policy.apex.service.engine.parameters; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import org.junit.After; import org.junit.Test; import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; import org.onap.policy.apex.service.parameters.ApexParameterHandler; @@ -35,16 +37,21 @@ import org.onap.policy.common.parameters.ParameterException; * @author Liam Fallon (liam.fallon@ericsson.com) */ public class ExecutorParameterTests { + @After + public void resetRelativeFileRoot() { + System.clearProperty("APEX_RELATIVE_FILE_ROOT"); + } @Test public void noParamsTest() { - final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorNoParams.json"}; + final String[] args = + { "-c", "src/test/resources/parameters/serviceExecutorNoParams.json" }; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); try { final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); - assertEquals(0, - parameters.getEngineServiceParameters().getEngineParameters().getExecutorParameterMap().size()); + assertEquals(0, parameters.getEngineServiceParameters().getEngineParameters().getExecutorParameterMap() + .size()); } catch (final ParameterException e) { fail("This test should not throw any exception: " + e.getMessage()); } @@ -52,24 +59,25 @@ public class ExecutorParameterTests { @Test public void badParamsTest() { - final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadParams.json"}; + final String[] args = + { "-c", "src/test/resources/parameters/serviceExecutorBadParams.json" }; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); try { new ApexParameterHandler().getParameters(arguments); fail("This test should throw an exception"); } catch (final ParameterException e) { - assertEquals( - "error reading parameters from \"src/test/resources/parameters/serviceExecutorBadParams.json\"\n" + assertEquals("error reading parameters from " + + "\"src/test/resources/parameters/serviceExecutorBadParams.json\"\n" + "(ParameterRuntimeException):value of \"executorParameters:ZOOBY\" entry is not " - + "a parameter JSON object", - e.getMessage()); + + "a parameter JSON object", e.getMessage()); } } @Test public void noExecutorParamsTest() { - final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorNoExecutorParams.json"}; + final String[] args = + { "-c", "src/test/resources/parameters/serviceExecutorNoExecutorParams.json" }; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); try { @@ -77,15 +85,16 @@ public class ExecutorParameterTests { fail("This test should throw an exception"); } catch (final ParameterException e) { assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceExecutorNoExecutorParams.json\"\n" - + "(ParameterRuntimeException):no \"executorParameters\" entry found in parameters," - + " at least one executor parameter entry must be specified", e.getMessage()); + + "\"src/test/resources/parameters/serviceExecutorNoExecutorParams.json\"\n" + + "(ParameterRuntimeException):no \"executorParameters\" entry found in parameters," + + " at least one executor parameter entry must be specified", e.getMessage()); } } @Test public void emptyParamsTest() { - final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorEmptyParams.json"}; + final String[] args = + { "-c", "src/test/resources/parameters/serviceExecutorEmptyParams.json" }; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); try { @@ -93,15 +102,16 @@ public class ExecutorParameterTests { fail("This test should throw an exception"); } catch (final ParameterException e) { assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceExecutorEmptyParams.json\"\n" - + "(ParameterRuntimeException):could not find field \"parameterClassName\" " - + "in \"executorParameters:ZOOBY\" entry", e.getMessage()); + + "\"src/test/resources/parameters/serviceExecutorEmptyParams.json\"\n" + + "(ParameterRuntimeException):could not find field \"parameterClassName\" " + + "in \"executorParameters:ZOOBY\" entry", e.getMessage()); } } @Test public void badPluginParamNameTest() { - final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginNameParams.json"}; + final String[] args = + { "-c", "src/test/resources/parameters/serviceExecutorBadPluginNameParams.json" }; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); try { @@ -109,15 +119,16 @@ public class ExecutorParameterTests { fail("This test should throw an exception"); } catch (final ParameterException e) { assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceExecutorBadPluginNameParams.json\"\n" - + "(ParameterRuntimeException):could not find field \"parameterClassName\" " - + "in \"executorParameters:ZOOBY\" entry", e.getMessage()); + + "\"src/test/resources/parameters/serviceExecutorBadPluginNameParams.json\"\n" + + "(ParameterRuntimeException):could not find field \"parameterClassName\" " + + "in \"executorParameters:ZOOBY\" entry", e.getMessage()); } } @Test public void badPluginParamObjectTest() { - final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginValueObjectParams.json"}; + final String[] args = + { "-c", "src/test/resources/parameters/serviceExecutorBadPluginValueObjectParams.json" }; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); try { @@ -125,15 +136,16 @@ public class ExecutorParameterTests { fail("This test should throw an exception"); } catch (final ParameterException e) { assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceExecutorBadPluginValueObjectParams.json\"\n" - + "(ParameterRuntimeException):value for field \"parameterClassName\" " - + "of \"executorParameters:LOOBY\" entry is not a plain string", e.getMessage()); + + "\"src/test/resources/parameters/serviceExecutorBadPluginValueObjectParams.json\"\n" + + "(ParameterRuntimeException):value for field \"parameterClassName\" " + + "of \"executorParameters:LOOBY\" entry is not a plain string", e.getMessage()); } } @Test public void badPluginParamBlankTest() { - final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginValueBlankParams.json"}; + final String[] args = + { "-c", "src/test/resources/parameters/serviceExecutorBadPluginValueBlankParams.json" }; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); try { @@ -141,16 +153,16 @@ public class ExecutorParameterTests { fail("This test should throw an exception"); } catch (final ParameterException e) { assertEquals("error reading parameters from " - + "\"src/test/resources/parameters/serviceExecutorBadPluginValueBlankParams.json\"\n" - + "(ParameterRuntimeException):value for field \"parameterClassName\" " - + "in \"executorParameters:LOOBY\" entry is not specified or is blank", e.getMessage()); + + "\"src/test/resources/parameters/serviceExecutorBadPluginValueBlankParams.json\"\n" + + "(ParameterRuntimeException):value for field \"parameterClassName\" " + + "in \"executorParameters:LOOBY\" entry is not specified or is blank", e.getMessage()); } } - @Test public void badPluginParamValueTest() { - final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginValueParams.json"}; + final String[] args = + { "-c", "src/test/resources/parameters/serviceExecutorBadPluginValueParams.json" }; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); try { @@ -158,16 +170,36 @@ public class ExecutorParameterTests { fail("This test should throw an exception"); } catch (final ParameterException e) { assertEquals("error reading parameters from" - + " \"src/test/resources/parameters/serviceExecutorBadPluginValueParams.json\"\n" - + "(ParameterRuntimeException):failed to deserialize the parameters " - + "for \"executorParameters:LOOBY\" to parameter class \"helloworld\"\n" - + "java.lang.ClassNotFoundException: helloworld", e.getMessage()); + + " \"src/test/resources/parameters/serviceExecutorBadPluginValueParams.json\"\n" + + "(ParameterRuntimeException):failed to deserialize the parameters " + + "for \"executorParameters:LOOBY\" to parameter class \"helloworld\"\n" + + "java.lang.ClassNotFoundException: helloworld", e.getMessage()); } } @Test public void goodParametersTest() { - final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"}; + final String[] args = + { "-c", "src/test/resources/parameters/goodParams.json" }; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); + assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); + assertEquals(45, parameters.getEngineServiceParameters().getId()); + assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); + assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + } catch (final ParameterException e) { + fail("This test should not throw any exception: " + e.getMessage()); + } + } + + @Test + public void relativeParametersTest() { + final String[] args = + { "-rfr", "src/test/resources", "-c", "src/test/resources/parameters/goodParamsRelative.json" }; final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); try { @@ -178,6 +210,8 @@ public class ExecutorParameterTests { assertEquals(45, parameters.getEngineServiceParameters().getId()); assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + assertTrue(parameters.getEngineServiceParameters().getPolicyModelFileName() + .endsWith("/src/test/resources/policymodels/SmallModel.json")); } catch (final ParameterException e) { fail("This test should not throw any exception: " + e.getMessage()); } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java index 7b71253b0..61518b84f 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java @@ -150,7 +150,7 @@ public class ParameterTests { + " field \"deploymentPort\" type \"int\" value \"65536\" INVALID, " + "deploymentPort [65536] invalid, must be specified as 1024 <= port <= 65535\n" + " field \"policyModelFileName\" type \"java.lang.String\" " - + "value \"/some/file/name.xml\" INVALID, not found or is not a plain file\n" + + "value \"/some/file/name.xml\" INVALID, not found\n" + " parameter group map \"eventOutputParameters\" INVALID, " + "parameter group has status INVALID\n" + " parameter group \"FirstProducer\" type " + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" @@ -159,8 +159,7 @@ public class ParameterTests { + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " + "parameter group has status INVALID\n" + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, " - + "fileName not specified or is blank or null, " - + "it must be specified as a valid file location\n" + + "\"null\" invalid, must be specified as a non-empty string\n" + " parameter group map \"eventInputParameters\" INVALID, " + "parameter group has status INVALID\n" + " parameter group \"TheFileConsumer1\" type " + "\"org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters\" INVALID" @@ -169,8 +168,29 @@ public class ParameterTests { + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " + "parameter group has status INVALID\n" + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, " - + "fileName not specified or is blank or null, " - + "it must be specified as a valid file location\n", e.getMessage()); + + "\"null\" invalid, must be specified as a non-empty string\n", e.getMessage()); + } + } + + @Test + public void modelNotFileTest() { + final String[] args = + { "-c", "src/test/resources/parameters/badParamsModelNotFile.json" }; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ParameterException e) { + assertEquals("validation error(s) on parameters from " + + "\"src/test/resources/parameters/badParamsModelNotFile.json\"\n" + + "parameter group \"APEX_PARAMETERS\" type " + + "\"org.onap.policy.apex.service.parameters.ApexParameters\" INVALID, " + + "parameter group has status INVALID\n" + " parameter group \"MyApexEngine\" type " + + "\"org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters\" " + + "INVALID, parameter group has status INVALID\n" + " field \"policyModelFileName\" " + + "type \"java.lang.String\" value \"src/test\" INVALID, is not a plain file\n", + e.getMessage()); } } @@ -257,8 +277,8 @@ public class ParameterTests { assertTrue(prodCarrierTech instanceof SuperDooperCarrierTechnologyParameters); - final SuperDooperCarrierTechnologyParameters superDooperParameters = - (SuperDooperCarrierTechnologyParameters) prodCarrierTech; + final SuperDooperCarrierTechnologyParameters superDooperParameters + = (SuperDooperCarrierTechnologyParameters) prodCarrierTech; assertEquals("somehost:12345", superDooperParameters.getBootstrapServers()); assertEquals("0", superDooperParameters.getAcks()); assertEquals(25, superDooperParameters.getRetries()); diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java index 374a2df14..87f1adc02 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.service.engine.parameters; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.Test; @@ -120,8 +121,7 @@ public class ProducerConsumerTests { + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " + "parameter group has status INVALID\n" + " field \"fileName\" type \"java.lang.String\" value \"null\" INVALID, " - + "fileName not specified or is blank or null, " - + "it must be specified as a valid file location\n", e.getMessage()); + + "\"null\" invalid, must be specified as a non-empty string\n", e.getMessage()); } } @@ -191,7 +191,7 @@ public class ProducerConsumerTests { final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); final FileCarrierTechnologyParameters fileParams = (FileCarrierTechnologyParameters) parameters .getEventOutputParameters().get("aProducer").getCarrierTechnologyParameters(); - assertEquals("/tmp/aaa.json", fileParams.getFileName()); + assertTrue(fileParams.getFileName().endsWith("target/aaa.json")); assertEquals(false, fileParams.isStandardError()); assertEquals(false, fileParams.isStandardIo()); assertEquals(false, fileParams.isStreamingMode()); @@ -223,8 +223,7 @@ public class ProducerConsumerTests { + "filecarrierplugin.FileCarrierTechnologyParameters\" INVALID, " + "parameter group has status INVALID\n" + " field \"fileName\" type " + "\"java.lang.String\" value \"null\" INVALID, " - + "fileName not specified or is blank or null, " - + "it must be specified as a valid file location\n", e.getMessage()); + + "\"null\" invalid, must be specified as a non-empty string\n", e.getMessage()); } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImplTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImplTest.java index f68a4872b..fcb207c62 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImplTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImplTest.java @@ -59,7 +59,7 @@ import org.onap.policy.common.parameters.ParameterService; public class EngineServiceImplTest { private static String simpleModelString; - private static String mfpModelString; + private static String differentModelString; private static AxPolicyModel simpleModel; /** @@ -71,9 +71,10 @@ public class EngineServiceImplTest { @BeforeClass public static void readSimpleModel() throws IOException, ApexModelException { simpleModelString = TextFileUtils - .getTextFileAsString("src/test/resources/policymodels/SamplePolicyModelJAVASCRIPT.json"); + .getTextFileAsString("src/test/resources/policymodels/SmallModel.json"); - mfpModelString = TextFileUtils.getTextFileAsString("src/test/resources/policymodels/MyFirstPolicyModel.json"); + differentModelString = TextFileUtils + .getTextFileAsString("src/test/resources/policymodels/SmallModelDifferent.json"); final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class); simpleModel = modelReader.read(new ByteArrayInputStream(simpleModelString.getBytes())); @@ -177,7 +178,7 @@ public class EngineServiceImplTest { } catch (Exception apEx) { assertEquals("removeEventListener()<-Engine-0:0.0.1,STOPPED, listenerName is null", apEx.getMessage()); } - + esImpl.deregisterActionListener("DummyListener"); assertEquals(esImpl, esImpl.getEngineServiceEventInterface()); @@ -425,17 +426,17 @@ public class EngineServiceImplTest { } try { - esImpl.updateModel(config.getEngineKey(), mfpModelString, false); + esImpl.updateModel(config.getEngineKey(), differentModelString, false); fail("test should throw an exception"); } catch (ApexException apEx) { - assertEquals("apex model update failed, supplied model with key \"MyFirstPolicyModel:0.0.1\" is not a " + assertEquals("apex model update failed, supplied model with key \"SmallModelDifferent:0.0.1\" is not a " + "compatible model update " - + "from the existing engine model with key \"SamplePolicyModelJAVASCRIPT:0.0.1\"", + + "from the existing engine model with key \"SmallModel:0.0.1\"", apEx.getMessage()); } try { - esImpl.updateModel(config.getEngineKey(), mfpModelString, true); + esImpl.updateModel(config.getEngineKey(), differentModelString, true); } catch (ApexException apEx) { fail("test should not throw an exception"); } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorkerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorkerTest.java index 1f8052d4a..87a523506 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorkerTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorkerTest.java @@ -64,7 +64,7 @@ public class EngineWorkerTest { private final ApplicationThreadFactory atFactory = new ApplicationThreadFactory("apex-engine-service", 512); private static String simpleModelString; - private static String mfpModelString; + private static String differentModelString; private static AxPolicyModel simpleModel; /** @@ -75,10 +75,10 @@ public class EngineWorkerTest { */ @BeforeClass public static void readSimpleModel() throws IOException, ApexModelException { - simpleModelString = TextFileUtils - .getTextFileAsString("src/test/resources/policymodels/SamplePolicyModelJAVASCRIPT.json"); + simpleModelString = TextFileUtils.getTextFileAsString("src/test/resources/policymodels/SmallModel.json"); - mfpModelString = TextFileUtils.getTextFileAsString("src/test/resources/policymodels/MyFirstPolicyModel.json"); + differentModelString = TextFileUtils + .getTextFileAsString("src/test/resources/policymodels/SmallModelDifferent.json"); final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class); simpleModel = modelReader.read(new ByteArrayInputStream(simpleModelString.getBytes())); @@ -161,9 +161,9 @@ public class EngineWorkerTest { } catch (Exception apEx) { assertEquals("addEventListener()<-Worker:0.0.1,STOPPED, listenerName is null", apEx.getMessage()); } - + worker.registerActionListener("DummyListener", null); - + try { worker.registerActionListener(null, new DummyApexEventListener()); fail("test should throw an exception"); @@ -172,7 +172,7 @@ public class EngineWorkerTest { } worker.registerActionListener("DummyListener", new DummyApexEventListener()); - + try { worker.deregisterActionListener(null); fail("test should throw an exception"); @@ -344,17 +344,16 @@ public class EngineWorkerTest { eventQueue.add(new ApexEvent("SomeEvent", "0.0.1", "the.event.namespace", "EventSource", "EventTarget")); try { - worker.updateModel(worker.getKey(), mfpModelString, false); + worker.updateModel(worker.getKey(), differentModelString, false); fail("test should throw an exception"); } catch (ApexException apEx) { - assertEquals("apex model update failed, supplied model with key \"MyFirstPolicyModel:0.0.1\" is not a " + assertEquals("apex model update failed, supplied model with key \"SmallModelDifferent:0.0.1\" is not a " + "compatible model update " - + "from the existing engine model with key \"SamplePolicyModelJAVASCRIPT:0.0.1\"", - apEx.getMessage()); + + "from the existing engine model with key \"SmallModel:0.0.1\"", apEx.getMessage()); } try { - worker.updateModel(worker.getKey(), mfpModelString, true); + worker.updateModel(worker.getKey(), differentModelString, true); } catch (ApexException apEx) { fail("test should not throw an exception"); } |