2 * Copyright 2017 ZTE Corporation.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.onap.holmes.engine.resources;
\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
30 import javax.servlet.http.HttpServletRequest;
\r
31 import javax.ws.rs.WebApplicationException;
\r
32 import java.util.Locale;
\r
34 import static org.easymock.EasyMock.*;
\r
36 public class EngineResourcesTest {
\r
38 public ExpectedException thrown = ExpectedException.none();
\r
39 DroolsEngine droolsEngine;
\r
40 private EngineResources engineResources;
\r
43 public void setUp() {
\r
44 droolsEngine = PowerMock.createMock(DroolsEngine.class);
\r
45 engineResources = new EngineResources();
\r
47 Whitebox.setInternalState(engineResources,"droolsEngine", droolsEngine);
\r
48 PowerMock.resetAll();
\r
52 public void deployRule_exception() throws CorrelationException {
\r
53 DeployRuleRequest deployRuleRequest = new DeployRuleRequest();
\r
54 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\r
56 thrown.expect(WebApplicationException.class);
\r
58 expect(httpRequest.getHeader("language-option")).andReturn("en_US");
\r
59 expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class))).
\r
60 andThrow(new CorrelationException(""));
\r
61 PowerMock.replayAll();
\r
62 engineResources.deployRule(deployRuleRequest, httpRequest);
\r
63 PowerMock.verifyAll();
\r
67 public void deployRule_normal() throws CorrelationException {
\r
68 DeployRuleRequest deployRuleRequest = new DeployRuleRequest();
\r
69 deployRuleRequest.setContent("package packageName;\n\nimport xxx.xxx.xxx;");
\r
70 deployRuleRequest.setLoopControlName("loopControlName");
\r
71 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\r
73 expect(httpRequest.getHeader("language-option")).andReturn("en_US");
\r
74 expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class))).andReturn("packageName");
\r
75 PowerMock.replayAll();
\r
76 engineResources.deployRule(deployRuleRequest, httpRequest);
\r
77 PowerMock.verifyAll();
\r
81 public void undeployRule_exception() throws CorrelationException {
\r
82 String packageName = "packageName";
\r
83 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\r
85 thrown.expect(WebApplicationException.class);
\r
87 expect(httpRequest.getHeader("language-option")).andReturn("en_US");
\r
88 droolsEngine.undeployRule(anyObject(String.class));
\r
89 expectLastCall().andThrow(new CorrelationException(""));
\r
90 PowerMock.replayAll();
\r
91 engineResources.undeployRule(packageName, httpRequest);
\r
92 PowerMock.verifyAll();
\r
96 public void undeployRule_normal() throws CorrelationException {
\r
97 String packageName = "packageName";
\r
98 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\r
100 expect(httpRequest.getHeader("language-option")).andReturn("en_US");
\r
101 droolsEngine.undeployRule(anyObject(String.class));
\r
102 PowerMock.replayAll();
\r
103 engineResources.undeployRule(packageName, httpRequest);
\r
104 PowerMock.verifyAll();
\r
108 public void compileRule_exception() throws CorrelationException {
\r
109 CompileRuleRequest compileRuleRequest = new CompileRuleRequest();
\r
110 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\r
112 thrown.expect(WebApplicationException.class);
\r
114 expect(httpRequest.getHeader("language-option")).andReturn("en_US");
\r
115 droolsEngine.compileRule(anyObject(String.class));
\r
116 expectLastCall().andThrow(new CorrelationException(""));
\r
117 PowerMock.replayAll();
\r
118 engineResources.compileRule(compileRuleRequest, httpRequest);
\r
119 PowerMock.verifyAll();
\r
123 public void compileRule_normal() throws CorrelationException {
\r
124 CompileRuleRequest compileRuleRequest = new CompileRuleRequest();
\r
125 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\r
127 expect(httpRequest.getHeader("language-option")).andReturn("en_US");
\r
128 droolsEngine.compileRule(anyObject(String.class));
\r
129 PowerMock.replayAll();
\r
130 engineResources.compileRule(compileRuleRequest, httpRequest);
\r
131 PowerMock.verifyAll();
\r