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