2 * Copyright 2017 ZTE Corporation.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.onap.holmes.rulemgt.bolt.enginebolt;
\r
20 import org.apache.http.HttpResponse;
\r
21 import org.apache.http.StatusLine;
\r
22 import org.easymock.EasyMock;
\r
23 import org.junit.Before;
\r
24 import org.junit.Rule;
\r
25 import org.junit.Test;
\r
26 import org.junit.rules.ExpectedException;
\r
27 import org.junit.runner.RunWith;
\r
28 import org.onap.holmes.common.exception.CorrelationException;
\r
29 import org.onap.holmes.common.utils.HttpsUtils;
\r
30 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
\r
31 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
\r
32 import org.powermock.api.easymock.PowerMock;
\r
33 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
34 import org.powermock.modules.junit4.PowerMockRunner;
\r
35 import org.powermock.reflect.Whitebox;
\r
37 import static org.hamcrest.MatcherAssert.assertThat;
\r
38 import static org.hamcrest.Matchers.equalTo;
\r
40 @PrepareForTest({EngineWrapper.class, EngineService.class, HttpsUtils.class, HttpResponse.class,
\r
42 @RunWith(PowerMockRunner.class)
\r
43 public class EngineWrapperTest {
\r
46 public ExpectedException thrown = ExpectedException.none();
\r
47 private EngineWrapper engineWrapper = new EngineWrapper();
\r
48 private EngineService engineServiceMock;
\r
49 private HttpResponse httpResponse;
\r
50 private StatusLine statusLineMock;
\r
53 public void setUp() throws Exception {
\r
54 engineServiceMock = PowerMock.createMock(EngineService.class);
\r
55 httpResponse = PowerMock.createMock(HttpResponse.class);
\r
56 statusLineMock = PowerMock.createMock(StatusLine.class);
\r
57 Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock);
\r
61 public void deployEngine_invoke_rule_deploy_exception() throws Exception {
\r
62 thrown.expect(CorrelationException.class);
\r
63 thrown.expectMessage("Failed to call the rule deployment RESTful API.");
\r
66 engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),
\r
67 EasyMock.anyObject(String.class)))
\r
69 new RuntimeException(""));
\r
70 PowerMock.replayAll();
\r
72 engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"127.0.0.1");
\r
74 PowerMock.verifyAll();
\r
78 public void deployEngine_http_status_not_ok() throws Exception {
\r
79 thrown.expect(CorrelationException.class);
\r
80 thrown.expectMessage("Failed to deploy the rule!");
\r
83 engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),
\r
84 EasyMock.anyObject(String.class)))
\r
85 .andReturn(httpResponse);
\r
86 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
87 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
\r
88 PowerMock.replayAll();
\r
90 engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"127.0.0.1");
\r
92 PowerMock.verifyAll();
\r
96 public void deployEngine_parse_content_exception() throws Exception {
\r
97 PowerMock.resetAll();
\r
98 String content = "";
\r
99 PowerMock.mockStatic(HttpsUtils.class);
\r
100 thrown.expect(CorrelationException.class);
\r
101 thrown.expectMessage(
\r
102 "Failed to parse the value returned by the engine management service.");
\r
104 engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),
\r
105 EasyMock.anyObject(String.class)))
\r
106 .andReturn(httpResponse);
\r
107 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
108 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
109 EasyMock.expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn(content);
\r
110 PowerMock.replayAll();
\r
112 engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"127.0.0.1");
\r
114 PowerMock.verifyAll();
\r
118 public void deployEngine_success() throws Exception {
\r
119 PowerMock.resetAll();
\r
120 String content = "{\"packageName\":\"test\"}";
\r
121 PowerMock.mockStatic(HttpsUtils.class);
\r
123 engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),
\r
124 EasyMock.anyObject(String.class)))
\r
125 .andReturn(httpResponse);
\r
126 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
127 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
128 EasyMock.expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn(content);
\r
129 PowerMock.replayAll();
\r
131 String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"127.0.0.1");
\r
133 assertThat(result, equalTo("test"));
\r
138 public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception {
\r
139 thrown.expect(CorrelationException.class);
\r
140 thrown.expectMessage("Failed to call the rule deleting RESTful API.");
\r
142 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class),
\r
143 EasyMock.anyObject(String.class))).andThrow(
\r
144 new RuntimeException(""));
\r
145 PowerMock.replayAll();
\r
147 engineWrapper.deleteRuleFromEngine("","127.0.0.1");
\r
149 PowerMock.verifyAll();
\r
153 public void deleteRuleFromEngine_http_status_not_ok() throws Exception {
\r
154 thrown.expect(CorrelationException.class);
\r
155 thrown.expectMessage("Failed to delete the rule!");
\r
157 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class),
\r
158 EasyMock.anyObject(String.class)))
\r
159 .andReturn(httpResponse);
\r
160 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
161 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
\r
163 PowerMock.replayAll();
\r
165 engineWrapper.deleteRuleFromEngine("","127.0.0.1");
\r
167 PowerMock.verifyAll();
\r
171 public void deleteRuleFromEngine_success() throws Exception {
\r
172 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class),
\r
173 EasyMock.anyObject(String.class)))
\r
174 .andReturn(httpResponse);
\r
175 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
176 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
178 PowerMock.replayAll();
\r
180 boolean result = engineWrapper.deleteRuleFromEngine("","127.0.0.1");
\r
182 assertThat(result, equalTo(true));
\r
186 public void checkRuleFromEngine_rule_delete_exception() throws Exception {
\r
187 thrown.expect(CorrelationException.class);
\r
188 thrown.expectMessage("Failed to call the rule verification RESTful API.");
\r
191 engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class),
\r
192 EasyMock.anyObject(String.class)))
\r
194 new RuntimeException(""));
\r
195 PowerMock.replayAll();
\r
197 engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(),"127.0.0.1");
\r
199 PowerMock.verifyAll();
\r
203 public void checkRuleFromEngine_success() throws Exception {
\r
205 engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class),
\r
206 EasyMock.anyObject(String.class)))
\r
207 .andReturn(httpResponse);
\r
208 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
209 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
211 PowerMock.replayAll();
\r
213 boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(),"127.0.0.1");
\r
215 assertThat(result, equalTo(true));
\r