Migrate from DW to Springboot
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / bolt / enginebolt / EngineWrapperTest.java
1 /**\r
2  * Copyright 2017 - 2021 ZTE Corporation.\r
3  * <p>\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  * <p>\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  * <p>\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.rulemgt.bolt.enginebolt;\r
18 \r
19 \r
20 import org.junit.After;\r
21 import org.junit.Before;\r
22 import org.junit.Rule;\r
23 import org.junit.Test;\r
24 import org.junit.rules.ExpectedException;\r
25 import org.junit.runner.RunWith;\r
26 import org.onap.holmes.common.exception.CorrelationException;\r
27 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
28 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
29 import org.powermock.modules.junit4.PowerMockRunner;\r
30 import org.powermock.reflect.Whitebox;\r
31 \r
32 import static org.easymock.EasyMock.*;\r
33 import static org.hamcrest.MatcherAssert.assertThat;\r
34 import static org.hamcrest.Matchers.equalTo;\r
35 import static org.hamcrest.Matchers.is;\r
36 import static org.powermock.api.easymock.PowerMock.createMock;\r
37 import static org.powermock.api.easymock.PowerMock.*;\r
38 \r
39 @RunWith(PowerMockRunner.class)\r
40 public class EngineWrapperTest {\r
41 \r
42     @Rule\r
43     public ExpectedException thrown = ExpectedException.none();\r
44 \r
45     private EngineWrapper engineWrapper = new EngineWrapper();\r
46 \r
47     private EngineService mockedEngineService;\r
48 \r
49     @Before\r
50     public void before() {\r
51         mockedEngineService = createMock(EngineService.class);\r
52         Whitebox.setInternalState(engineWrapper, "engineService", mockedEngineService);\r
53     }\r
54 \r
55     @After\r
56     public void after() {\r
57         resetAll();\r
58     }\r
59 \r
60     @Test\r
61     public void deployEngine_fail() throws Exception {\r
62         thrown.expect(CorrelationException.class);\r
63         thrown.expectMessage("Failed to deploy the rule!");\r
64 \r
65         expect(mockedEngineService.deploy(anyObject(CorrelationDeployRule4Engine.class),\r
66                 anyObject(String.class))).andReturn(null);\r
67 \r
68         replayAll();\r
69 \r
70         engineWrapper.deployEngine(new CorrelationDeployRule4Engine(), "127.0.0.1");\r
71 \r
72         verifyAll();\r
73     }\r
74 \r
75     @Test\r
76     public void deployEngine_parse_content_exception() throws Exception {\r
77         thrown.expect(CorrelationException.class);\r
78         thrown.expectMessage(\r
79                 "Failed to parse the value returned by the engine management service.");\r
80         expect(mockedEngineService.deploy(anyObject(CorrelationDeployRule4Engine.class),\r
81                 anyObject(String.class))).andReturn("");\r
82 \r
83         replayAll();\r
84 \r
85         engineWrapper.deployEngine(new CorrelationDeployRule4Engine(), "127.0.0.1");\r
86 \r
87         verifyAll();\r
88     }\r
89 \r
90     @Test\r
91     public void deployEngine_success() throws Exception {\r
92         String content = "{\"packageName\":\"test\"}";\r
93         expect(mockedEngineService.deploy(anyObject(CorrelationDeployRule4Engine.class),\r
94                 anyObject(String.class))).andReturn(content);\r
95 \r
96         replayAll();\r
97 \r
98         String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine(), "127.0.0.1");\r
99 \r
100         assertThat(result, equalTo("test"));\r
101 \r
102     }\r
103 \r
104     @Test\r
105     public void deleteRuleFromEngine_fail() throws Exception {\r
106         thrown.expect(CorrelationException.class);\r
107         thrown.expectMessage("Failed to delete the rule!");\r
108 \r
109         expect(mockedEngineService.delete(anyObject(String.class),\r
110                 anyObject(String.class)))\r
111                 .andReturn(false);\r
112 \r
113         replayAll();\r
114 \r
115         engineWrapper.deleteRuleFromEngine("", "127.0.0.1");\r
116 \r
117         verifyAll();\r
118     }\r
119 \r
120     @Test\r
121     public void deleteRuleFromEngine_success() throws Exception {\r
122         expect(mockedEngineService.delete(anyObject(String.class),\r
123                 anyObject(String.class)))\r
124                 .andReturn(true);\r
125 \r
126         replayAll();\r
127 \r
128         boolean result = engineWrapper.deleteRuleFromEngine("", "127.0.0.1");\r
129 \r
130         assertThat(result, equalTo(true));\r
131     }\r
132 \r
133     @Test\r
134     public void checkRuleFromEngine_fail() throws Exception {\r
135         thrown.expect(CorrelationException.class);\r
136         thrown.expectMessage("Failed to verify the rule. The contents of the rule are invalid.");\r
137 \r
138         expect(\r
139                 mockedEngineService.check(anyObject(CorrelationCheckRule4Engine.class),\r
140                         anyObject(String.class))).andReturn(false);\r
141         replayAll();\r
142 \r
143         engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(), "127.0.0.1");\r
144 \r
145         verifyAll();\r
146     }\r
147 \r
148     @Test\r
149     public void checkRuleFromEngine_success() throws Exception {\r
150         expect(mockedEngineService.check(anyObject(CorrelationCheckRule4Engine.class),anyString())).andReturn(true);\r
151 \r
152         replayAll();\r
153 \r
154         boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(), "127.0.0.1");\r
155 \r
156         assertThat(result, is(true));\r
157     }\r
158 }