diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-09-27 14:29:21 +0100 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-09-27 14:29:31 +0100 |
commit | 2b21188b82e21eb87c4e79a6f31beff9325ab2ea (patch) | |
tree | 4bdb78a8709ddf1956ec1b862deabbe478b1a90d /tools/model-generator/src/test/java | |
parent | 3db2feb37ac427a09790fef1ba637c16c3187ed6 (diff) |
Add unit test for Apex command line tools
THere was no unit test for the command line tools. This
review adds unit test and also fixes a few small bugs that
showed up when uit test was run.
Issue-ID: POLICY-1034
Change-Id: Ic19aacdb168fb5a6faa0cd83ed22ccfcedaa51f5
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'tools/model-generator/src/test/java')
4 files changed, 622 insertions, 0 deletions
diff --git a/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/KeyInfoGetterTest.java b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/KeyInfoGetterTest.java new file mode 100644 index 000000000..e6ecefd12 --- /dev/null +++ b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/KeyInfoGetterTest.java @@ -0,0 +1,72 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.tools.model.generator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.concepts.AxState; +import org.onap.policy.apex.testsuites.integration.common.model.SampleDomainModelFactory; + +/** + * Test the Key Info Getter. + */ +public class KeyInfoGetterTest { + + @Test + public void testKeyInfoGetter() { + AxPolicyModel sampleModel = new SampleDomainModelFactory().getSamplePolicyModel("JAVASCRIPT"); + + KeyInfoGetter kiGetter = new KeyInfoGetter(sampleModel); + + assertNull(kiGetter.getName(null)); + assertEquals("SamplePolicyModelJAVASCRIPT", kiGetter.getName(sampleModel.getKey())); + + assertNull(kiGetter.getUuid(null)); + assertNull(kiGetter.getUuid(new AxArtifactKey())); + assertEquals(36, kiGetter.getUuid(sampleModel.getKey()).length()); + + assertNull(kiGetter.getDesc(null)); + assertNull(kiGetter.getDesc(new AxArtifactKey())); + assertEquals("Generated description for concept referred to by key " + "\"SamplePolicyModelJAVASCRIPT:0.0.1\"", + kiGetter.getDesc(sampleModel.getKey())); + + assertNull(kiGetter.getVersion(null)); + assertEquals("0.0.1", kiGetter.getVersion(sampleModel.getKey())); + + AxState matchState = sampleModel.getPolicies().get("Policy0").getStateMap().get("Match"); + + assertNull(kiGetter.getLName(null)); + assertEquals("Match", kiGetter.getLName(matchState.getKey())); + + assertNull(kiGetter.getPName(null)); + assertEquals("Policy0", kiGetter.getPName(matchState.getKey())); + + assertNull(kiGetter.getPVersion(null)); + assertEquals("0.0.1", kiGetter.getPVersion(matchState.getKey())); + + assertNull(kiGetter.getPlName(null)); + assertEquals("NULL", kiGetter.getPlName(matchState.getKey())); + } +} diff --git a/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/SchemaUtilsTest.java b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/SchemaUtilsTest.java new file mode 100644 index 000000000..164f187cc --- /dev/null +++ b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/SchemaUtilsTest.java @@ -0,0 +1,192 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.tools.model.generator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.avro.Schema; +import org.apache.avro.Schema.Field; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxModel; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.apex.plugins.context.schema.avro.AvroSchemaHelper; +import org.onap.policy.apex.plugins.context.schema.avro.AvroSchemaHelperParameters; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.common.parameters.ParameterService; + +/** + * Test the Key Info Getter. + */ +public class SchemaUtilsTest { + private static AxPolicyModel avroModel; + + /** + * Read the models into strings. + * + * @throws IOException on model reading errors + * @throws ApexModelException on model reading exceptions + */ + @BeforeClass + public static void readSimpleModel() throws IOException, ApexModelException { + String avroModelString = TextFileUtils.getTextFileAsString("src/test/resources/models/AvroModel.json"); + + final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class); + avroModel = modelReader.read(new ByteArrayInputStream(avroModelString.getBytes())); + } + + @Test + public void testSchemaUtilsErrors() throws ApexEventException { + AxEvent event = avroModel.getEvents().get("CustomerContextEventIn"); + AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl"); + + AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1"); + try { + SchemaUtils.getEventSchema(event); + fail("test should throw an exception"); + } catch (Exception apEx) { + assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" + + " not found in model service", apEx.getMessage()); + } + + try { + Map<String, Schema> preexistingParamSchemas = new LinkedHashMap<>(); + SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas); + fail("test should throw an exception"); + } catch (Exception apEx) { + assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" + + " not found in model service", apEx.getMessage()); + } + + List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields(); + assertEquals(5, skeletonFields.size()); + + try { + AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory() + .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey()); + + Map<String, Schema> schemaMap = new LinkedHashMap<>(); + SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap); + fail("test should throw an exception"); + } catch (Exception apEx) { + assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" + + " not found in model service", apEx.getMessage()); + } + } + + @Test + public void testSchemaUtils() throws ApexEventException { + ParameterService.clear(); + final SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + ParameterService.register(schemaParameters); + + ModelService.registerModel(AxModel.class, avroModel); + ModelService.registerModel(AxContextSchemas.class, avroModel.getSchemas()); + + AxEvent event = avroModel.getEvents().get("CustomerContextEventIn"); + AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl"); + AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1"); + + Schema eventSchema = SchemaUtils.getEventSchema(event); + assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"", + eventSchema.toString().substring(0, 48)); + + Map<String, Schema> preexistingParamSchemas = new LinkedHashMap<>(); + Schema epSchema = SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), + preexistingParamSchemas); + assertEquals("\"string\"", epSchema.toString()); + + List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields(); + assertEquals(5, skeletonFields.size()); + + try { + AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory() + .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey()); + + Map<String, Schema> schemaMap = new LinkedHashMap<>(); + SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap); + fail("test should throw an exception"); + } catch (Exception apEx) { + assertEquals("context schema helper parameters not found for context schema \"Avro\"", apEx.getMessage()); + } + + schemaParameters.getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters()); + + AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory().createSchemaHelper(topoNodesKey, + avroCtxtSchema.getKey()); + + Map<String, Schema> schemaMap = new LinkedHashMap<>(); + try { + SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap); + } catch (Exception exc) { + fail("test should not throw an exception"); + } + + eventSchema = SchemaUtils.getEventSchema(event); + assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"", + eventSchema.toString().substring(0, 48)); + + epSchema = SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas); + assertEquals("\"string\"", epSchema.toString()); + + AxInputField inField = new AxInputField(new AxReferenceKey("FieldParent", "0.0.1", "Field"), + avroCtxtSchema.getKey(), false); + + Schema ep2Schema = SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas); + assertEquals("{\"type\":\"record\",\"name\":\"TopologyNodes\"", ep2Schema.toString().substring(0, 39)); + + skeletonFields = SchemaUtils.getSkeletonEventSchemaFields(); + assertEquals(5, skeletonFields.size()); + + schemaParameters.getSchemaHelperParameterMap().put("Avro", new JavaSchemaHelperParameters()); + try { + ep2Schema = SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas); + fail("test should throw an exception"); + } catch (Exception apEx) { + assertEquals("FieldParent:0.0.1:NULL:Field: class/type", apEx.getMessage().substring(0, 40)); + } + + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + ModelService.clear(); + } +} diff --git a/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliTest.java b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliTest.java new file mode 100644 index 000000000..170004e7c --- /dev/null +++ b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliTest.java @@ -0,0 +1,176 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.tools.model.generator.model2cli; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; + +import org.junit.Test; + +/** + * Test the Model2Cli utility. + */ +public class Model2CliTest { + @Test + public void testModel2Cli() { + try { + final String[] cliArgs = + { "-h" }; + + Model2CliMain.main(cliArgs); + } catch (Exception exc) { + fail("test should not throw an exception"); + } + } + + @Test + public void testModel2CliNoOptions() { + final String[] cliArgs = new String[] + {}; + + final String outputString = runModel2Cli(cliArgs); + + assertTrue(outputString + .contains("gen-model2cli: no '-m' model file given, cannot proceed (try -h for help)\n")); + } + + @Test + public void testModel2CliBadOptions() { + final String[] cliArgs = + { "-zabbu" }; + + final String outputString = runModel2Cli(cliArgs); + + assertTrue(outputString.contains("usage: gen-model2cli")); + } + + @Test + public void testModel2CliHelp() { + final String[] cliArgs = + { "-h" }; + + final String outputString = runModel2Cli(cliArgs); + + assertTrue(outputString.contains("usage: gen-model2cli")); + } + + @Test + public void testModel2CliVersion() { + final String[] cliArgs = + { "-v" }; + + final String outputString = runModel2Cli(cliArgs); + + assertTrue(outputString.contains("gen-model2cli")); + } + + @Test + public void testModel2CliOverwrite() throws IOException { + File tempFile = File.createTempFile("AvroModel", ".apex"); + tempFile.deleteOnExit(); + + final String[] cliArgs = + { "-m", "src/test/resources/models/AvroModel.json", "-o", tempFile.getCanonicalPath() }; + + final String outputString = runModel2Cli(cliArgs); + + assertTrue(outputString.contains("gen-model2cli: error with '-o' option: \"file already exists\"")); + } + + @Test + public void testModel2CliAvro() throws IOException { + testModel2CliModel("AvroModel"); + } + + @Test + public void testModel2CliAadm() throws IOException { + testModel2CliModel("AADMPolicyModel"); + } + + @Test + public void testModel2CliAnomaly() { + testModel2CliModel("AnomalyDetectionPolicyModel"); + } + + @Test + public void testModel2CliAutoLearn() { + testModel2CliModel("AutoLearnPolicyModel"); + } + + @Test + public void testModel2CliJms() { + testModel2CliModel("JMSSamplePolicyModel"); + } + + @Test + public void testModel2CliMfp() { + testModel2CliModel("MyFirstPolicyModel"); + } + + @Test + public void testModel2CliSample() { + testModel2CliModel("SamplePolicyModelJAVASCRIPT"); + } + + /** + * Run the application. + * + * @param cliArgs the command arguments + * @return a string containing the command output + */ + private String runModel2Cli(final String[] cliArgs) { + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + final ByteArrayOutputStream baosErr = new ByteArrayOutputStream(); + + new Model2CliMain(cliArgs, new PrintStream(baosOut, true), new PrintStream(baosErr, true)); + + String outString = baosOut.toString(); + String errString = baosErr.toString(); + + return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString; + } + + /** + * Test CLI generation. + * + * @param modelName the name of the model file + */ + private void testModel2CliModel(String modelName) { + try { + File tempFile = File.createTempFile(modelName, ".apex"); + tempFile.deleteOnExit(); + + final String[] cliArgs = + { "-m", "src/test/resources/models/" + modelName + ".json", "-o", tempFile.getCanonicalPath(), "-ow" }; + runModel2Cli(cliArgs); + + assertTrue(tempFile.isFile()); + assertTrue(tempFile.length() > 0); + } catch (Exception e) { + fail("test should not throw an exception"); + } + } +} diff --git a/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventTest.java b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventTest.java new file mode 100644 index 000000000..1804aca30 --- /dev/null +++ b/tools/model-generator/src/test/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventTest.java @@ -0,0 +1,182 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.tools.model.generator.model2event; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; + +/** + * Test the Model2Event utility. + */ +public class Model2EventTest { + @Test + public void testModel2Event() { + try { + final String[] EventArgs = + { "-h" }; + + Model2EventMain.main(EventArgs); + } catch (Exception exc) { + fail("test should not throw an exception"); + } + } + + @Test + public void testModel2EventNoOptions() { + final String[] EventArgs = new String[] + {}; + + final String outputString = runModel2Event(EventArgs); + + assertTrue(outputString.contains("gen-model2event: no model file given, cannot proceed (try -h for help)")); + } + + @Test + public void testModel2EventBadOptions() { + final String[] EventArgs = + { "-zabbu" }; + + final String outputString = runModel2Event(EventArgs); + + assertTrue(outputString.contains("usage: gen-model2event")); + } + + @Test + public void testModel2EventHelp() { + final String[] EventArgs = + { "-h" }; + + final String outputString = runModel2Event(EventArgs); + + assertTrue(outputString.contains("usage: gen-model2event")); + } + + @Test + public void testModel2EventVersion() { + final String[] EventArgs = + { "-v" }; + + final String outputString = runModel2Event(EventArgs); + + assertTrue(outputString.contains("gen-model2event")); + } + + @Test + public void testModel2EventNoType() { + final String[] EventArgs = + { "-m", "src/test/resources/models/AvroModel.json" }; + + final String outputString = runModel2Event(EventArgs); + + assertTrue(outputString.contains("gen-model2event: no event type given, cannot proceed (try -h for help)")); + } + + @Test + public void testModel2EventBadType() { + final String[] EventArgs = + { "-m", "src/test/resources/models/AvroModel.json", "-t", "Zooby" }; + + final String outputString = runModel2Event(EventArgs); + + assertTrue(outputString.contains("gen-model2event: unknown type <Zooby>, cannot proceed (try -h for help)")); + } + + @Test + public void testModel2EventAadm() throws IOException { + testModel2EventModel("AADMPolicyModel"); + } + + @Test + public void testModel2EventAnomaly() { + testModel2EventModel("AnomalyDetectionPolicyModel"); + } + + @Test + public void testModel2EventAutoLearn() { + testModel2EventModel("AutoLearnPolicyModel"); + } + + @Test + public void testModel2EventMfp() { + testModel2EventModel("MyFirstPolicyModel"); + } + + @Test + public void testModel2EventSample() { + testModel2EventModel("SamplePolicyModelJAVASCRIPT"); + } + + /** + * Run the application. + * + * @param eventArgs the command arguments + * @return a string containing the command output + */ + private String runModel2Event(final String[] eventArgs) { + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + final ByteArrayOutputStream baosErr = new ByteArrayOutputStream(); + + new Model2EventMain(eventArgs, new PrintStream(baosOut, true)); + + String outString = baosOut.toString(); + String errString = baosErr.toString(); + + return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString; + } + + /** + * Test Event generation. + * + * @param modelName the name of the model file + */ + private void testModel2EventModel(String modelName) { + try { + File tempFile = File.createTempFile(modelName, ".apex"); + tempFile.deleteOnExit(); + + final String[] eventArgs0 = + { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" }; + final String outputString0 = runModel2Event(eventArgs0); + + assertTrue(outputString0.contains("type: stimuli")); + + final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" }; + final String outputString1 = runModel2Event(eventArgs1); + + assertTrue(outputString1.contains("type: response")); + + final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" }; + final String outputString2 = runModel2Event(eventArgs2); + + assertTrue(outputString2.contains("type: internal")); + } catch (Exception e) { + throw new ApexRuntimeException("test should not throw an exception", e); + } + } +} |