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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
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);
46 assertThatThrownBy(facade::init)
47 .hasMessage("could not handshake with server localhost:51273");
48 assertNull(facade.getKey());
49 assertNull(facade.getApexModelKey());
50 assertNull(facade.getEngineKeyArray());
52 assertThatThrownBy(() -> facade.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json",
54 .hasMessage("could not deploy apex model, deployer is not initialized");
56 // Second init should work
57 dummyDeploymentClient.setInitSuccessful(true);
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());
64 assertThatThrownBy(() -> facade.deployModel("src/test/resources/models/NonExistantModel.json",
66 .hasMessage("could not create apex model, could not read from file "
67 + "src/test/resources/models/NonExistantModel.json");
68 assertThatThrownBy(() -> facade.deployModel("src/test/resources/models/JunkModel.json",
70 .hasMessage("could not deploy apex model from src/test/resources/models/JunkModel.json");
72 InputStream badStream = new ByteArrayInputStream("".getBytes());
73 assertThatThrownBy(() -> facade.deployModel("MyModel", badStream, false, false))
74 .hasMessage("format of input for Apex concept is neither JSON nor XML");
75 InputStream closedStream = new ByteArrayInputStream("".getBytes());
78 assertThatThrownBy(() -> facade.deployModel("MyModel", closedStream, false, false))
79 .hasMessage("format of input for Apex concept is neither JSON nor XML");
80 assertThatThrownBy(() -> facade.deployModel("src/test/resources/models/SmallModel.json", false, false))
81 .hasMessage("could not deploy apex model from src/test/resources/models/SmallModel.json");
82 facade.deployModel("src/test/resources/models/SmallModel.json", false, false);
84 assertThatThrownBy(() -> facade.startEngine(facade.getEngineKeyArray()[0]))
85 .hasMessage("failed response Operation failed received from serverlocalhost:51273");
86 facade.startEngine(facade.getEngineKeyArray()[0]);
88 assertThatThrownBy(() -> facade.stopEngine(facade.getEngineKeyArray()[0]))
89 .hasMessage("failed response Operation failed received from serverlocalhost:51273");
90 facade.stopEngine(facade.getEngineKeyArray()[0]);
92 assertThatThrownBy(() -> facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000))
93 .hasMessage("failed response Operation failed received from serverlocalhost:51273");
94 facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000);
96 assertThatThrownBy(() -> facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]))
97 .hasMessage("failed response Operation failed received from serverlocalhost:51273");
98 facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
100 assertThatThrownBy(() -> facade.getEngineStatus(facade.getEngineKeyArray()[0]))
101 .hasMessage("failed response Operation failed received from serverlocalhost:51273");
102 facade.getEngineStatus(facade.getEngineKeyArray()[0]);
104 assertThatThrownBy(() -> facade.getEngineInfo(facade.getEngineKeyArray()[0]))
105 .hasMessage("failed response Operation failed received from serverlocalhost:51273");
106 facade.getEngineInfo(facade.getEngineKeyArray()[0]);
108 assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("ReturnBadMessage", "0.0.1")))
109 .hasMessage("response received from server is of incorrect type "
110 + "org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus, should be of type "
111 + "org.onap.policy.apex.core.protocols.engdep.messages.Response");
112 assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("ReturnBadResponse", "0.0.1")))
113 .hasMessage("response received is not correct response to sent message GET_ENGINE_STATUS");
114 assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("DoNotRespond", "0.0.1")))
115 .hasMessage("no response received to sent message GET_ENGINE_STATUS");
116 assertThatThrownBy(() -> facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]))
117 .hasMessage("failed response Operation failed received from serverlocalhost:51273");
119 facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
121 facade.getEngineStatus(facade.getEngineKeyArray()[0]);
123 assertThatThrownBy(() -> facade.getEngineInfo(facade.getEngineKeyArray()[0]))
124 .hasMessage("failed response Operation failed received from serverlocalhost:51273");
126 facade.getEngineInfo(facade.getEngineKeyArray()[0]);
128 assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("ReturnBadMessage", "0.0.1")))
129 .hasMessage("response received from server is of incorrect type "
130 + "org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus, should be of type "
131 + "org.onap.policy.apex.core.protocols.engdep.messages.Response");
132 assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("ReturnBadResponse", "0.0.1")))
133 .hasMessage("response received is not correct response to sent message GET_ENGINE_STATUS");
134 assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("DoNotRespond", "0.0.1")))
135 .hasMessage("no response received to sent message GET_ENGINE_STATUS");