Fix modify rules of fault
[holmes/rule-management.git] / rulemgt / src / test / java / org / openo / holmes / rulemgt / bolt / enginebolt / EngineWrapperTest.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 package org.openo.holmes.rulemgt.bolt.enginebolt;\r
18 \r
19 \r
20 import static org.hamcrest.MatcherAssert.assertThat;\r
21 import static org.hamcrest.Matchers.equalTo;\r
22 \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
36 \r
37 public class EngineWrapperTest {\r
38 \r
39     @Rule\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
45 \r
46     @Before\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
52     }\r
53 \r
54     @Test\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
58 \r
59         EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))).andThrow(\r
60                 new RuntimeException(""));\r
61         PowerMock.replayAll();\r
62 \r
63         engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
64 \r
65         PowerMock.verifyAll();\r
66     }\r
67 \r
68     @Test\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
72 \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
77 \r
78         engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
79 \r
80         PowerMock.verifyAll();\r
81     }\r
82 \r
83     @Test\r
84     public void deployEngine_parse_content_exception() throws Exception {\r
85         String content = "";\r
86 \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
94 \r
95         engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
96 \r
97         PowerMock.verifyAll();\r
98     }\r
99 \r
100     @Test\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
108 \r
109         String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
110 \r
111         assertThat(result, equalTo("test"));\r
112 \r
113     }\r
114 \r
115     @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
119 \r
120         EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow(\r
121                 new RuntimeException(""));\r
122         PowerMock.replayAll();\r
123 \r
124         engineWrapper.deleteRuleFromEngine("");\r
125 \r
126         PowerMock.verifyAll();\r
127     }\r
128 \r
129     @Test\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
133 \r
134         EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))\r
135                 .andReturn(response);\r
136         EasyMock.expect(response.getStatus()).andReturn(400);\r
137 \r
138         PowerMock.replayAll();\r
139 \r
140         engineWrapper.deleteRuleFromEngine("");\r
141 \r
142         PowerMock.verifyAll();\r
143     }\r
144 \r
145     @Test\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
150 \r
151         PowerMock.replayAll();\r
152 \r
153         boolean result = engineWrapper.deleteRuleFromEngine("");\r
154 \r
155         assertThat(result, equalTo(true));\r
156     }\r
157 \r
158     @Test\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
162 \r
163         EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow(\r
164                 new RuntimeException(""));\r
165         PowerMock.replayAll();\r
166 \r
167         engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());\r
168 \r
169         PowerMock.verifyAll();\r
170     }\r
171 \r
172     @Test\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
177 \r
178         PowerMock.replayAll();\r
179 \r
180         boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());\r
181 \r
182         assertThat(result, equalTo(true));\r
183     }\r
184 }