From aac3c1d70c439910e25a2634f872f497486509ec Mon Sep 17 00:00:00 2001 From: waynedunican Date: Mon, 13 Jul 2020 11:13:53 +0100 Subject: Replace try/catch with assertj Replaced try/catch blocks with assertj assertions in apex-pdp. Last batch of changes Issue-ID: POLICY-2451 Change-Id: I39bd02fdbd8389818dcf1b786189c1e344ddcea5 Signed-off-by: waynedunican --- .../tools/model/generator/SchemaUtilsTest.java | 61 +++++++-------------- .../model/generator/model2cli/Model2CliTest.java | 58 +++++++++----------- .../generator/model2event/Model2EventTest.java | 64 ++++++++++------------ .../apex/tools/simple/wsclient/WsClientTest.java | 20 +++---- 4 files changed, 81 insertions(+), 122 deletions(-) (limited to 'tools') 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 index 3d6519a65..6af2959ed 100644 --- 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 @@ -21,8 +21,8 @@ package org.onap.policy.apex.tools.model.generator; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -77,40 +77,30 @@ public class SchemaUtilsTest { @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 { + assertThatThrownBy(() -> SchemaUtils.getEventSchema(event)) + .hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" + + " not found in model service"); + + assertThatThrownBy(() -> { Map 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()); - } + }).hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" + + " not found in model service"); List skeletonFields = SchemaUtils.getSkeletonEventSchemaFields(); assertEquals(5, skeletonFields.size()); - try { + AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl"); + AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1"); + assertThatThrownBy(() -> { AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory() .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey()); Map 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()); - } + }).hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas" + + " not found in model service"); } @Test @@ -124,8 +114,6 @@ public class SchemaUtilsTest { 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\"", @@ -136,19 +124,18 @@ public class SchemaUtilsTest { SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas); assertEquals("\"string\"", epSchema.toString()); + AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl"); + AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1"); List skeletonFields = SchemaUtils.getSkeletonEventSchemaFields(); assertEquals(5, skeletonFields.size()); - try { + assertThatThrownBy(() -> { AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory() .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey()); Map 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()); - } + }).hasMessage("context schema helper parameters not found for context schema \"Avro\""); schemaParameters.getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters()); @@ -156,11 +143,7 @@ public class SchemaUtilsTest { (AvroSchemaHelper) new SchemaHelperFactory().createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey()); Map schemaMap = new LinkedHashMap<>(); - try { - SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap); eventSchema = SchemaUtils.getEventSchema(event); assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"", @@ -179,12 +162,8 @@ public class SchemaUtilsTest { 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)); - } + assertThatThrownBy(() -> SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas)) + .hasMessageContaining("FieldParent:0.0.1:NULL:Field: class/type"); 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 index a109e47da..1263dc4fd 100644 --- 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 @@ -22,7 +22,6 @@ 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; @@ -36,13 +35,9 @@ import org.junit.Test; public class Model2CliTest { @Test public void testModel2Cli() { - try { - final String[] cliArgs = {"-h"}; + final String[] cliArgs = {"-h"}; - Model2CliMain.main(cliArgs); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + Model2CliMain.main(cliArgs); } @Test @@ -104,27 +99,27 @@ public class Model2CliTest { } @Test - public void testModel2CliAnomaly() { + public void testModel2CliAnomaly() throws IOException { testModel2CliModel("target/examples/models/Adaptive", "AnomalyDetectionPolicyModel"); } @Test - public void testModel2CliAutoLearn() { + public void testModel2CliAutoLearn() throws IOException { testModel2CliModel("target/examples/models/Adaptive", "AutoLearnPolicyModel"); } @Test - public void testModel2CliJms() { + public void testModel2CliJms() throws IOException { testModel2CliModel("target/examples/models/JMS", "JMSTestModel"); } @Test - public void testModel2CliMfp() { + public void testModel2CliMfp() throws IOException { testModel2CliModel("target/examples/models/MyFirstPolicy/2", "MyFirstPolicyModel_0.0.1"); } @Test - public void testModel2CliSample() { + public void testModel2CliSample() throws IOException { testModel2CliModel("target/examples/models/SampleDomain", "SamplePolicyModelJAVASCRIPT"); } @@ -151,26 +146,23 @@ public class Model2CliTest { * * @param modelName the name of the model file */ - private void testModel2CliModel(final String modelPath, final String modelName) { - try { - File tempFile = File.createTempFile(modelName, ".apex"); - tempFile.deleteOnExit(); - - // @formatter:off - final String[] cliArgs = { - "-m", - modelPath + "/" + modelName + ".json", - "-o", - tempFile.getCanonicalPath(), - "-ow" - }; - // @formatter:on - runModel2Cli(cliArgs); - - assertTrue(tempFile.isFile()); - assertTrue(tempFile.length() > 0); - } catch (Exception e) { - fail("test should not throw an exception"); - } + private void testModel2CliModel(final String modelPath, final String modelName) throws IOException { + File tempFile = File.createTempFile(modelName, ".apex"); + tempFile.deleteOnExit(); + + // @formatter:off + final String[] cliArgs = { + "-m", + modelPath + "/" + modelName + ".json", + "-o", + tempFile.getCanonicalPath(), + "-ow" + }; + // @formatter:on + runModel2Cli(cliArgs); + + assertTrue(tempFile.isFile()); + assertTrue(tempFile.length() > 0); + } } 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 index f7c913054..58c860d1c 100644 --- 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 @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * 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========================================================= */ @@ -21,14 +22,12 @@ 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. @@ -36,14 +35,11 @@ import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; 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"); - } + final String[] EventArgs = + { "-h" }; + + Model2EventMain.main(EventArgs); + } @Test @@ -112,28 +108,28 @@ public class Model2EventTest { } @Test - public void testModel2EventAnomaly() { + public void testModel2EventAnomaly() throws IOException { testModel2EventModel("AnomalyDetectionPolicyModel"); } @Test - public void testModel2EventAutoLearn() { + public void testModel2EventAutoLearn() throws IOException { testModel2EventModel("AutoLearnPolicyModel"); } @Test - public void testModel2EventMfp() { + public void testModel2EventMfp() throws IOException { testModel2EventModel("MyFirstPolicyModel"); } @Test - public void testModel2EventSample() { + public void testModel2EventSample() throws IOException { testModel2EventModel("SamplePolicyModelJAVASCRIPT"); } /** * Run the application. - * + * * @param eventArgs the command arguments * @return a string containing the command output */ @@ -151,31 +147,27 @@ public class Model2EventTest { /** * 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(); + private void testModel2EventModel(String modelName) throws IOException { + 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); + final String[] eventArgs0 = + { "-m", "src/test/resources/models/" + modelName + ".json", "-t", "stimuli" }; + final String outputString0 = runModel2Event(eventArgs0); - assertTrue(outputString0.contains("type: stimuli")); + assertTrue(outputString0.contains("type: stimuli")); - final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" }; - final String outputString1 = runModel2Event(eventArgs1); + final String[] eventArgs1 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "response" }; + final String outputString1 = runModel2Event(eventArgs1); - assertTrue(outputString1.contains("type: response")); + assertTrue(outputString1.contains("type: response")); - final String[] eventArgs2 = {"-m", "src/test/resources/models/" + modelName + ".json", "-t", "internal" }; - final String outputString2 = runModel2Event(eventArgs2); + 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); - } + assertTrue(outputString2.contains("type: internal")); } } diff --git a/tools/simple-wsclient/src/test/java/org/onap/policy/apex/tools/simple/wsclient/WsClientTest.java b/tools/simple-wsclient/src/test/java/org/onap/policy/apex/tools/simple/wsclient/WsClientTest.java index 9ca7e9296..8ff185504 100644 --- a/tools/simple-wsclient/src/test/java/org/onap/policy/apex/tools/simple/wsclient/WsClientTest.java +++ b/tools/simple-wsclient/src/test/java/org/onap/policy/apex/tools/simple/wsclient/WsClientTest.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ * 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========================================================= */ @@ -21,7 +22,6 @@ package org.onap.policy.apex.tools.simple.wsclient; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -35,14 +35,10 @@ import org.junit.Test; public class WsClientTest { @Test public void testWsClient() { - try { - final String[] EventArgs = - { "-h" }; - - WsClientMain.main(EventArgs); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + final String[] EventArgs = + { "-h" }; + + WsClientMain.main(EventArgs); } @Test -- cgit 1.2.3-korg