2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.deployment;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
26 import java.io.ByteArrayOutputStream;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.PrintStream;
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
34 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
35 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
38 * Test the periodic event manager utility.
40 public class BatchDeployerTest {
42 public void testBatchDeployerBad() {
44 final String[] eventArgs =
47 BatchDeployer.main(eventArgs);
48 fail("test should throw an exception");
49 } catch (Exception exc) {
50 assertEquals("invalid arguments: [-h]", exc.getMessage().substring(0, 23));
55 public void testBatchDeployerBadPort() {
57 final String[] eventArgs =
58 { "localhost", "aport", "afile" };
60 BatchDeployer.main(eventArgs);
61 fail("test should throw an exception");
62 } catch (Exception exc) {
63 assertEquals("argument port is invalid", exc.getMessage().substring(0, 24));
68 public void testBatchDeployerOk() {
70 final String[] eventArgs =
71 { "Host", "43443", "src/test/resources/models/SamplePolicyModelJAVASCRIPT.json" };
73 BatchDeployer.main(eventArgs);
74 } catch (Exception exc) {
75 assertEquals("model deployment failed on parameters Host 43443", exc.getMessage());
80 public void testBatchDeployerDeployString() {
81 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
83 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
84 deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
88 } catch (ApexDeploymentException ade) {
89 assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage());
94 } catch (ApexDeploymentException ade) {
95 ade.printStackTrace();
96 fail("test should not throw an exception");
100 deployer.deployModel("src/test/resources/models/SmallModel.json", false, false);
101 } catch (ApexException ade) {
102 assertEquals("could not deploy apex model from src/test/resources/models/SmallModel.json",
107 deployer.deployModel("src/test/resources/models/SmallModel.json", false, false);
108 } catch (ApexException ade) {
109 fail("test should not throw an exception");
116 public void testBatchDeployerStream() throws ApexModelException, FileNotFoundException {
118 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
120 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
121 deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
125 } catch (ApexDeploymentException ade) {
126 assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage());
131 } catch (ApexDeploymentException ade) {
132 ade.printStackTrace();
133 fail("test should not throw an exception");
136 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
137 modelReader.setValidateFlag(false);
138 final AxPolicyModel apexPolicyModel = modelReader.read(
139 new FileInputStream(new File("src/test/resources/models/SmallModel.json")));
142 deployer.deployModel(apexPolicyModel, false, false);
143 } catch (ApexException ade) {
144 assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage());
148 deployer.deployModel(apexPolicyModel, false, false);
149 } catch (ApexException ade) {
150 fail("test should not throw an exception");
157 public void testBatchDeployerUninitialized() {
158 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
160 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
161 deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
164 deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false);
165 fail("test should throw an exception");
166 } catch (ApexException ade) {
167 assertEquals("cound not deploy apex model, deployer is not initialized", ade.getMessage());
171 deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false);
172 fail("test should throw an exception");
173 } catch (ApexException ade) {
174 assertEquals("cound not deploy apex model, deployer is not initialized", ade.getMessage());
181 public void testBatchDeployerStreamUninitialized() throws ApexModelException, FileNotFoundException {
182 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
184 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
185 deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
187 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
188 modelReader.setValidateFlag(false);
189 final AxPolicyModel apexPolicyModel = modelReader.read(
190 new FileInputStream(new File("src/test/resources/models/SmallModel.json")));
193 deployer.deployModel(apexPolicyModel, false, false);
194 fail("test should throw an exception");
195 } catch (ApexException ade) {
196 assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage());