2a403366f68b34ae0c7867577237c73c090c0351
[holmes/engine-management.git] / engine-d / src / test / java / org / openo / holmes / engine / resources / EngineResourcesTest.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 package org.openo.holmes.engine.resources;\r
17 \r
18 import org.junit.Before;\r
19 import org.junit.Rule;\r
20 import org.junit.Test;\r
21 import org.junit.rules.ExpectedException;\r
22 import org.openo.holmes.common.exception.CorrelationException;\r
23 import org.openo.holmes.engine.manager.DroolsEngine;\r
24 import org.openo.holmes.engine.request.CompileRuleRequest;\r
25 import org.openo.holmes.engine.request.DeployRuleRequest;\r
26 import org.powermock.api.easymock.PowerMock;\r
27 import org.powermock.reflect.Whitebox;\r
28 \r
29 import javax.servlet.http.HttpServletRequest;\r
30 import javax.ws.rs.WebApplicationException;\r
31 import java.util.Locale;\r
32 \r
33 import static org.easymock.EasyMock.*;\r
34 \r
35 /**\r
36  * Created by Administrator on 2017/2/20.\r
37  */\r
38 public class EngineResourcesTest {\r
39     @Rule\r
40     public ExpectedException thrown = ExpectedException.none();\r
41     DroolsEngine droolsEngine;\r
42     private EngineResources engineResources;\r
43 \r
44     @Before\r
45     public void setUp() {\r
46         droolsEngine = PowerMock.createMock(DroolsEngine.class);\r
47         engineResources = new EngineResources();\r
48 \r
49         Whitebox.setInternalState(engineResources,"droolsEngine",droolsEngine);\r
50         PowerMock.resetAll();\r
51     }\r
52 \r
53     @Test\r
54     public void deployRule_exception() throws CorrelationException {\r
55         DeployRuleRequest deployRuleRequest = new DeployRuleRequest();\r
56         HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
57 \r
58         thrown.expect(WebApplicationException.class);\r
59 \r
60         expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
61         expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class), anyObject(Locale.class))).\r
62                 andThrow(new CorrelationException(""));\r
63         PowerMock.replayAll();\r
64         engineResources.deployRule(deployRuleRequest, httpRequest);\r
65         PowerMock.verifyAll();\r
66     }\r
67 \r
68     @Test\r
69     public void deployRule_normal() throws CorrelationException {\r
70         DeployRuleRequest deployRuleRequest = new DeployRuleRequest();\r
71         HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
72 \r
73         expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
74         expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class),\r
75                 anyObject(Locale.class))).andReturn("packageName");\r
76         PowerMock.replayAll();\r
77         engineResources.deployRule(deployRuleRequest, httpRequest);\r
78         PowerMock.verifyAll();\r
79     }\r
80 \r
81     @Test\r
82     public void undeployRule_exception() throws CorrelationException {\r
83         String packageName = "packageName";\r
84         HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
85 \r
86         thrown.expect(WebApplicationException.class);\r
87 \r
88         expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
89         droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class));\r
90         expectLastCall().andThrow(new CorrelationException(""));\r
91         PowerMock.replayAll();\r
92         engineResources.undeployRule(packageName, httpRequest);\r
93         PowerMock.verifyAll();\r
94     }\r
95 \r
96     @Test\r
97     public void undeployRule_normal() throws CorrelationException {\r
98         String packageName = "packageName";\r
99         HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
100 \r
101         expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
102         droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class));\r
103         PowerMock.replayAll();\r
104         engineResources.undeployRule(packageName, httpRequest);\r
105         PowerMock.verifyAll();\r
106     }\r
107 \r
108     @Test\r
109     public void compileRule_exception() throws CorrelationException {\r
110         CompileRuleRequest compileRuleRequest = new CompileRuleRequest();\r
111         HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
112 \r
113         thrown.expect(WebApplicationException.class);\r
114 \r
115         expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
116         droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class));\r
117         expectLastCall().andThrow(new CorrelationException(""));\r
118         PowerMock.replayAll();\r
119         engineResources.compileRule(compileRuleRequest, httpRequest);\r
120         PowerMock.verifyAll();\r
121     }\r
122 \r
123     @Test\r
124     public void compileRule_normal() throws CorrelationException {\r
125         CompileRuleRequest compileRuleRequest = new CompileRuleRequest();\r
126         HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
127 \r
128         expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
129         droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class));\r
130         PowerMock.replayAll();\r
131         engineResources.compileRule(compileRuleRequest, httpRequest);\r
132         PowerMock.verifyAll();\r
133     }\r
134 }\r