Remove try/catch blocks with assertj - apex-pdp
[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 java.time.Duration;
31 import org.awaitility.Awaitility;
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34
35 /**
36  * Test the deployment web socket client.
37  */
38 public class EngineServiceFacadeTest {
39     @Test
40     public void testEngineServiceFacade() throws Exception {
41         EngineServiceFacade facade = new EngineServiceFacade("localhost", 51273);
42
43         final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
44         facade.setDeploymentClient(dummyDeploymentClient);
45
46         // First init should fail due to our dummy client
47         dummyDeploymentClient.setInitSuccessful(false);
48         assertThatThrownBy(facade::init)
49             .hasMessage("could not handshake with server localhost:51273");
50         assertNull(facade.getKey());
51         assertNull(facade.getApexModelKey());
52         assertNull(facade.getEngineKeyArray());
53
54         assertThatThrownBy(() -> facade.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json",
55                 false, false))
56             .hasMessage("could not deploy apex model, deployer is not initialized");
57
58         // Second init should work
59         Awaitility.await().atLeast(Duration.ofMillis(1000));
60         dummyDeploymentClient.setInitSuccessful(true);
61         facade.init();
62
63         assertEquals("EngineService:0.0.1", facade.getKey().getId());
64         assertEquals("Model:0.0.1", facade.getApexModelKey().getId());
65         assertEquals("Engine:0.0.1", facade.getEngineKeyArray()[0].getId());
66
67         assertThatThrownBy(() -> facade.deployModel("src/test/resources/models/NonExistantModel.json",
68                 false, false))
69             .hasMessage("could not create apex model, could not read from file "
70                             + "src/test/resources/models/NonExistantModel.json");
71         assertThatThrownBy(() -> facade.deployModel("src/test/resources/models/JunkModel.json",
72                 false, false))
73             .hasMessage("could not deploy apex model from src/test/resources/models/JunkModel.json");
74
75         InputStream badStream = new ByteArrayInputStream("".getBytes());
76         assertThatThrownBy(() -> facade.deployModel("MyModel", badStream, false, false))
77             .hasMessage("format of input for Apex concept is neither JSON nor XML");
78         InputStream closedStream = new ByteArrayInputStream("".getBytes());
79         closedStream.close();
80
81         assertThatThrownBy(() -> facade.deployModel("MyModel", closedStream, false, false))
82             .hasMessage("format of input for Apex concept is neither JSON nor XML");
83         assertThatThrownBy(() -> facade.deployModel("src/test/resources/models/SmallModel.json", false, false))
84             .hasMessage("could not deploy apex model from src/test/resources/models/SmallModel.json");
85         facade.deployModel("src/test/resources/models/SmallModel.json", false, false);
86
87         assertThatThrownBy(() -> facade.startEngine(facade.getEngineKeyArray()[0]))
88             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
89         facade.startEngine(facade.getEngineKeyArray()[0]);
90
91         assertThatThrownBy(() -> facade.stopEngine(facade.getEngineKeyArray()[0]))
92             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
93         facade.stopEngine(facade.getEngineKeyArray()[0]);
94
95         assertThatThrownBy(() -> facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000))
96             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
97         facade.startPerioidicEvents(facade.getEngineKeyArray()[0], 1000);
98
99         assertThatThrownBy(() -> facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]))
100             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
101         facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
102
103         assertThatThrownBy(() -> facade.getEngineStatus(facade.getEngineKeyArray()[0]))
104             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
105         facade.getEngineStatus(facade.getEngineKeyArray()[0]);
106
107         assertThatThrownBy(() -> facade.getEngineInfo(facade.getEngineKeyArray()[0]))
108             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
109         facade.getEngineInfo(facade.getEngineKeyArray()[0]);
110
111         assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("ReturnBadMessage", "0.0.1")))
112             .hasMessage("response received from server is of incorrect type "
113                 + "org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus, should be of type "
114                 + "org.onap.policy.apex.core.protocols.engdep.messages.Response");
115         assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("ReturnBadResponse", "0.0.1")))
116             .hasMessage("response received is not correct response to sent message GET_ENGINE_STATUS");
117         assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("DoNotRespond", "0.0.1")))
118             .hasMessage("no response received to sent message GET_ENGINE_STATUS");
119         assertThatThrownBy(() -> facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]))
120             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
121
122         facade.stopPerioidicEvents(facade.getEngineKeyArray()[0]);
123
124         facade.getEngineStatus(facade.getEngineKeyArray()[0]);
125
126         assertThatThrownBy(() -> facade.getEngineInfo(facade.getEngineKeyArray()[0]))
127             .hasMessage("failed response Operation failed received from serverlocalhost:51273");
128
129         facade.getEngineInfo(facade.getEngineKeyArray()[0]);
130
131         assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("ReturnBadMessage", "0.0.1")))
132             .hasMessage("response received from server is of incorrect type "
133                             + "org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus, should be of type "
134                             + "org.onap.policy.apex.core.protocols.engdep.messages.Response");
135         assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("ReturnBadResponse", "0.0.1")))
136             .hasMessage("response received is not correct response to sent message GET_ENGINE_STATUS");
137         assertThatThrownBy(() -> facade.getEngineStatus(new AxArtifactKey("DoNotRespond", "0.0.1")))
138             .hasMessage("no response received to sent message GET_ENGINE_STATUS");
139         facade.close();
140     }
141 }