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 static org.hamcrest.MatcherAssert.assertThat;
\r
21 import static org.hamcrest.Matchers.equalTo;
\r
23 import org.apache.http.HttpResponse;
\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.junit.runner.RunWith;
\r
31 import org.onap.holmes.common.utils.HttpsUtils;
\r
32 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
\r
33 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
\r
34 import org.onap.holmes.common.exception.CorrelationException;
\r
35 import org.powermock.api.easymock.PowerMock;
\r
36 import org.powermock.api.mockito.PowerMockito;
\r
37 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
38 import org.powermock.modules.junit4.PowerMockRunner;
\r
39 import org.powermock.reflect.Whitebox;
\r
41 @PrepareForTest({EngineWrapper.class, EngineService.class, HttpsUtils.class, HttpResponse.class,
\r
43 @RunWith(PowerMockRunner.class)
\r
44 public class EngineWrapperTest {
\r
47 public ExpectedException thrown = ExpectedException.none();
\r
48 private EngineWrapper engineWrapper = new EngineWrapper();
\r
49 private EngineService engineServiceMock;
\r
50 private HttpResponse httpResponse;
\r
51 private StatusLine statusLineMock;
\r
54 public void setUp() throws Exception {
\r
55 engineServiceMock = PowerMock.createMock(EngineService.class);
\r
56 httpResponse = PowerMock.createMock(HttpResponse.class);
\r
57 statusLineMock = PowerMock.createMock(StatusLine.class);
\r
58 Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock);
\r
62 public void deployEngine_invoke_rule_deploy_exception() throws Exception {
\r
63 thrown.expect(CorrelationException.class);
\r
64 thrown.expectMessage("Failed to call the rule deployment RESTful API.");
\r
67 engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),
\r
68 EasyMock.anyObject(String.class)))
\r
70 new RuntimeException(""));
\r
71 PowerMock.replayAll();
\r
73 engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34");
\r
75 PowerMock.verifyAll();
\r
79 public void deployEngine_http_status_not_ok() throws Exception {
\r
80 thrown.expect(CorrelationException.class);
\r
81 thrown.expectMessage("Failed to deploy the rule!");
\r
84 engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),
\r
85 EasyMock.anyObject(String.class)))
\r
86 .andReturn(httpResponse);
\r
87 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
88 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
\r
89 PowerMock.replayAll();
\r
91 engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34");
\r
93 PowerMock.verifyAll();
\r
97 public void deployEngine_parse_content_exception() throws Exception {
\r
98 PowerMock.resetAll();
\r
99 String content = "";
\r
100 PowerMockito.mockStatic(HttpsUtils.class);
\r
101 thrown.expect(CorrelationException.class);
\r
102 thrown.expectMessage(
\r
103 "Failed to parse the value returned by the engine management service.");
\r
105 engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),
\r
106 EasyMock.anyObject(String.class)))
\r
107 .andReturn(httpResponse);
\r
108 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
109 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
110 PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content);
\r
111 PowerMock.replayAll();
\r
113 engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34");
\r
115 PowerMock.verifyAll();
\r
119 public void deployEngine_success() throws Exception {
\r
120 PowerMock.resetAll();
\r
121 String content = "{\"packageName\":\"test\"}";
\r
122 PowerMockito.mockStatic(HttpsUtils.class);
\r
124 engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),
\r
125 EasyMock.anyObject(String.class)))
\r
126 .andReturn(httpResponse);
\r
127 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
128 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
129 PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content);
\r
130 PowerMock.replayAll();
\r
132 String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34");
\r
134 assertThat(result, equalTo("test"));
\r
139 public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception {
\r
140 thrown.expect(CorrelationException.class);
\r
141 thrown.expectMessage("Failed to call the rule deleting RESTful API.");
\r
143 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class),
\r
144 EasyMock.anyObject(String.class))).andThrow(
\r
145 new RuntimeException(""));
\r
146 PowerMock.replayAll();
\r
148 engineWrapper.deleteRuleFromEngine("","10.96.33.34");
\r
150 PowerMock.verifyAll();
\r
154 public void deleteRuleFromEngine_http_status_not_ok() throws Exception {
\r
155 thrown.expect(CorrelationException.class);
\r
156 thrown.expectMessage("Failed to delete the rule!");
\r
158 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class),
\r
159 EasyMock.anyObject(String.class)))
\r
160 .andReturn(httpResponse);
\r
161 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
162 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
\r
164 PowerMock.replayAll();
\r
166 engineWrapper.deleteRuleFromEngine("","10.96.33.34");
\r
168 PowerMock.verifyAll();
\r
172 public void deleteRuleFromEngine_success() throws Exception {
\r
173 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class),
\r
174 EasyMock.anyObject(String.class)))
\r
175 .andReturn(httpResponse);
\r
176 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
177 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
179 PowerMock.replayAll();
\r
181 boolean result = engineWrapper.deleteRuleFromEngine("","10.96.33.34");
\r
183 assertThat(result, equalTo(true));
\r
187 public void checkRuleFromEngine_rule_delete_exception() throws Exception {
\r
188 thrown.expect(CorrelationException.class);
\r
189 thrown.expectMessage("Failed to call the rule verification RESTful API.");
\r
192 engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class),
\r
193 EasyMock.anyObject(String.class)))
\r
195 new RuntimeException(""));
\r
196 PowerMock.replayAll();
\r
198 engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(),"10.96.33.34");
\r
200 PowerMock.verifyAll();
\r
204 public void checkRuleFromEngine_success() throws Exception {
\r
206 engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class),
\r
207 EasyMock.anyObject(String.class)))
\r
208 .andReturn(httpResponse);
\r
209 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
210 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
212 PowerMock.replayAll();
\r
214 boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(),"10.96.33.34");
\r
216 assertThat(result, equalTo(true));
\r