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 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
36 public class EngineWrapperTest {
\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
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
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
58 EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))).andThrow(
\r
59 new RuntimeException(""));
\r
60 PowerMock.replayAll();
\r
62 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
64 PowerMock.verifyAll();
\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
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
77 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
79 PowerMock.verifyAll();
\r
83 public void deployEngine_parse_content_exception() throws Exception {
\r
84 String content = "";
\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
94 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
96 PowerMock.verifyAll();
\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
108 String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
110 assertThat(result, equalTo("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
119 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow(
\r
120 new RuntimeException(""));
\r
121 PowerMock.replayAll();
\r
123 engineWrapper.deleteRuleFromEngine("");
\r
125 PowerMock.verifyAll();
\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
133 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
\r
134 .andReturn(response);
\r
135 EasyMock.expect(response.getStatus()).andReturn(400);
\r
137 PowerMock.replayAll();
\r
139 engineWrapper.deleteRuleFromEngine("");
\r
141 PowerMock.verifyAll();
\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
150 PowerMock.replayAll();
\r
152 boolean result = engineWrapper.deleteRuleFromEngine("");
\r
154 assertThat(result, equalTo(true));
\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
162 EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow(
\r
163 new RuntimeException(""));
\r
164 PowerMock.replayAll();
\r
166 engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
\r
168 PowerMock.verifyAll();
\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
177 PowerMock.replayAll();
\r
179 boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
\r
181 assertThat(result, equalTo(true));
\r