automate dependency.json version population
[policy/engine.git] / BRMSGateway / src / test / java / org / onap / brmsgw / test / BrmsPushTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.brmsgw.test;
22
23 import static org.junit.Assert.assertNotNull;
24
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Properties;
31
32 import org.junit.Test;
33 import org.onap.policy.api.PolicyException;
34 import org.onap.policy.brms.api.BrmsHandler;
35
36 public class BrmsPushTest {
37
38     private static final String VALID_FILE = "src/test/resources/config.properties";
39     private static final String INVALID_FILE = "src/test/resources/failure.properties";
40
41     @Test(expected = PolicyException.class)
42     public void brmsHandlerFailTest() throws PolicyException {
43         new BrmsHandler(null);
44     }
45
46     @Test(expected = PolicyException.class)
47     public void brmsHandlerFailTest1() throws PolicyException {
48         new BrmsHandler("src/test/resources/filenotexists.txt");
49     }
50
51     @Test(expected = PolicyException.class)
52     public void brmsHandlerFailTest2() throws PolicyException {
53         final PropertyChange prop = new PropertyChange();
54         prop.key = "defaultName";
55         prop.remove = true;
56         final List<PropertyChange> props = new LinkedList<>();
57         props.add(prop);
58         setFailureProperties(props);
59         new BrmsHandler(INVALID_FILE);
60     }
61
62     @Test(expected = PolicyException.class)
63     public void brmsHandlerFailTest3() throws PolicyException {
64         PropertyChange prop = new PropertyChange();
65         prop.key = "repositoryID";
66         prop.remove = true;
67         final List<PropertyChange> props = new LinkedList<>();
68         props.add(prop);
69         prop = new PropertyChange();
70         prop.key = "RESOURCE_NAME";
71         prop.remove = true;
72         props.add(prop);
73         setFailureProperties(props);
74         new BrmsHandler(INVALID_FILE);
75     }
76
77     @Test(expected = PolicyException.class)
78     public void brmsHandlerFailTest4() throws PolicyException {
79         final PropertyChange prop = new PropertyChange();
80         prop.key = "repositoryURL";
81         prop.remove = true;
82         final List<PropertyChange> props = new LinkedList<>();
83         props.add(prop);
84         setFailureProperties(props);
85         new BrmsHandler(INVALID_FILE);
86     }
87
88     @Test(expected = PolicyException.class)
89     public void brmsHandlerFailTest5() throws PolicyException {
90         final PropertyChange prop = new PropertyChange();
91         prop.key = "repositoryName";
92         prop.remove = true;
93         final List<PropertyChange> props = new LinkedList<>();
94         props.add(prop);
95         setFailureProperties(props);
96         new BrmsHandler(INVALID_FILE);
97     }
98
99     @Test(expected = PolicyException.class)
100     public void brmsHandlerFailTest6() throws PolicyException {
101         PropertyChange prop = new PropertyChange();
102         prop.key = "repositoryURL";
103         prop.value =
104                 "http://nexus:8081/nexus/content/repositories/releases, http://nexus:8081/nexus/content/repositories/releases";
105         prop.remove = false;
106         final List<PropertyChange> props = new LinkedList<>();
107         props.add(prop);
108         prop = new PropertyChange();
109         prop.key = "repositoryUsername";
110         prop.remove = true;
111         props.add(prop);
112         setFailureProperties(props);
113         new BrmsHandler(INVALID_FILE);
114     }
115
116     @Test(expected = PolicyException.class)
117     public void brmsHandlerFailTest7() throws PolicyException {
118         final PropertyChange prop = new PropertyChange();
119         prop.key = "repositoryPassword";
120         prop.remove = true;
121         final List<PropertyChange> props = new LinkedList<>();
122         props.add(prop);
123         setFailureProperties(props);
124         new BrmsHandler(INVALID_FILE);
125     }
126
127     @Test(expected = PolicyException.class)
128     public void brmsHandlerFailTest8() throws PolicyException {
129         final PropertyChange prop = new PropertyChange();
130         prop.key = "policyKeyID";
131         prop.remove = true;
132         final List<PropertyChange> props = new LinkedList<>();
133         props.add(prop);
134         setFailureProperties(props);
135         new BrmsHandler(INVALID_FILE);
136     }
137
138     @Test(expected = PolicyException.class)
139     public void brmsHandlerFailTest9() throws PolicyException {
140         PropertyChange prop = new PropertyChange();
141         prop.key = "sync";
142         prop.value = "true";
143         prop.remove = false;
144         final List<PropertyChange> props = new LinkedList<>();
145         props.add(prop);
146         prop = new PropertyChange();
147         prop.key = "brms.dependency.version";
148         prop.remove = true;
149         props.add(prop);
150         prop = new PropertyChange();
151         prop.key = "groupNames";
152         prop.remove = true;
153         props.add(prop);
154         setFailureProperties(props);
155         new BrmsHandler(INVALID_FILE);
156     }
157
158     @Test(expected = PolicyException.class)
159     public void brmsHandlerFailTest10() throws PolicyException {
160         final PropertyChange prop = new PropertyChange();
161         prop.key = "groupNames";
162         prop.value = "";
163         prop.remove = false;
164         final List<PropertyChange> props = new LinkedList<>();
165         props.add(prop);
166         setFailureProperties(props);
167         new BrmsHandler(INVALID_FILE);
168     }
169
170     @Test(expected = PolicyException.class)
171     public void brmsHandlerFailTest11() throws PolicyException {
172         final PropertyChange prop = new PropertyChange();
173         prop.key = "default.groupID";
174         prop.remove = true;
175         final List<PropertyChange> props = new LinkedList<>();
176         props.add(prop);
177         setFailureProperties(props);
178         new BrmsHandler(INVALID_FILE);
179     }
180
181     @Test(expected = PolicyException.class)
182     public void brmsHandlerFailTest12() throws PolicyException {
183         final PropertyChange prop = new PropertyChange();
184         prop.key = "default.artifactID";
185         prop.remove = true;
186         final List<PropertyChange> props = new LinkedList<>();
187         props.add(prop);
188         setFailureProperties(props);
189         new BrmsHandler(INVALID_FILE);
190     }
191
192     @Test(expected = PolicyException.class)
193     public void brmsHandlerFailTest13() throws PolicyException {
194         PropertyChange prop = new PropertyChange();
195         prop.key = "NOTIFICATION_TYPE";
196         prop.value = "dmaap";
197         prop.remove = false;
198         final List<PropertyChange> props = new LinkedList<>();
199         props.add(prop);
200         prop = new PropertyChange();
201         prop.key = "NOTIFICATION_SERVERS";
202         prop.remove = true;
203         props.add(prop);
204         setFailureProperties(props);
205         new BrmsHandler(INVALID_FILE);
206     }
207
208     @Test
209     public void brmsHandlerTest() throws PolicyException {
210         assertNotNull(new BrmsHandler(VALID_FILE));
211     }
212
213     private void setFailureProperties(final List<PropertyChange> properties) throws PolicyException {
214         final Properties validProp = new Properties();
215         try {
216             validProp.load(new FileInputStream(VALID_FILE));
217             for (final PropertyChange prop : properties) {
218                 if (prop.remove) {
219                     validProp.remove(prop.key);
220                 } else {
221                     validProp.setProperty(prop.key, prop.value);
222                 }
223             }
224             validProp.store(new FileOutputStream(INVALID_FILE), null);
225         } catch (final IOException e) {
226             throw new PolicyException(e);
227         }
228     }
229
230     class PropertyChange {
231         public String key = null;
232         public String value = null;
233         public Boolean remove = false;
234     }
235 }