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;
26 import java.io.ByteArrayOutputStream;
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;
40 * Test the periodic event manager utility.
42 public class BatchDeployerTest {
44 public void testBatchDeployerBad() {
45 final String[] eventArgs = { "-h" };
47 assertThatThrownBy(() -> BatchDeployer.main(eventArgs))
48 .hasMessageContaining("invalid arguments: [-h]");
52 public void testBatchDeployerBadPort() {
53 final String[] eventArgs = { "localhost", "aport", "afile" };
55 assertThatThrownBy(() -> BatchDeployer.main(eventArgs))
56 .hasMessage("argument port is invalid");
60 public void testBatchDeployerOk() {
61 final String[] eventArgs = { "Host", "43443",
62 "src/test/resources/models/SamplePolicyModelJAVASCRIPT.json" };
64 assertThatThrownBy(() -> BatchDeployer.main(eventArgs))
65 .hasMessage("model deployment failed on parameters Host 43443");
69 public void testBatchDeployerDeployString() throws ApexException {
70 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
72 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
73 final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
74 deployer.getEngineServiceFacade().setDeploymentClient(dummyDeploymentClient);
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
81 Awaitility.await().atLeast(Duration.ofMillis(500));
83 // We are testing towards a dummy client, make it return a successful initiation
84 dummyDeploymentClient.setInitSuccessful(true);
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);
95 public void testBatchDeployerStream() throws FileNotFoundException, ApexException {
97 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
99 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
100 final DummyDeploymentClient dummyDeploymentClient = new DummyDeploymentClient("aHost", 54553);
101 deployer.getEngineServiceFacade().setDeploymentClient(dummyDeploymentClient);
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
108 Awaitility.await().atLeast(Duration.ofMillis(500));
110 dummyDeploymentClient.setInitSuccessful(true);
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")));
119 assertThatThrownBy(() -> deployer.deployModel(apexPolicyModel, false, false))
120 .hasMessage("failed response Operation failed received from serverlocalhost:12345");
122 deployer.deployModel(apexPolicyModel, false, false);
128 public void testBatchDeployerUninitialized() {
129 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
131 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
132 deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
134 assertThatThrownBy(() -> deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json",
136 .hasMessage("could not deploy apex model, deployer is not initialized");
137 assertThatThrownBy(() -> deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json",
139 .hasMessage("could not deploy apex model, deployer is not initialized");
145 public void testBatchDeployerStreamUninitialized() throws ApexModelException, FileNotFoundException {
146 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
148 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
149 deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
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")));
156 assertThatThrownBy(() -> deployer.deployModel(apexPolicyModel, false, false))
157 .hasMessage("failed response Operation failed received from serverlocalhost:12345");