--- /dev/null
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.holmes.engine.resources;\r
+\r
+import org.junit.Before;\r
+import org.junit.Rule;\r
+import org.junit.Test;\r
+import org.junit.rules.ExpectedException;\r
+import org.onap.holmes.common.exception.CorrelationException;\r
+import org.onap.holmes.engine.manager.DroolsEngine;\r
+import org.onap.holmes.engine.request.CompileRuleRequest;\r
+import org.onap.holmes.engine.request.DeployRuleRequest;\r
+import org.powermock.api.easymock.PowerMock;\r
+import org.powermock.reflect.Whitebox;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.ws.rs.WebApplicationException;\r
+import java.util.Locale;\r
+\r
+import static org.easymock.EasyMock.*;\r
+\r
+public class EngineResourcesTest {\r
+ @Rule\r
+ public ExpectedException thrown = ExpectedException.none();\r
+ DroolsEngine droolsEngine;\r
+ private EngineResources engineResources;\r
+\r
+ @Before\r
+ public void setUp() {\r
+ droolsEngine = PowerMock.createMock(DroolsEngine.class);\r
+ engineResources = new EngineResources();\r
+\r
+ Whitebox.setInternalState(engineResources,"droolsEngine",droolsEngine);\r
+ PowerMock.resetAll();\r
+ }\r
+\r
+ @Test\r
+ public void deployRule_exception() throws CorrelationException {\r
+ DeployRuleRequest deployRuleRequest = new DeployRuleRequest();\r
+ HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+ thrown.expect(WebApplicationException.class);\r
+\r
+ expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+ expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class), anyObject(Locale.class))).\r
+ andThrow(new CorrelationException(""));\r
+ PowerMock.replayAll();\r
+ engineResources.deployRule(deployRuleRequest, httpRequest);\r
+ PowerMock.verifyAll();\r
+ }\r
+\r
+ @Test\r
+ public void deployRule_normal() throws CorrelationException {\r
+ DeployRuleRequest deployRuleRequest = new DeployRuleRequest();\r
+ HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+ expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+ expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class),\r
+ anyObject(Locale.class))).andReturn("packageName");\r
+ PowerMock.replayAll();\r
+ engineResources.deployRule(deployRuleRequest, httpRequest);\r
+ PowerMock.verifyAll();\r
+ }\r
+\r
+ @Test\r
+ public void undeployRule_exception() throws CorrelationException {\r
+ String packageName = "packageName";\r
+ HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+ thrown.expect(WebApplicationException.class);\r
+\r
+ expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+ droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class));\r
+ expectLastCall().andThrow(new CorrelationException(""));\r
+ PowerMock.replayAll();\r
+ engineResources.undeployRule(packageName, httpRequest);\r
+ PowerMock.verifyAll();\r
+ }\r
+\r
+ @Test\r
+ public void undeployRule_normal() throws CorrelationException {\r
+ String packageName = "packageName";\r
+ HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+ expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+ droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class));\r
+ PowerMock.replayAll();\r
+ engineResources.undeployRule(packageName, httpRequest);\r
+ PowerMock.verifyAll();\r
+ }\r
+\r
+ @Test\r
+ public void compileRule_exception() throws CorrelationException {\r
+ CompileRuleRequest compileRuleRequest = new CompileRuleRequest();\r
+ HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+ thrown.expect(WebApplicationException.class);\r
+\r
+ expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+ droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class));\r
+ expectLastCall().andThrow(new CorrelationException(""));\r
+ PowerMock.replayAll();\r
+ engineResources.compileRule(compileRuleRequest, httpRequest);\r
+ PowerMock.verifyAll();\r
+ }\r
+\r
+ @Test\r
+ public void compileRule_normal() throws CorrelationException {\r
+ CompileRuleRequest compileRuleRequest = new CompileRuleRequest();\r
+ HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+ expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+ droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class));\r
+ PowerMock.replayAll();\r
+ engineResources.compileRule(compileRuleRequest, httpRequest);\r
+ PowerMock.verifyAll();\r
+ }\r
+}\r