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;
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
34 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
35 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
36 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
39 * Test the periodic event manager utility.
41 public class BatchDeployerTest {
43 public void testBatchDeployerBad() {
45 final String[] eventArgs =
48 BatchDeployer.main(eventArgs);
49 fail("test should throw an exception");
50 } catch (Exception exc) {
51 assertEquals("invalid arguments: [-h]", exc.getMessage().substring(0, 23));
56 public void testBatchDeployerBadPort() {
58 final String[] eventArgs =
59 { "localhost", "aport", "afile" };
61 BatchDeployer.main(eventArgs);
62 fail("test should throw an exception");
63 } catch (Exception exc) {
64 assertEquals("argument port is invalid", exc.getMessage().substring(0, 24));
69 public void testBatchDeployerOk() {
71 final String[] eventArgs =
72 { "Host", "43443", "src/test/resources/models/SamplePolicyModelJAVASCRIPT.json" };
74 BatchDeployer.main(eventArgs);
75 } catch (Exception exc) {
76 assertEquals("model deployment failed on parameters Host 43443", exc.getMessage());
81 public void testBatchDeployerDeployString() {
82 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
84 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
85 deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
89 } catch (ApexDeploymentException ade) {
90 assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage());
95 } catch (ApexDeploymentException ade) {
96 ade.printStackTrace();
97 fail("test should not throw an exception");
101 deployer.deployModel("src/test/resources/models/SmallModel.json", false, false);
102 } catch (ApexException ade) {
103 assertEquals("could not deploy apex model from src/test/resources/models/SmallModel.json",
108 deployer.deployModel("src/test/resources/models/SmallModel.json", false, false);
109 } catch (ApexException ade) {
110 fail("test should not throw an exception");
117 public void testBatchDeployerStream() throws ApexModelException, FileNotFoundException {
119 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
121 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
122 deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
126 } catch (ApexDeploymentException ade) {
127 assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage());
132 } catch (ApexDeploymentException ade) {
133 ade.printStackTrace();
134 fail("test should not throw an exception");
137 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
138 modelReader.setValidateFlag(false);
139 final AxPolicyModel apexPolicyModel = modelReader.read(
140 new FileInputStream(new File("src/test/resources/models/SmallModel.json")));
143 deployer.deployModel(apexPolicyModel, false, false);
144 } catch (ApexException ade) {
145 assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage());
149 deployer.deployModel(apexPolicyModel, false, false);
150 } catch (ApexException ade) {
151 fail("test should not throw an exception");
158 public void testBatchDeployerUninitialized() {
159 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
161 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
162 deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
165 deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false);
166 fail("test should throw an exception");
167 } catch (ApexException ade) {
168 assertEquals("cound not deploy apex model, deployer is not initialized", ade.getMessage());
172 deployer.deployModel("src/test/resources/models/SamplePolicyModelJAVASCRIPT.json", false, false);
173 fail("test should throw an exception");
174 } catch (ApexException ade) {
175 assertEquals("cound not deploy apex model, deployer is not initialized", ade.getMessage());
182 public void testBatchDeployerStreamUninitialized() throws ApexModelException, FileNotFoundException {
183 final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
185 BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
186 deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
188 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
189 modelReader.setValidateFlag(false);
190 final AxPolicyModel apexPolicyModel = modelReader.read(
191 new FileInputStream(new File("src/test/resources/models/SmallModel.json")));
194 deployer.deployModel(apexPolicyModel, false, false);
195 fail("test should throw an exception");
196 } catch (ApexException ade) {
197 assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage());