Merge "Update docker tag convention"
[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.brmsInterface.BRMSHandler;
35
36 public class BRMSPushTest {
37     
38     private final String VALIDFILE = "src/test/resources/config.properties";
39     private final String INVALIDFILE = "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         PropertyChange prop = new PropertyChange();
54         prop.key = "defaultName";
55         prop.remove = true;
56         List<PropertyChange> props = new LinkedList<>();
57         props.add(prop);
58         setFailureProperties(props);
59         new BRMSHandler(INVALIDFILE);
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         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(INVALIDFILE);
75     }
76     
77     @Test (expected = PolicyException.class)
78     public void bRMSHandlerFailTest4() throws PolicyException {
79         PropertyChange prop = new PropertyChange();
80         prop.key = "repositoryURL";
81         prop.remove = true;
82         List<PropertyChange> props = new LinkedList<>();
83         props.add(prop);
84         setFailureProperties(props);
85         new BRMSHandler(INVALIDFILE);
86     }
87     
88     @Test (expected = PolicyException.class)
89     public void bRMSHandlerFailTest5() throws PolicyException {
90         PropertyChange prop = new PropertyChange();
91         prop.key = "repositoryName";
92         prop.remove = true;
93         List<PropertyChange> props = new LinkedList<>();
94         props.add(prop);
95         setFailureProperties(props);
96         new BRMSHandler(INVALIDFILE);
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 = "http://nexus:8081/nexus/content/repositories/releases, http://nexus:8081/nexus/content/repositories/releases";
104         prop.remove = false;
105         List<PropertyChange> props = new LinkedList<>();
106         props.add(prop);
107         prop = new PropertyChange();
108         prop.key = "repositoryUsername";
109         prop.remove = true;
110         props.add(prop);
111         setFailureProperties(props);
112         new BRMSHandler(INVALIDFILE);
113     }
114     
115     @Test (expected = PolicyException.class)
116     public void bRMSHandlerFailTest7() throws PolicyException {
117         PropertyChange prop = new PropertyChange();
118         prop.key = "repositoryPassword";
119         prop.remove = true;
120         List<PropertyChange> props = new LinkedList<>();
121         props.add(prop);
122         setFailureProperties(props);
123         new BRMSHandler(INVALIDFILE);
124     }
125     
126     @Test (expected = PolicyException.class)
127     public void bRMSHandlerFailTest8() throws PolicyException {
128         PropertyChange prop = new PropertyChange();
129         prop.key = "policyKeyID";
130         prop.remove = true;
131         List<PropertyChange> props = new LinkedList<>();
132         props.add(prop);
133         setFailureProperties(props);
134         new BRMSHandler(INVALIDFILE);
135     }
136     
137     @Test (expected = PolicyException.class)
138     public void bRMSHandlerFailTest9() throws PolicyException {
139         PropertyChange prop = new PropertyChange();
140         prop.key = "sync";
141         prop.value = "true";
142         prop.remove = false;
143         List<PropertyChange> props = new LinkedList<>();
144         props.add(prop);
145         prop = new PropertyChange();
146         prop.key = "brms.dependency.version";
147         prop.remove = true;
148         props.add(prop);
149         prop = new PropertyChange();
150         prop.key = "groupNames";
151         prop.remove = true;
152         props.add(prop);
153         setFailureProperties(props);
154         new BRMSHandler(INVALIDFILE);
155     }
156     
157     @Test (expected = PolicyException.class)
158     public void bRMSHandlerFailTest10() throws PolicyException {
159         PropertyChange prop = new PropertyChange();
160         prop.key = "groupNames";
161         prop.value = "";
162         prop.remove = false;
163         List<PropertyChange> props = new LinkedList<>();
164         props.add(prop);
165         setFailureProperties(props);
166         new BRMSHandler(INVALIDFILE);
167     }
168     
169     @Test (expected = PolicyException.class)
170     public void bRMSHandlerFailTest11() throws PolicyException {
171         PropertyChange prop = new PropertyChange();
172         prop.key = "default.groupID";
173         prop.remove = true;
174         List<PropertyChange> props = new LinkedList<>();
175         props.add(prop);
176         setFailureProperties(props);
177         new BRMSHandler(INVALIDFILE);
178     }
179     
180     @Test (expected = PolicyException.class)
181     public void bRMSHandlerFailTest12() throws PolicyException {
182         PropertyChange prop = new PropertyChange();
183         prop.key = "default.artifactID";
184         prop.remove = true;
185         List<PropertyChange> props = new LinkedList<>();
186         props.add(prop);
187         setFailureProperties(props);
188         new BRMSHandler(INVALIDFILE);
189     }
190     
191     @Test (expected = PolicyException.class)
192     public void bRMSHandlerFailTest13() throws PolicyException {
193         PropertyChange prop = new PropertyChange();
194         prop.key = "NOTIFICATION_TYPE";
195         prop.value = "dmaap";
196         prop.remove = false;
197         List<PropertyChange> props = new LinkedList<>();
198         props.add(prop);
199         prop = new PropertyChange();
200         prop.key = "NOTIFICATION_SERVERS";
201         prop.remove = true;
202         props.add(prop);
203         setFailureProperties(props);
204         new BRMSHandler(INVALIDFILE);
205     }
206     
207     @Test
208     public void BRMSHandlerTest() throws PolicyException {
209         assertNotNull(new BRMSHandler(VALIDFILE));
210     }
211     
212     private void setFailureProperties(List<PropertyChange> properties) throws PolicyException {
213         Properties validProp = new Properties();
214         try {
215             validProp.load(new FileInputStream(VALIDFILE));
216             for (PropertyChange prop: properties) {
217                 if(prop.remove) {
218                     validProp.remove(prop.key);
219                 }else {
220                     validProp.setProperty(prop.key, prop.value);
221                 }
222             }
223             validProp.store(new FileOutputStream(INVALIDFILE), null);
224         } catch (IOException e) {
225             throw new PolicyException(e);
226         }
227     }
228     
229     class PropertyChange {
230         public String key = null;
231         public String value = null;
232         public Boolean remove = false;
233     }
234 }