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