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