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 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.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 Response response;
\r
44 private StatusLine statusLineMock;
\r
47 public void setUp() throws Exception {
\r
48 engineServiceMock = PowerMock.createMock(EngineService.class);
\r
49 response = PowerMock.createMock(Response.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(response);
\r
75 EasyMock.expect(response.getStatus()).andReturn(400);
\r
76 PowerMock.replayAll();
\r
78 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
80 PowerMock.verifyAll();
\r
84 public void deployEngine_parse_content_exception() throws Exception {
\r
85 String content = "";
\r
87 thrown.expect(CorrelationException.class);
\r
88 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR);
\r
89 EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
\r
90 .andReturn(response);
\r
91 EasyMock.expect(response.getStatus()).andReturn(200);
\r
92 EasyMock.expect(response.readEntity(String.class)).andReturn(content);
\r
93 PowerMock.replayAll();
\r
95 engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
97 PowerMock.verifyAll();
\r
101 public void deployEngine_success() throws Exception {
\r
102 String content = "{\"package\":\"test\"}";
\r
103 EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
\r
104 .andReturn(response);
\r
105 EasyMock.expect(response.getStatus()).andReturn(200);
\r
106 EasyMock.expect(response.readEntity(String.class)).andReturn(content);
\r
107 PowerMock.replayAll();
\r
109 String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
\r
111 assertThat(result, equalTo("test"));
\r
116 public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception {
\r
117 thrown.expect(CorrelationException.class);
\r
118 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED);
\r
120 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow(
\r
121 new RuntimeException(""));
\r
122 PowerMock.replayAll();
\r
124 engineWrapper.deleteRuleFromEngine("");
\r
126 PowerMock.verifyAll();
\r
130 public void deleteRuleFromEngine_http_status_not_ok() throws Exception {
\r
131 thrown.expect(CorrelationException.class);
\r
132 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED);
\r
134 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
\r
135 .andReturn(response);
\r
136 EasyMock.expect(response.getStatus()).andReturn(400);
\r
138 PowerMock.replayAll();
\r
140 engineWrapper.deleteRuleFromEngine("");
\r
142 PowerMock.verifyAll();
\r
146 public void deleteRuleFromEngine_success() throws Exception {
\r
147 EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
\r
148 .andReturn(response);
\r
149 EasyMock.expect(response.getStatus()).andReturn(200);
\r
151 PowerMock.replayAll();
\r
153 boolean result = engineWrapper.deleteRuleFromEngine("");
\r
155 assertThat(result, equalTo(true));
\r
159 public void checkRuleFromEngine_rule_delete_exception() throws Exception {
\r
160 thrown.expect(CorrelationException.class);
\r
161 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_CHECK_RULE_REST_FAILED);
\r
163 EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow(
\r
164 new RuntimeException(""));
\r
165 PowerMock.replayAll();
\r
167 engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
\r
169 PowerMock.verifyAll();
\r
173 public void checkRuleFromEngine_success() throws Exception {
\r
174 EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
\r
175 .andReturn(response);
\r
176 EasyMock.expect(response.getStatus()).andReturn(200);
\r
178 PowerMock.replayAll();
\r
180 boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
\r
182 assertThat(result, equalTo(true));
\r