Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-deployment / src / test / java / org / onap / policy / apex / core / deployment / EngineServiceFacadeTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.deployment;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.fail;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.InputStream;
29 import org.junit.Test;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31
32 /**
33  * Test the deployment web socket client.
34  */
35 public class EngineServiceFacadeTest {
36     @Test
37     public void testEngineServiceFacade() throws Exception {
38         EngineServiceFacade facade = new EngineServiceFacade("localhost", 51273);
39
40         facade.setDeploymentClient(new DummyDeploymentClient("localhost", 51273));
41
42         // First init should fail
43         facade.init();
44
45         assertNull(facade.getKey());
46         assertNull(facade.getApexModelKey());
47         assertNull(facade.getEngineKeyArray());
48
49         try {
50             facade.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false);
51             fail("test should throw an exception here");
52         } catch (final Exception ade) {
53             assertEquals("cound not deploy apex model, deployer is not initialized", ade.getMessage());
54         }
55
56         // Second init should work
57         facade.init();
58
59         assertEquals("EngineService:0.0.1", facade.getKey().getId());
60         assertEquals("Model:0.0.1", facade.getApexModelKey().getId());
61         assertEquals("Engine:0.0.1", facade.getEngineKeyArray()[0].getId());
62
63         try {
64             facade.deployModel("src/test/resources/models/NonExistantModel.json", false, false);
65             fail("test should throw an exception here");
66         } catch (final Exception ade) {
67             assertEquals("cound not create apex model, could not read from file "
68                             + "src/test/resources/models/NonExistantModel.json", ade.getMessage());
69         }
70
71         try {
72             facade.deployModel("src/test/resources/models/JunkModel.json", false, false);
73             fail("test should throw an exception here");
74         } catch (final Exception ade) {
75             assertEquals("could not deploy apex model from src/test/resources/models/JunkModel.json", ade.getMessage());
76         }
77
78         InputStream badStream = new ByteArrayInputStream("".getBytes());
79         try {
80             facade.deployModel("MyModel", badStream, false, false);
81             fail("test should throw an exception here");
82         } catch (final Exception ade) {
83             assertEquals("format of input for Apex concept is neither JSON nor XML", ade.getMessage());
84         }
85
86         InputStream closedStream = new ByteArrayInputStream("".getBytes());
87         closedStream.close();
88         try {
89             facade.deployModel("MyModel", closedStream, false, false);
90             fail("test should throw an exception here");
91         } catch (final Exception ade) {
92             assertEquals("format of input for Apex concept is neither JSON nor XML", ade.getMessage());
93         }
94
95         try {
96             facade.deployModel("src/test/resources/models/SmallModel.json", false, false);
97             fail("test should throw an exception here");
98         } catch (final Exception ade) {
99             assertEquals("could not deploy apex model from src/test/resources/models/SmallModel.json",
100                             ade.getMessage());
101         }
102
103         try {
104             facade.deployModel("src/test/resources/models/SmallModel.json", false, false);
105         } catch (final Exception ade) {
106             fail("test should not throw an exception here");
107         }
108
109         try {
110             facade.startEngine(facade.getEngineKeyArray()[0]);
111             fail("test should throw an exception here");
112         } catch (final Exception ade) {
113             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
114         }
115
116         try {
117             facade.startEngine(facade.getEngineKeyArray()[0]);
118         } catch (final Exception ade) {
119             fail("test should not throw an exception here");
120         }
121
122         try {
123             facade.stopEngine(facade.getEngineKeyArray()[0]);
124             fail("test should throw an exception here");
125         } catch (final Exception ade) {
126             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
127         }
128
129         try {
130             facade.stopEngine(facade.getEngineKeyArray()[0]);
131         } catch (final Exception ade) {
132             fail("test should not throw an exception here");
133         }
134
135         try {
136             facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000);
137             fail("test should throw an exception here");
138         } catch (final Exception ade) {
139             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
140         }
141
142         try {
143             facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000);
144         } catch (final Exception ade) {
145             fail("test should not throw an exception here");
146         }
147
148         try {
149             facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
150             fail("test should throw an exception here");
151         } catch (final Exception ade) {
152             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
153         }
154
155         try {
156             facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
157         } catch (final Exception ade) {
158             fail("test should not throw an exception here");
159         }
160
161         try {
162             facade.getEngineStatus(facade.getEngineKeyArray()[0]);
163             fail("test should throw an exception here");
164         } catch (final Exception ade) {
165             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
166         }
167
168         try {
169             facade.getEngineStatus(facade.getEngineKeyArray()[0]);
170         } catch (final Exception ade) {
171             fail("test should not throw an exception here");
172         }
173
174         try {
175             facade.getEngineInfo(facade.getEngineKeyArray()[0]);
176             fail("test should throw an exception here");
177         } catch (final Exception ade) {
178             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
179         }
180
181         try {
182             facade.getEngineInfo(facade.getEngineKeyArray()[0]);
183         } catch (final Exception ade) {
184             fail("test should not throw an exception here");
185         }
186
187         try {
188             facade.getEngineStatus(new AxArtifactKey("ReturnBadMessage", "0.0.1"));
189             fail("test should throw an exception here");
190         } catch (final Exception ade) {
191             assertEquals("response received from server is of incorrect type "
192                             + "org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus, should be of type "
193                             + "org.onap.policy.apex.core.protocols.engdep.messages.Response", ade.getMessage());
194         }
195
196         try {
197             facade.getEngineStatus(new AxArtifactKey("ReturnBadResponse", "0.0.1"));
198             fail("test should throw an exception here");
199         } catch (final Exception ade) {
200             assertEquals("response received is not correct response to sent message GET_ENGINE_STATUS",
201                             ade.getMessage());
202         }
203
204         try {
205             facade.getEngineStatus(new AxArtifactKey("DoNotRespond", "0.0.1"));
206             fail("test should throw an exception here");
207         } catch (final Exception ade) {
208             assertEquals("no response received to sent message GET_ENGINE_STATUS", ade.getMessage());
209         }
210
211         facade.close();
212     }
213 }