a676031cb86f4ba0b3ad030c8452a4f51bbd663c
[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
30 import org.junit.Test;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32
33 /**
34  * Test the deployment web socket client.
35  */
36 public class EngineServiceFacadeTest {
37     @Test
38     public void testEngineServiceFacade() throws Exception {
39         EngineServiceFacade facade = new EngineServiceFacade("localhost", 51273);
40
41         facade.setDeploymentClient(new DummyDeploymentClient("localhost", 51273));
42
43         // First init should fail
44         facade.init();
45
46         assertNull(facade.getKey());
47         assertNull(facade.getApexModelKey());
48         assertNull(facade.getEngineKeyArray());
49
50         try {
51             facade.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false);
52             fail("test should throw an exception here");
53         } catch (final Exception ade) {
54             assertEquals("cound not deploy apex model, deployer is not initialized", ade.getMessage());
55         }
56
57         // Second init should work
58         facade.init();
59
60         assertEquals("EngineService:0.0.1", facade.getKey().getId());
61         assertEquals("Model:0.0.1", facade.getApexModelKey().getId());
62         assertEquals("Engine:0.0.1", facade.getEngineKeyArray()[0].getId());
63
64         try {
65             facade.deployModel("src/test/resources/models/NonExistantModel.json", false, false);
66             fail("test should throw an exception here");
67         } catch (final Exception ade) {
68             assertEquals("cound not create apex model, could not read from file "
69                             + "src/test/resources/models/NonExistantModel.json", ade.getMessage());
70         }
71
72         try {
73             facade.deployModel("src/test/resources/models/JunkModel.json", false, false);
74             fail("test should throw an exception here");
75         } catch (final Exception ade) {
76             assertEquals("could not deploy apex model from src/test/resources/models/JunkModel.json", ade.getMessage());
77         }
78
79         InputStream badStream = new ByteArrayInputStream("".getBytes());
80         try {
81             facade.deployModel("MyModel", badStream, false, false);
82             fail("test should throw an exception here");
83         } catch (final Exception ade) {
84             assertEquals("format of input for Apex concept is neither JSON nor XML", ade.getMessage());
85         }
86
87         InputStream closedStream = new ByteArrayInputStream("".getBytes());
88         closedStream.close();
89         try {
90             facade.deployModel("MyModel", closedStream, false, false);
91             fail("test should throw an exception here");
92         } catch (final Exception ade) {
93             assertEquals("format of input for Apex concept is neither JSON nor XML", ade.getMessage());
94         }
95
96         try {
97             facade.deployModel("src/test/resources/models/SmallModel.json", false, false);
98             fail("test should throw an exception here");
99         } catch (final Exception ade) {
100             assertEquals("could not deploy apex model from src/test/resources/models/SmallModel.json",
101                             ade.getMessage());
102         }
103
104         try {
105             facade.deployModel("src/test/resources/models/SmallModel.json", false, false);
106         } catch (final Exception ade) {
107             fail("test should not throw an exception here");
108         }
109
110         try {
111             facade.startEngine(facade.getEngineKeyArray()[0]);
112             fail("test should throw an exception here");
113         } catch (final Exception ade) {
114             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
115         }
116
117         try {
118             facade.startEngine(facade.getEngineKeyArray()[0]);
119         } catch (final Exception ade) {
120             fail("test should not throw an exception here");
121         }
122
123         try {
124             facade.stopEngine(facade.getEngineKeyArray()[0]);
125             fail("test should throw an exception here");
126         } catch (final Exception ade) {
127             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
128         }
129
130         try {
131             facade.stopEngine(facade.getEngineKeyArray()[0]);
132         } catch (final Exception ade) {
133             fail("test should not throw an exception here");
134         }
135
136         try {
137             facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000);
138             fail("test should throw an exception here");
139         } catch (final Exception ade) {
140             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
141         }
142
143         try {
144             facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000);
145         } catch (final Exception ade) {
146             fail("test should not throw an exception here");
147         }
148
149         try {
150             facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
151             fail("test should throw an exception here");
152         } catch (final Exception ade) {
153             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
154         }
155
156         try {
157             facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
158         } catch (final Exception ade) {
159             fail("test should not throw an exception here");
160         }
161
162         try {
163             facade.getEngineStatus(facade.getEngineKeyArray()[0]);
164             fail("test should throw an exception here");
165         } catch (final Exception ade) {
166             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
167         }
168
169         try {
170             facade.getEngineStatus(facade.getEngineKeyArray()[0]);
171         } catch (final Exception ade) {
172             fail("test should not throw an exception here");
173         }
174
175         try {
176             facade.getEngineInfo(facade.getEngineKeyArray()[0]);
177             fail("test should throw an exception here");
178         } catch (final Exception ade) {
179             assertEquals("failed response Operation failed received from serverlocalhost:51273", ade.getMessage());
180         }
181
182         try {
183             facade.getEngineInfo(facade.getEngineKeyArray()[0]);
184         } catch (final Exception ade) {
185             fail("test should not throw an exception here");
186         }
187
188         try {
189             facade.getEngineStatus(new AxArtifactKey("ReturnBadMessage", "0.0.1"));
190             fail("test should throw an exception here");
191         } catch (final Exception ade) {
192             assertEquals("response received from server is of incorrect type "
193                             + "org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus, should be of type "
194                             + "org.onap.policy.apex.core.protocols.engdep.messages.Response", ade.getMessage());
195         }
196
197         try {
198             facade.getEngineStatus(new AxArtifactKey("ReturnBadResponse", "0.0.1"));
199             fail("test should throw an exception here");
200         } catch (final Exception ade) {
201             assertEquals("response received is not correct response to sent message GET_ENGINE_STATUS",
202                             ade.getMessage());
203         }
204
205         try {
206             facade.getEngineStatus(new AxArtifactKey("DoNotRespond", "0.0.1"));
207             fail("test should throw an exception here");
208         } catch (final Exception ade) {
209             assertEquals("no response received to sent message GET_ENGINE_STATUS", ade.getMessage());
210         }
211
212         facade.close();
213     }
214 }