b63b1067f53bde8e648ba814e120760935a67bec
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / bolt / enginebolt / EngineWrapperTest.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\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  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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 static org.hamcrest.MatcherAssert.assertThat;\r
21 import static org.hamcrest.Matchers.equalTo;\r
22 \r
23 import javax.ws.rs.core.Response;\r
24 import org.apache.http.StatusLine;\r
25 import org.easymock.EasyMock;\r
26 import org.junit.Before;\r
27 import org.junit.Rule;\r
28 import org.junit.Test;\r
29 import org.junit.rules.ExpectedException;\r
30 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
31 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
32 import org.onap.holmes.common.exception.CorrelationException;\r
33 import org.powermock.api.easymock.PowerMock;\r
34 import org.powermock.reflect.Whitebox;\r
35 \r
36 public class EngineWrapperTest {\r
37 \r
38     @Rule\r
39     public ExpectedException thrown = ExpectedException.none();\r
40     private EngineWrapper engineWrapper = new EngineWrapper();\r
41     private EngineService engineServiceMock;\r
42     private Response response;\r
43     private StatusLine statusLineMock;\r
44 \r
45     @Before\r
46     public void setUp() throws Exception {\r
47         engineServiceMock = PowerMock.createMock(EngineService.class);\r
48         response = PowerMock.createMock(Response.class);\r
49         statusLineMock = PowerMock.createMock(StatusLine.class);\r
50         Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock);\r
51     }\r
52 \r
53     @Test\r
54     public void deployEngine_invoke_rule_deploy_exception() throws Exception {\r
55         thrown.expect(CorrelationException.class);\r
56         thrown.expectMessage("Failed to call the rule deployment RESTful API.");\r
57 \r
58         EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))).andThrow(\r
59                 new RuntimeException(""));\r
60         PowerMock.replayAll();\r
61 \r
62         engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
63 \r
64         PowerMock.verifyAll();\r
65     }\r
66 \r
67     @Test\r
68     public void deployEngine_http_status_not_ok() throws Exception {\r
69         thrown.expect(CorrelationException.class);\r
70         thrown.expectMessage("Failed to deploy the rule!");\r
71 \r
72         EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
73                 .andReturn(response);\r
74         EasyMock.expect(response.getStatus()).andReturn(400);\r
75         PowerMock.replayAll();\r
76 \r
77         engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
78 \r
79         PowerMock.verifyAll();\r
80     }\r
81 \r
82     @Test\r
83     public void deployEngine_parse_content_exception() throws Exception {\r
84         String content = "";\r
85 \r
86         thrown.expect(CorrelationException.class);\r
87         thrown.expectMessage("Failed to parse the value returned by the engine management service.");\r
88         EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
89                 .andReturn(response);\r
90         EasyMock.expect(response.getStatus()).andReturn(200);\r
91         EasyMock.expect(response.readEntity(String.class)).andReturn(content);\r
92         PowerMock.replayAll();\r
93 \r
94         engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
95 \r
96         PowerMock.verifyAll();\r
97     }\r
98 \r
99     @Test\r
100     public void deployEngine_success() throws Exception {\r
101         String content = "{\"package\":\"test\"}";\r
102         EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
103                 .andReturn(response);\r
104         EasyMock.expect(response.getStatus()).andReturn(200);\r
105         EasyMock.expect(response.readEntity(String.class)).andReturn(content);\r
106         PowerMock.replayAll();\r
107 \r
108         String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
109 \r
110         assertThat(result, equalTo("test"));\r
111 \r
112     }\r
113 \r
114     @Test\r
115     public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception {\r
116         thrown.expect(CorrelationException.class);\r
117         thrown.expectMessage("Failed to call the rule deleting RESTful API.");\r
118 \r
119         EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow(\r
120                 new RuntimeException(""));\r
121         PowerMock.replayAll();\r
122 \r
123         engineWrapper.deleteRuleFromEngine("");\r
124 \r
125         PowerMock.verifyAll();\r
126     }\r
127 \r
128     @Test\r
129     public void deleteRuleFromEngine_http_status_not_ok() throws Exception {\r
130         thrown.expect(CorrelationException.class);\r
131         thrown.expectMessage("Failed to delete the rule!");\r
132 \r
133         EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))\r
134                 .andReturn(response);\r
135         EasyMock.expect(response.getStatus()).andReturn(400);\r
136 \r
137         PowerMock.replayAll();\r
138 \r
139         engineWrapper.deleteRuleFromEngine("");\r
140 \r
141         PowerMock.verifyAll();\r
142     }\r
143 \r
144     @Test\r
145     public void deleteRuleFromEngine_success() throws Exception {\r
146         EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))\r
147                 .andReturn(response);\r
148         EasyMock.expect(response.getStatus()).andReturn(200);\r
149 \r
150         PowerMock.replayAll();\r
151 \r
152         boolean result = engineWrapper.deleteRuleFromEngine("");\r
153 \r
154         assertThat(result, equalTo(true));\r
155     }\r
156 \r
157     @Test\r
158     public void checkRuleFromEngine_rule_delete_exception() throws Exception {\r
159         thrown.expect(CorrelationException.class);\r
160         thrown.expectMessage("Failed to call the rule verification RESTful API.");\r
161 \r
162         EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow(\r
163                 new RuntimeException(""));\r
164         PowerMock.replayAll();\r
165 \r
166         engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());\r
167 \r
168         PowerMock.verifyAll();\r
169     }\r
170 \r
171     @Test\r
172     public void checkRuleFromEngine_success() throws Exception {\r
173         EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))\r
174                 .andReturn(response);\r
175         EasyMock.expect(response.getStatus()).andReturn(200);\r
176 \r
177         PowerMock.replayAll();\r
178 \r
179         boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());\r
180 \r
181         assertThat(result, equalTo(true));\r
182     }\r
183 }