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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.deployment;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
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;
34 * Test the deployment web socket client.
36 public class EngineServiceFacadeTest {
38 public void testEngineServiceFacade() throws Exception {
39 EngineServiceFacade facade = new EngineServiceFacade("localhost", 51273);
41 final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
42 facade.setDeploymentClient(dummyDeploymentClient);
44 // First init should fail due to our dummy client
45 dummyDeploymentClient.setInitSuccessful(false);
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());
53 assertNull(facade.getKey());
54 assertNull(facade.getApexModelKey());
55 assertNull(facade.getEngineKeyArray());
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());
64 // Second init should work
65 dummyDeploymentClient.setInitSuccessful(true);
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());
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());
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());
87 InputStream badStream = new ByteArrayInputStream("".getBytes());
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());
95 InputStream closedStream = new ByteArrayInputStream("".getBytes());
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());
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",
112 facade.deployModel("src/test/resources/models/SmallModel.json", false, false);
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());
121 facade.startEngine(facade.getEngineKeyArray()[0]);
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());
130 facade.stopEngine(facade.getEngineKeyArray()[0]);
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());
139 facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000);
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());
148 facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
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());
157 facade.getEngineStatus(facade.getEngineKeyArray()[0]);
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());
166 facade.getEngineInfo(facade.getEngineKeyArray()[0]);
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());
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",
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());