Remove try/catch blocks with assertj - apex-pdp
[policy/apex-pdp.git] / core / core-deployment / src / test / java / org / onap / policy / apex / core / deployment / BatchDeployerTest.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
26 import java.io.ByteArrayOutputStream;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.PrintStream;
31 import java.time.Duration;
32 import org.awaitility.Awaitility;
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
35 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
36 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
37 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
38
39 /**
40  * Test the periodic event manager utility.
41  */
42 public class BatchDeployerTest {
43     @Test
44     public void testBatchDeployerBad() {
45         final String[] eventArgs = { "-h" };
46
47         assertThatThrownBy(() -> BatchDeployer.main(eventArgs))
48             .hasMessageContaining("invalid arguments: [-h]");
49     }
50
51     @Test
52     public void testBatchDeployerBadPort() {
53         final String[] eventArgs = { "localhost", "aport", "afile" };
54
55         assertThatThrownBy(() -> BatchDeployer.main(eventArgs))
56             .hasMessage("argument port is invalid");
57     }
58
59     @Test
60     public void testBatchDeployerOk() {
61         final String[] eventArgs = { "Host", "43443",
62             "src/test/resources/models/SamplePolicyModelJAVASCRIPT.json" };
63
64         assertThatThrownBy(() -> BatchDeployer.main(eventArgs))
65             .hasMessage("model deployment failed on parameters Host 43443");
66     }
67
68     @Test
69     public void testBatchDeployerDeployString() throws ApexException {
70         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
71
72         BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
73         final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
74         deployer.getEngineServiceFacade().setDeploymentClient(dummyDeploymentClient);
75
76         // We are testing towards a dummy client, make it return a failed initiation
77         dummyDeploymentClient.setInitSuccessful(false);
78         assertThatThrownBy(deployer::init).hasMessage("model deployment failed on parameters localhost 12345");
79         // Wait until the connection to the server closes following the bad connection
80         // attempt
81         Awaitility.await().atLeast(Duration.ofMillis(500));
82
83         // We are testing towards a dummy client, make it return a successful initiation
84         dummyDeploymentClient.setInitSuccessful(true);
85         deployer.init();
86
87         assertThatThrownBy(() -> deployer.deployModel("src/test/resources/models/SmallModel.json", false, false))
88             .hasMessage("could not deploy apex model from src/test/resources/models/SmallModel.json");
89         deployer.deployModel("src/test/resources/models/SmallModel.json", false, false);
90
91         deployer.close();
92     }
93
94     @Test
95     public void testBatchDeployerStream() throws FileNotFoundException, ApexException {
96
97         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
98
99         BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
100         final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
101         deployer.getEngineServiceFacade().setDeploymentClient(dummyDeploymentClient);
102
103         dummyDeploymentClient.setInitSuccessful(false);
104         assertThatThrownBy(deployer::init)
105             .hasMessage("model deployment failed on parameters localhost 12345");
106         // Wait until the connection to the server closes following the bad connection
107         // attempt
108         Awaitility.await().atLeast(Duration.ofMillis(500));
109
110         dummyDeploymentClient.setInitSuccessful(true);
111
112         deployer.init();
113
114         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
115         modelReader.setValidateFlag(false);
116         final AxPolicyModel apexPolicyModel = modelReader
117             .read(new FileInputStream(new File("src/test/resources/models/SmallModel.json")));
118
119         assertThatThrownBy(() -> deployer.deployModel(apexPolicyModel, false, false))
120             .hasMessage("failed response Operation failed received from serverlocalhost:12345");
121
122         deployer.deployModel(apexPolicyModel, false, false);
123
124         deployer.close();
125     }
126
127     @Test
128     public void testBatchDeployerUninitialized() {
129         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
130
131         BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
132         deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
133
134         assertThatThrownBy(() -> deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json",
135                 false, false))
136             .hasMessage("could not deploy apex model, deployer is not initialized");
137         assertThatThrownBy(() -> deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json",
138                 false, false))
139             .hasMessage("could not deploy apex model, deployer is not initialized");
140
141         deployer.close();
142     }
143
144     @Test
145     public void testBatchDeployerStreamUninitialized() throws ApexModelException, FileNotFoundException {
146         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
147
148         BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
149         deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
150
151         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
152         modelReader.setValidateFlag(false);
153         final AxPolicyModel apexPolicyModel = modelReader
154             .read(new FileInputStream(new File("src/test/resources/models/SmallModel.json")));
155
156         assertThatThrownBy(() -> deployer.deployModel(apexPolicyModel, false, false))
157             .hasMessage("failed response Operation failed received from serverlocalhost:12345");
158         deployer.close();
159     }
160 }