Fix more sonar issues in models: yaml to dao
[policy/models.git] / models-interactions / model-yaml / src / test / java / org / onap / policy / controlloop / compiler / ControlLoopCompilerTest.java
index 1028bde..6603dcb 100644 (file)
@@ -2,15 +2,15 @@
  * ============LICENSE_START=======================================================
  * policy-yaml unit test
  * ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2019 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.
@@ -27,12 +27,9 @@ import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
-
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -41,10 +38,13 @@ import org.onap.policy.controlloop.policy.FinalResult;
 
 public class ControlLoopCompilerTest {
 
+    private static final String POLICY_RECIPE_IS_INVALID = "Policy recipe is invalid";
+    private static final String RESTART_UNKNOWN_POLICY =
+                    "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy";
     @Rule
     public ExpectedException expectedException = ExpectedException.none();
 
-    @Test 
+    @Test
     public void testTest() throws Exception {
         List<String> expectedOnErrorMessages = new ArrayList<>();
         expectedOnErrorMessages.add("Operational Policy has an bad ID");
@@ -53,9 +53,9 @@ public class ControlLoopCompilerTest {
         expectedOnErrorMessages.add("Policy actor is null");
         expectedOnErrorMessages.add("Policy actor is invalid");
         expectedOnErrorMessages.add("Policy recipe is null");
-        expectedOnErrorMessages.add("Policy recipe is invalid");
-        expectedOnErrorMessages.add("Policy recipe is invalid");
-        expectedOnErrorMessages.add("Policy recipe is invalid");
+        expectedOnErrorMessages.add(POLICY_RECIPE_IS_INVALID);
+        expectedOnErrorMessages.add(POLICY_RECIPE_IS_INVALID);
+        expectedOnErrorMessages.add(POLICY_RECIPE_IS_INVALID);
         expectedOnErrorMessages.add("Policy target is null");
         expectedOnErrorMessages.add("Policy target is invalid");
         expectedOnErrorMessages.add("Policy success is neither another policy nor FINAL_SUCCESS");
@@ -67,9 +67,9 @@ public class ControlLoopCompilerTest {
         expectedOnErrorMessages.add("Unsupported version for this compiler");
         expectedOnErrorMessages.add("controlLoop overall timeout is less than the sum of operational policy timeouts.");
 
-        TestControlLoopCompilerCallback testControlLoopCompilerCallback = 
+        TestControlLoopCompilerCallback testControlLoopCompilerCallback =
                         new TestControlLoopCompilerCallback(expectedOnErrorMessages);
-        ControlLoopPolicy controlLoopPolicy = this.test("src/test/resources/v1.0.0/test.yaml", 
+        ControlLoopPolicy controlLoopPolicy = this.test("src/test/resources/v1.0.0/test.yaml",
                         testControlLoopCompilerCallback);
         assertEquals(22, controlLoopPolicy.getPolicies().size());
         assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
@@ -79,7 +79,7 @@ public class ControlLoopCompilerTest {
     public void testSuccessConnectedToUnknownPolicy() throws Exception {
         expectedException.expect(CompilerException.class);
         expectedException.expectMessage(
-                        "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
+                        RESTART_UNKNOWN_POLICY);
         this.test("src/test/resources/v1.0.0/bad_policy_success_connected_to_unknown_policy.yaml");
     }
 
@@ -87,7 +87,7 @@ public class ControlLoopCompilerTest {
     public void testFailureConnectedToUnknownPolicy() throws Exception {
         expectedException.expect(CompilerException.class);
         expectedException.expectMessage(
-                        "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
+                        RESTART_UNKNOWN_POLICY);
         this.test("src/test/resources/v1.0.0/bad_policy_failure_connected_to_unknown_policy.yaml");
     }
 
@@ -95,7 +95,7 @@ public class ControlLoopCompilerTest {
     public void testFailureTimeoutToUnknownPolicy() throws Exception {
         expectedException.expect(CompilerException.class);
         expectedException.expectMessage(
-                        "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
+                        RESTART_UNKNOWN_POLICY);
         this.test("src/test/resources/v1.0.0/bad_policy_failure_timeout_connected_to_unknown_policy.yaml");
     }
 
@@ -103,7 +103,7 @@ public class ControlLoopCompilerTest {
     public void testFailureRetriesToUnknownPolicy() throws Exception {
         expectedException.expect(CompilerException.class);
         expectedException.expectMessage(
-                        "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
+                        RESTART_UNKNOWN_POLICY);
         this.test("src/test/resources/v1.0.0/bad_policy_failure_retries_connected_to_unknown_policy.yaml");
     }
 
@@ -111,7 +111,7 @@ public class ControlLoopCompilerTest {
     public void testFailureExceptionToUnknownPolicy() throws Exception {
         expectedException.expect(CompilerException.class);
         expectedException.expectMessage(
-                        "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
+                        RESTART_UNKNOWN_POLICY);
         this.test("src/test/resources/v1.0.0/bad_policy_failure_exception_connected_to_unknown_policy.yaml");
     }
 
@@ -119,38 +119,38 @@ public class ControlLoopCompilerTest {
     public void testFailureGuardToUnknownPolicy() throws Exception {
         expectedException.expect(CompilerException.class);
         expectedException.expectMessage(
-                        "Operation Policy unique-policy-id-1-restart is connected to unknown policy unknown-policy");
+                        RESTART_UNKNOWN_POLICY);
         this.test("src/test/resources/v1.0.0/bad_policy_failure_guard_connected_to_unknown_policy.yaml");
     }
 
-    @Test 
+    @Test
     public void testInvalidTriggerPolicyId() throws Exception {
         expectedException.expect(CompilerException.class);
         expectedException.expectMessage(
-                        "Unexpected value for trigger_policy, should only be " 
+                        "Unexpected value for trigger_policy, should only be "
                         + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
         this.test("src/test/resources/v1.0.0/bad_trigger_1.yaml");
     }
 
-    @Test 
+    @Test
     public void testNoTriggerPolicyId() throws Exception {
         expectedException.expect(CompilerException.class);
         this.test("src/test/resources/v1.0.0/bad_trigger_no_trigger_id.yaml");
     }
 
-    @Test 
+    @Test
     public void testNoControlLoopName() throws Exception {
         List<String> expectedOnErrorMessages = new ArrayList<>();
         expectedOnErrorMessages.add("Missing controlLoopName");
         expectedOnErrorMessages.add("Unsupported version for this compiler");
-        TestControlLoopCompilerCallback testControlLoopCompilerCallback = 
+        TestControlLoopCompilerCallback testControlLoopCompilerCallback =
                         new TestControlLoopCompilerCallback(expectedOnErrorMessages);
-        this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml", 
+        this.test("src/test/resources/v1.0.0/bad_control_loop_no_control_loop_name.yaml",
                         testControlLoopCompilerCallback);
         assertTrue(testControlLoopCompilerCallback.areAllExpectedOnErrorsReceived());
     }
 
-    @Test 
+    @Test
     public void testInvalidFinalResult() throws Exception {
         expectedException.expect(CompilerException.class);
         expectedException.expectMessage(
@@ -158,7 +158,7 @@ public class ControlLoopCompilerTest {
         this.test("src/test/resources/v1.0.0/bad_trigger_2.yaml");
     }
 
-    @Test 
+    @Test
     public void testCompileEmptyFile() throws Exception {
         expectedException.expect(CompilerException.class);
         expectedException.expectMessage("Could not parse yaml specification.");
@@ -171,24 +171,17 @@ public class ControlLoopCompilerTest {
 
     /**
      * Does the actual test.
-     * 
+     *
      * @param testFile test file
      * @param controlLoopCompilerCallback callback method
      * @return the policy object
      * @throws Exception exception
      */
-    public ControlLoopPolicy test(String testFile, 
+    public ControlLoopPolicy test(String testFile,
                     ControlLoopCompilerCallback controlLoopCompilerCallback) throws Exception {
         try (InputStream is = new FileInputStream(new File(testFile))) {
             return ControlLoopCompiler.compile(is, controlLoopCompilerCallback);
-        } catch (FileNotFoundException e) {
-            fail(e.getMessage());
-        } catch (IOException e) {
-            fail(e.getMessage());
-        } catch (Exception e) {
-            throw e;
         }
-        return null;
     }
 
     class TestControlLoopCompilerCallback implements ControlLoopCompilerCallback {
@@ -213,7 +206,7 @@ public class ControlLoopCompilerTest {
         }
 
         public boolean areAllExpectedOnErrorsReceived() {
-            return expectedOnErrorMessages.size() == 0;
+            return expectedOnErrorMessages.isEmpty();
         }
 
     }