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