Change the groupid from openo to onap
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / bolt / enginebolt / EngineWrapperTest.java
diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java
new file mode 100644 (file)
index 0000000..b63b106
--- /dev/null
@@ -0,0 +1,183 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.holmes.rulemgt.bolt.enginebolt;\r
+\r
+\r
+import static org.hamcrest.MatcherAssert.assertThat;\r
+import static org.hamcrest.Matchers.equalTo;\r
+\r
+import javax.ws.rs.core.Response;\r
+import org.apache.http.StatusLine;\r
+import org.easymock.EasyMock;\r
+import org.junit.Before;\r
+import org.junit.Rule;\r
+import org.junit.Test;\r
+import org.junit.rules.ExpectedException;\r
+import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
+import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
+import org.onap.holmes.common.exception.CorrelationException;\r
+import org.powermock.api.easymock.PowerMock;\r
+import org.powermock.reflect.Whitebox;\r
+\r
+public class EngineWrapperTest {\r
+\r
+    @Rule\r
+    public ExpectedException thrown = ExpectedException.none();\r
+    private EngineWrapper engineWrapper = new EngineWrapper();\r
+    private EngineService engineServiceMock;\r
+    private Response response;\r
+    private StatusLine statusLineMock;\r
+\r
+    @Before\r
+    public void setUp() throws Exception {\r
+        engineServiceMock = PowerMock.createMock(EngineService.class);\r
+        response = PowerMock.createMock(Response.class);\r
+        statusLineMock = PowerMock.createMock(StatusLine.class);\r
+        Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock);\r
+    }\r
+\r
+    @Test\r
+    public void deployEngine_invoke_rule_deploy_exception() throws Exception {\r
+        thrown.expect(CorrelationException.class);\r
+        thrown.expectMessage("Failed to call the rule deployment RESTful API.");\r
+\r
+        EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))).andThrow(\r
+                new RuntimeException(""));\r
+        PowerMock.replayAll();\r
+\r
+        engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void deployEngine_http_status_not_ok() throws Exception {\r
+        thrown.expect(CorrelationException.class);\r
+        thrown.expectMessage("Failed to deploy the rule!");\r
+\r
+        EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                .andReturn(response);\r
+        EasyMock.expect(response.getStatus()).andReturn(400);\r
+        PowerMock.replayAll();\r
+\r
+        engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void deployEngine_parse_content_exception() throws Exception {\r
+        String content = "";\r
+\r
+        thrown.expect(CorrelationException.class);\r
+        thrown.expectMessage("Failed to parse the value returned by the engine management service.");\r
+        EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                .andReturn(response);\r
+        EasyMock.expect(response.getStatus()).andReturn(200);\r
+        EasyMock.expect(response.readEntity(String.class)).andReturn(content);\r
+        PowerMock.replayAll();\r
+\r
+        engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void deployEngine_success() throws Exception {\r
+        String content = "{\"package\":\"test\"}";\r
+        EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                .andReturn(response);\r
+        EasyMock.expect(response.getStatus()).andReturn(200);\r
+        EasyMock.expect(response.readEntity(String.class)).andReturn(content);\r
+        PowerMock.replayAll();\r
+\r
+        String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
+\r
+        assertThat(result, equalTo("test"));\r
+\r
+    }\r
+\r
+    @Test\r
+    public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception {\r
+        thrown.expect(CorrelationException.class);\r
+        thrown.expectMessage("Failed to call the rule deleting RESTful API.");\r
+\r
+        EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow(\r
+                new RuntimeException(""));\r
+        PowerMock.replayAll();\r
+\r
+        engineWrapper.deleteRuleFromEngine("");\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void deleteRuleFromEngine_http_status_not_ok() throws Exception {\r
+        thrown.expect(CorrelationException.class);\r
+        thrown.expectMessage("Failed to delete the rule!");\r
+\r
+        EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))\r
+                .andReturn(response);\r
+        EasyMock.expect(response.getStatus()).andReturn(400);\r
+\r
+        PowerMock.replayAll();\r
+\r
+        engineWrapper.deleteRuleFromEngine("");\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void deleteRuleFromEngine_success() throws Exception {\r
+        EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))\r
+                .andReturn(response);\r
+        EasyMock.expect(response.getStatus()).andReturn(200);\r
+\r
+        PowerMock.replayAll();\r
+\r
+        boolean result = engineWrapper.deleteRuleFromEngine("");\r
+\r
+        assertThat(result, equalTo(true));\r
+    }\r
+\r
+    @Test\r
+    public void checkRuleFromEngine_rule_delete_exception() throws Exception {\r
+        thrown.expect(CorrelationException.class);\r
+        thrown.expectMessage("Failed to call the rule verification RESTful API.");\r
+\r
+        EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow(\r
+                new RuntimeException(""));\r
+        PowerMock.replayAll();\r
+\r
+        engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void checkRuleFromEngine_success() throws Exception {\r
+        EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))\r
+                .andReturn(response);\r
+        EasyMock.expect(response.getStatus()).andReturn(200);\r
+\r
+        PowerMock.replayAll();\r
+\r
+        boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());\r
+\r
+        assertThat(result, equalTo(true));\r
+    }\r
+}
\ No newline at end of file