6e0826c3f0e0fb41db82b6187b2e32a59a10cdd5
[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  *  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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
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         assertThatThrownBy(facade::init)
47             .hasMessage("could not handshake with server localhost:51273");
48         assertNull(facade.getKey());
49         assertNull(facade.getApexModelKey());
50         assertNull(facade.getEngineKeyArray());
51
52         assertThatThrownBy(() -> facade.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json",
53                 false, false))
54             .hasMessage("could not deploy apex model, deployer is not initialized");
55
56         // Second init should work
57         dummyDeploymentClient.setInitSuccessful(true);
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         assertThatThrownBy(() -> facade.deployModel("src/test/resources/models/NonExistantModel.json",
65                 false, false))
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",
69                 false, false))
70             .hasMessage("could not deploy apex model from src/test/resources/models/JunkModel.json");
71
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());
76         closedStream.close();
77
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);
83
84         assertThatThrownBy(() -> facade.startEngine(facade.getEngineKeyArray()[0]))
85             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
86         facade.startEngine(facade.getEngineKeyArray()[0]);
87
88         assertThatThrownBy(() -> facade.stopEngine(facade.getEngineKeyArray()[0]))
89             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
90         facade.stopEngine(facade.getEngineKeyArray()[0]);
91
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);
95
96         assertThatThrownBy(() -> facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]))
97             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
98         facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
99
100         assertThatThrownBy(() -> facade.getEngineStatus(facade.getEngineKeyArray()[0]))
101             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
102         facade.getEngineStatus(facade.getEngineKeyArray()[0]);
103
104         assertThatThrownBy(() -> facade.getEngineInfo(facade.getEngineKeyArray()[0]))
105             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
106         facade.getEngineInfo(facade.getEngineKeyArray()[0]);
107
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");
118
119         facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
120
121         facade.getEngineStatus(facade.getEngineKeyArray()[0]);
122
123         assertThatThrownBy(() -> facade.getEngineInfo(facade.getEngineKeyArray()[0]))
124             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
125
126         facade.getEngineInfo(facade.getEngineKeyArray()[0]);
127
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");
136         facade.close();
137     }
138 }