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.openo.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.openo.holmes.common.exception.CorrelationException;
\r
31 import org.openo.holmes.common.utils.I18nProxy;
\r
32 import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
\r
33 import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
\r
34 import org.powermock.api.easymock.PowerMock;
\r
35 import org.powermock.reflect.Whitebox;
\r
37 public class EngineWrapperTest {
\r
40 public ExpectedException thrown = ExpectedException.none();
\r
41 private EngineWrapper engineWrapper = new EngineWrapper();
\r
42 private EngineService engineServiceMock;
\r
43 private HttpResponse httpResponseMock;
\r
44 private StatusLine statusLineMock;
\r
47 public void setUp() throws Exception {
\r
48 engineServiceMock = PowerMock.createMock(EngineService.class);
\r
49 httpResponseMock = PowerMock.createMock(HttpResponse.class);
\r
50 statusLineMock = PowerMock.createMock(StatusLine.class);
\r
51 Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock);
\r
55 public void deployEngine_invoke_rule_deploy_exception() throws Exception {
\r
56 thrown.expect(CorrelationException.class);
\r
57 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED);
\r
59 EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))).andThrow(
\r
60 new RuntimeException(""));
\r
61 PowerMock.replayAll();
\r
63 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
65 PowerMock.verifyAll();
\r
69 public void deployEngine_http_status_not_ok() throws Exception {
\r
70 thrown.expect(CorrelationException.class);
\r
71 thrown.expectMessage(I18nProxy.ENGINE_DEPLOY_RULE_FAILED);
\r
73 EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
\r
74 .andReturn(httpResponseMock);
\r
75 EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
\r
76 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
\r
78 PowerMock.replayAll();
\r
80 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
82 PowerMock.verifyAll();
\r
86 public void deployEngine_parse_content_exception() throws Exception {
\r
87 String content = "";
\r
89 thrown.expect(CorrelationException.class);
\r
90 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR);
\r
91 EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
\r
92 .andReturn(httpResponseMock);
\r
93 EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
\r
94 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
95 EasyMock.expect(engineServiceMock.getResponseContent(EasyMock.anyObject(HttpResponse.class)))
\r
96 .andReturn(content);
\r
98 PowerMock.replayAll();
\r
100 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
102 PowerMock.verifyAll();
\r
106 public void deployEngine_success() throws Exception {
\r
107 String content = "{\"package\":\"test\"}";
\r
108 EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
\r
109 .andReturn(httpResponseMock);
\r
110 EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
\r
111 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
112 EasyMock.expect(engineServiceMock.getResponseContent(EasyMock.anyObject(HttpResponse.class)))
\r
113 .andReturn(content);
\r
114 PowerMock.replayAll();
\r
116 String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
118 assertThat(result, equalTo("test"));
\r
123 public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception {
\r
124 thrown.expect(CorrelationException.class);
\r
125 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED);
\r
127 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow(
\r
128 new RuntimeException(""));
\r
129 PowerMock.replayAll();
\r
131 engineWrapper.deleteRuleFromEngine("");
\r
133 PowerMock.verifyAll();
\r
137 public void deleteRuleFromEngine_http_status_not_ok() throws Exception {
\r
138 thrown.expect(CorrelationException.class);
\r
139 thrown.expectMessage(I18nProxy.ENGINE_DELETE_RULE_FAILED);
\r
141 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
\r
142 .andReturn(httpResponseMock);
\r
143 EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
\r
144 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
\r
146 PowerMock.replayAll();
\r
148 engineWrapper.deleteRuleFromEngine("");
\r
150 PowerMock.verifyAll();
\r
154 public void deleteRuleFromEngine_success() throws Exception {
\r
155 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
\r
156 .andReturn(httpResponseMock);
\r
157 EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
\r
158 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
160 PowerMock.replayAll();
\r
162 boolean result = engineWrapper.deleteRuleFromEngine("");
\r
164 assertThat(result, equalTo(true));
\r
168 public void checkRuleFromEngine_rule_delete_exception() throws Exception {
\r
169 thrown.expect(CorrelationException.class);
\r
170 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_CHECK_RULE_REST_FAILED);
\r
172 EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow(
\r
173 new RuntimeException(""));
\r
174 PowerMock.replayAll();
\r
176 engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
\r
178 PowerMock.verifyAll();
\r
182 public void checkRuleFromEngine_http_status_not_200() throws Exception {
\r
183 thrown.expect(CorrelationException.class);
\r
184 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CHECK_NO_PASS);
\r
186 EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
\r
187 .andReturn(httpResponseMock);
\r
188 EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
\r
189 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
\r
191 PowerMock.replayAll();
\r
193 engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
\r
195 PowerMock.verifyAll();
\r
199 public void checkRuleFromEngine_success() throws Exception {
\r
200 EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
\r
201 .andReturn(httpResponseMock);
\r
202 EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
\r
203 EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
\r
205 PowerMock.replayAll();
\r
207 boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
\r
209 assertThat(result, equalTo(true));
\r