e0610df0dd780599db0604ba27123e2545a5641a
[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  * ================================================================================
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.deployment;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
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
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;
37
38 /**
39  * Test the periodic event manager utility.
40  */
41 public class BatchDeployerTest {
42     @Test
43     public void testBatchDeployerBad() {
44         try {
45             final String[] eventArgs =
46                 { "-h" };
47
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));
52         }
53     }
54
55     @Test
56     public void testBatchDeployerBadPort() {
57         try {
58             final String[] eventArgs =
59                 { "localhost", "aport", "afile" };
60
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));
65         }
66     }
67
68     @Test
69     public void testBatchDeployerOk() {
70         try {
71             final String[] eventArgs =
72                 { "Host", "43443", "src/test/resources/models/SamplePolicyModelJAVASCRIPT.json" };
73
74             BatchDeployer.main(eventArgs);
75         } catch (Exception exc) {
76             assertEquals("model deployment failed on parameters Host 43443", exc.getMessage());
77         }
78     }
79     
80     @Test
81     public void testBatchDeployerDeployString() {
82         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
83
84         BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
85         deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
86
87         try {
88             deployer.init();
89         } catch (ApexDeploymentException ade) {
90             assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage());
91         }
92
93         try {
94             deployer.init();
95         } catch (ApexDeploymentException ade) {
96             ade.printStackTrace();
97             fail("test should not throw an exception");
98         }
99
100         try {
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",
104                             ade.getMessage());
105         }
106
107         try {
108             deployer.deployModel("src/test/resources/models/SmallModel.json", false, false);
109         } catch (ApexException ade) {
110             fail("test should not throw an exception");
111         }
112
113         deployer.close();
114     }
115
116     @Test
117     public void testBatchDeployerStream() throws ApexModelException, FileNotFoundException {
118
119         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
120
121         BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
122         deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
123
124         try {
125             deployer.init();
126         } catch (ApexDeploymentException ade) {
127             assertEquals("model deployment failed on parameters localhost 12345 true", ade.getMessage());
128         }
129
130         try {
131             deployer.init();
132         } catch (ApexDeploymentException ade) {
133             ade.printStackTrace();
134             fail("test should not throw an exception");
135         }
136
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")));
141
142         try {
143             deployer.deployModel(apexPolicyModel, false, false);
144         } catch (ApexException ade) {
145             assertEquals("failed response Operation failed received from serverlocalhost:12345", ade.getMessage());
146         }
147
148         try {
149             deployer.deployModel(apexPolicyModel, false, false);
150         } catch (ApexException ade) {
151             fail("test should not throw an exception");
152         }
153
154         deployer.close();
155     }
156
157     @Test
158     public void testBatchDeployerUninitialized() {
159         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
160
161         BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
162         deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
163
164         try {
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());
169         }
170
171         try {
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());
176         }
177
178         deployer.close();
179     }
180
181     @Test
182     public void testBatchDeployerStreamUninitialized() throws ApexModelException, FileNotFoundException {
183         final ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
184
185         BatchDeployer deployer = new BatchDeployer("localhost", 12345, new PrintStream(baosOut, true));
186         deployer.getEngineServiceFacade().setDeploymentClient(new DummyDeploymentClient("aHost", 54553));
187
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")));
192
193         try {
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());
198         }
199
200         deployer.close();
201     }
202 }