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
69 new RuntimeException(""));
\r
70 PowerMock.replayAll();
\r
72 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\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 .andReturn(httpResponse);
\r
85 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
86 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
\r
87 PowerMock.replayAll();
\r
89 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
91 PowerMock.verifyAll();
\r
95 public void deployEngine_parse_content_exception() throws Exception {
\r
96 PowerMock.resetAll();
\r
97 String content = "";
\r
98 PowerMockito.mockStatic(HttpsUtils.class);
\r
99 thrown.expect(CorrelationException.class);
\r
100 thrown.expectMessage(
\r
101 "Failed to parse the value returned by the engine management service.");
\r
103 engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
\r
104 .andReturn(httpResponse);
\r
105 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
106 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
107 PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content);
\r
108 PowerMock.replayAll();
\r
110 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
112 PowerMock.verifyAll();
\r
116 public void deployEngine_success() throws Exception {
\r
117 PowerMock.resetAll();
\r
118 String content = "{\"packageName\":\"test\"}";
\r
119 PowerMockito.mockStatic(HttpsUtils.class);
\r
121 engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
\r
122 .andReturn(httpResponse);
\r
123 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
124 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
125 PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content);
\r
126 PowerMock.replayAll();
\r
128 String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
130 assertThat(result, equalTo("test"));
\r
135 public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception {
\r
136 thrown.expect(CorrelationException.class);
\r
137 thrown.expectMessage("Failed to call the rule deleting RESTful API.");
\r
139 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow(
\r
140 new RuntimeException(""));
\r
141 PowerMock.replayAll();
\r
143 engineWrapper.deleteRuleFromEngine("");
\r
145 PowerMock.verifyAll();
\r
149 public void deleteRuleFromEngine_http_status_not_ok() throws Exception {
\r
150 thrown.expect(CorrelationException.class);
\r
151 thrown.expectMessage("Failed to delete the rule!");
\r
153 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
\r
154 .andReturn(httpResponse);
\r
155 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
156 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
\r
158 PowerMock.replayAll();
\r
160 engineWrapper.deleteRuleFromEngine("");
\r
162 PowerMock.verifyAll();
\r
166 public void deleteRuleFromEngine_success() throws Exception {
\r
167 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
\r
168 .andReturn(httpResponse);
\r
169 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
170 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
172 PowerMock.replayAll();
\r
174 boolean result = engineWrapper.deleteRuleFromEngine("");
\r
176 assertThat(result, equalTo(true));
\r
180 public void checkRuleFromEngine_rule_delete_exception() throws Exception {
\r
181 thrown.expect(CorrelationException.class);
\r
182 thrown.expectMessage("Failed to call the rule verification RESTful API.");
\r
185 engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
\r
187 new RuntimeException(""));
\r
188 PowerMock.replayAll();
\r
190 engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
\r
192 PowerMock.verifyAll();
\r
196 public void checkRuleFromEngine_success() throws Exception {
\r
198 engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
\r
199 .andReturn(httpResponse);
\r
200 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
\r
201 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
203 PowerMock.replayAll();
\r
205 boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
\r
207 assertThat(result, equalTo(true));
\r