Removed Dependency: httpclient
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / bolt / enginebolt / EngineWrapperTest.java
1 /**\r
2  * Copyright 2017 - 2021 ZTE Corporation.\r
3  * <p>\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  * <p>\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  * <p>\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.onap.holmes.rulemgt.bolt.enginebolt;\r
18 \r
19 \r
20 import org.junit.After;\r
21 import org.junit.Before;\r
22 import org.junit.Rule;\r
23 import org.junit.Test;\r
24 import org.junit.rules.ExpectedException;\r
25 import org.junit.runner.RunWith;\r
26 import org.onap.holmes.common.exception.CorrelationException;\r
27 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
28 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
29 import org.powermock.modules.junit4.PowerMockRunner;\r
30 import org.powermock.reflect.Whitebox;\r
31 \r
32 import static org.easymock.EasyMock.*;\r
33 import static org.hamcrest.MatcherAssert.assertThat;\r
34 import static org.hamcrest.Matchers.equalTo;\r
35 import static org.hamcrest.Matchers.is;\r
36 import static org.powermock.api.easymock.PowerMock.*;\r
37 \r
38 @RunWith(PowerMockRunner.class)\r
39 public class EngineWrapperTest {\r
40 \r
41     @Rule\r
42     public ExpectedException thrown = ExpectedException.none();\r
43 \r
44     private EngineWrapper engineWrapper = new EngineWrapper();\r
45 \r
46     private EngineService mockedEngineService;\r
47 \r
48     @Before\r
49     public void before() {\r
50         mockedEngineService = createMock(EngineService.class);\r
51         Whitebox.setInternalState(engineWrapper, "engineService", mockedEngineService);\r
52     }\r
53 \r
54     @After\r
55     public void after() {\r
56         resetAll();\r
57     }\r
58 \r
59     @Test\r
60     public void deployEngine_fail() throws Exception {\r
61         thrown.expect(CorrelationException.class);\r
62         thrown.expectMessage("Failed to deploy the rule!");\r
63 \r
64         expect(mockedEngineService.deploy(anyObject(CorrelationDeployRule4Engine.class),\r
65                 anyObject(String.class))).andReturn(null);\r
66 \r
67         replayAll();\r
68 \r
69         engineWrapper.deployEngine(new CorrelationDeployRule4Engine(), "127.0.0.1");\r
70 \r
71         verifyAll();\r
72     }\r
73 \r
74     @Test\r
75     public void deployEngine_parse_content_exception() throws Exception {\r
76         thrown.expect(CorrelationException.class);\r
77         thrown.expectMessage(\r
78                 "Failed to parse the value returned by the engine management service.");\r
79         expect(mockedEngineService.deploy(anyObject(CorrelationDeployRule4Engine.class),\r
80                 anyObject(String.class))).andReturn("");\r
81 \r
82         replayAll();\r
83 \r
84         engineWrapper.deployEngine(new CorrelationDeployRule4Engine(), "127.0.0.1");\r
85 \r
86         verifyAll();\r
87     }\r
88 \r
89     @Test\r
90     public void deployEngine_success() throws Exception {\r
91         String content = "{\"packageName\":\"test\"}";\r
92         expect(mockedEngineService.deploy(anyObject(CorrelationDeployRule4Engine.class),\r
93                 anyObject(String.class))).andReturn(content);\r
94 \r
95         replayAll();\r
96 \r
97         String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine(), "127.0.0.1");\r
98 \r
99         assertThat(result, equalTo("test"));\r
100 \r
101     }\r
102 \r
103     @Test\r
104     public void deleteRuleFromEngine_fail() throws Exception {\r
105         thrown.expect(CorrelationException.class);\r
106         thrown.expectMessage("Failed to delete the rule!");\r
107 \r
108         expect(mockedEngineService.delete(anyObject(String.class),\r
109                 anyObject(String.class)))\r
110                 .andReturn(false);\r
111 \r
112         replayAll();\r
113 \r
114         engineWrapper.deleteRuleFromEngine("", "127.0.0.1");\r
115 \r
116         verifyAll();\r
117     }\r
118 \r
119     @Test\r
120     public void deleteRuleFromEngine_success() throws Exception {\r
121         expect(mockedEngineService.delete(anyObject(String.class),\r
122                 anyObject(String.class)))\r
123                 .andReturn(true);\r
124 \r
125         replayAll();\r
126 \r
127         boolean result = engineWrapper.deleteRuleFromEngine("", "127.0.0.1");\r
128 \r
129         assertThat(result, equalTo(true));\r
130     }\r
131 \r
132     @Test\r
133     public void checkRuleFromEngine_fail() throws Exception {\r
134         thrown.expect(CorrelationException.class);\r
135         thrown.expectMessage("Failed to verify the rule. The contents of the rule are invalid.");\r
136 \r
137         expect(\r
138                 mockedEngineService.check(anyObject(CorrelationCheckRule4Engine.class),\r
139                         anyObject(String.class))).andReturn(false);\r
140         replayAll();\r
141 \r
142         engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(), "127.0.0.1");\r
143 \r
144         verifyAll();\r
145     }\r
146 \r
147     @Test\r
148     public void checkRuleFromEngine_success() throws Exception {\r
149         expect(mockedEngineService.check(anyObject(CorrelationCheckRule4Engine.class),anyString())).andReturn(true);\r
150 \r
151         replayAll();\r
152 \r
153         boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(), "127.0.0.1");\r
154 \r
155         assertThat(result, is(true));\r
156     }\r
157 }