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.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
31 import javax.servlet.http.HttpServletRequest;
\r
32 import javax.ws.rs.WebApplicationException;
\r
33 import java.util.Locale;
\r
35 import static org.easymock.EasyMock.*;
\r
37 public class EngineResourcesTest {
\r
39 public ExpectedException thrown = ExpectedException.none();
\r
40 private DroolsEngine droolsEngine;
\r
41 private EngineResources engineResources;
\r
42 private ClosedLoopControlNameCache closedLoopControlNameCache;
\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
51 Whitebox.setInternalState(engineResources,"droolsEngine", droolsEngine);
\r
52 PowerMock.resetAll();
\r
56 public void deployRule_exception() throws CorrelationException {
\r
57 DeployRuleRequest deployRuleRequest = new DeployRuleRequest();
\r
58 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\r
60 thrown.expect(WebApplicationException.class);
\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
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
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
85 public void undeployRule_exception() throws CorrelationException {
\r
86 String packageName = "packageName";
\r
87 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\r
89 thrown.expect(WebApplicationException.class);
\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
100 public void undeployRule_normal() throws CorrelationException {
\r
101 String packageName = "packageName";
\r
102 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\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
112 public void compileRule_exception() throws CorrelationException {
\r
113 CompileRuleRequest compileRuleRequest = new CompileRuleRequest();
\r
114 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\r
116 thrown.expect(WebApplicationException.class);
\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
127 public void compileRule_normal() throws CorrelationException {
\r
128 CompileRuleRequest compileRuleRequest = new CompileRuleRequest();
\r
129 HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
\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