Eliminate error messages about BACKUPMONITORENTITY
[policy/engine.git] / BRMSGateway / src / test / java / org / onap / policy / brms / 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.policy.brms;
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 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.Persistence;
33 import org.eclipse.persistence.config.PersistenceUnitProperties;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.api.PolicyException;
38 import org.onap.policy.brms.api.BrmsHandler;
39 import org.onap.policy.brms.api.BrmsPush;
40
41 public class BrmsPushTest {
42
43     private static final String VALID_FILE = "src/test/resources/config.properties";
44     private static final String INVALID_FILE = "src/test/resources/failure.properties";
45
46     private static EntityManagerFactory emf;
47
48     /**
49      * Creates the test DB and keeps it open until all tests complete.
50      * 
51      * @throws Exception if an error occurs
52      */
53     @BeforeClass
54     public static void setUpBeforeClass() throws Exception {
55         Properties props = new Properties();
56         try (FileInputStream inp = new FileInputStream(VALID_FILE)) {
57             props.load(inp);
58         }
59         props.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML,
60                         props.getProperty(BrmsPush.BRMSPERSISTENCE));
61
62         emf = Persistence.createEntityManagerFactory("BRMSGW", props);
63     }
64
65     /**
66      * Closes the test DB.
67      */
68     @AfterClass
69     public static void tearDownAfterClass() {
70         emf.close();
71     }
72
73     @Test(expected = PolicyException.class)
74     public void brmsHandlerFailTest() throws PolicyException {
75         new BrmsHandler(null);
76     }
77
78     @Test(expected = PolicyException.class)
79     public void brmsHandlerFailTest1() throws PolicyException {
80         new BrmsHandler("src/test/resources/filenotexists.txt");
81     }
82
83     @Test(expected = PolicyException.class)
84     public void brmsHandlerFailTest2() throws PolicyException {
85         final PropertyChange prop = new PropertyChange();
86         prop.key = "defaultName";
87         prop.remove = true;
88         final List<PropertyChange> props = new LinkedList<>();
89         props.add(prop);
90         setFailureProperties(props);
91         new BrmsHandler(INVALID_FILE);
92     }
93
94     @Test(expected = PolicyException.class)
95     public void brmsHandlerFailTest3() throws PolicyException {
96         PropertyChange prop = new PropertyChange();
97         prop.key = "repositoryID";
98         prop.remove = true;
99         final List<PropertyChange> props = new LinkedList<>();
100         props.add(prop);
101         prop = new PropertyChange();
102         prop.key = "RESOURCE_NAME";
103         prop.remove = true;
104         props.add(prop);
105         setFailureProperties(props);
106         new BrmsHandler(INVALID_FILE);
107     }
108
109     @Test(expected = PolicyException.class)
110     public void brmsHandlerFailTest4() throws PolicyException {
111         final PropertyChange prop = new PropertyChange();
112         prop.key = "repositoryURL";
113         prop.remove = true;
114         final List<PropertyChange> props = new LinkedList<>();
115         props.add(prop);
116         setFailureProperties(props);
117         new BrmsHandler(INVALID_FILE);
118     }
119
120     @Test(expected = PolicyException.class)
121     public void brmsHandlerFailTest5() throws PolicyException {
122         final PropertyChange prop = new PropertyChange();
123         prop.key = "repositoryName";
124         prop.remove = true;
125         final List<PropertyChange> props = new LinkedList<>();
126         props.add(prop);
127         setFailureProperties(props);
128         new BrmsHandler(INVALID_FILE);
129     }
130
131     @Test(expected = PolicyException.class)
132     public void brmsHandlerFailTest6() throws PolicyException {
133         PropertyChange prop = new PropertyChange();
134         prop.key = "repositoryURL";
135         prop.value = "http://nexus:8081/nexus/content/repositories/releases,"
136                         + "http://nexus:8081/nexus/content/repositories/releases";
137         prop.remove = false;
138         final List<PropertyChange> props = new LinkedList<>();
139         props.add(prop);
140         prop = new PropertyChange();
141         prop.key = "repositoryUsername";
142         prop.remove = true;
143         props.add(prop);
144         setFailureProperties(props);
145         new BrmsHandler(INVALID_FILE);
146     }
147
148     @Test(expected = PolicyException.class)
149     public void brmsHandlerFailTest7() throws PolicyException {
150         final PropertyChange prop = new PropertyChange();
151         prop.key = "repositoryPassword";
152         prop.remove = true;
153         final List<PropertyChange> props = new LinkedList<>();
154         props.add(prop);
155         setFailureProperties(props);
156         new BrmsHandler(INVALID_FILE);
157     }
158
159     @Test(expected = PolicyException.class)
160     public void brmsHandlerFailTest8() throws PolicyException {
161         final PropertyChange prop = new PropertyChange();
162         prop.key = "policyKeyID";
163         prop.remove = true;
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 brmsHandlerFailTest9() throws PolicyException {
172         PropertyChange prop = new PropertyChange();
173         prop.key = "sync";
174         prop.value = "true";
175         prop.remove = false;
176         final List<PropertyChange> props = new LinkedList<>();
177         props.add(prop);
178         prop = new PropertyChange();
179         prop.key = "brms.dependency.version";
180         prop.remove = true;
181         props.add(prop);
182         prop = new PropertyChange();
183         prop.key = "groupNames";
184         prop.remove = true;
185         props.add(prop);
186         setFailureProperties(props);
187         new BrmsHandler(INVALID_FILE);
188     }
189
190     @Test(expected = PolicyException.class)
191     public void brmsHandlerFailTest10() throws PolicyException {
192         final PropertyChange prop = new PropertyChange();
193         prop.key = "groupNames";
194         prop.value = "";
195         prop.remove = false;
196         final List<PropertyChange> props = new LinkedList<>();
197         props.add(prop);
198         setFailureProperties(props);
199         new BrmsHandler(INVALID_FILE);
200     }
201
202     @Test(expected = PolicyException.class)
203     public void brmsHandlerFailTest11() throws PolicyException {
204         final PropertyChange prop = new PropertyChange();
205         prop.key = "default.groupID";
206         prop.remove = true;
207         final List<PropertyChange> props = new LinkedList<>();
208         props.add(prop);
209         setFailureProperties(props);
210         new BrmsHandler(INVALID_FILE);
211     }
212
213     @Test(expected = PolicyException.class)
214     public void brmsHandlerFailTest12() throws PolicyException {
215         final PropertyChange prop = new PropertyChange();
216         prop.key = "default.artifactID";
217         prop.remove = true;
218         final List<PropertyChange> props = new LinkedList<>();
219         props.add(prop);
220         setFailureProperties(props);
221         new BrmsHandler(INVALID_FILE);
222     }
223
224     @Test(expected = PolicyException.class)
225     public void brmsHandlerFailTest13() throws PolicyException {
226         PropertyChange prop = new PropertyChange();
227         prop.key = "NOTIFICATION_TYPE";
228         prop.value = "dmaap";
229         prop.remove = false;
230         final List<PropertyChange> props = new LinkedList<>();
231         props.add(prop);
232         prop = new PropertyChange();
233         prop.key = "NOTIFICATION_SERVERS";
234         prop.remove = true;
235         props.add(prop);
236         setFailureProperties(props);
237         new BrmsHandler(INVALID_FILE);
238     }
239
240     @Test
241     public void brmsHandlerTest() throws PolicyException {
242         assertNotNull(new BrmsHandler(VALID_FILE));
243     }
244
245     private void setFailureProperties(final List<PropertyChange> properties) throws PolicyException {
246         final Properties validProp = new Properties();
247         try {
248             validProp.load(new FileInputStream(VALID_FILE));
249             for (final PropertyChange prop : properties) {
250                 if (prop.remove) {
251                     validProp.remove(prop.key);
252                 } else {
253                     validProp.setProperty(prop.key, prop.value);
254                 }
255             }
256             validProp.store(new FileOutputStream(INVALID_FILE), null);
257         } catch (final IOException e) {
258             throw new PolicyException(e);
259         }
260     }
261
262     class PropertyChange {
263         public String key = null;
264         public String value = null;
265         public Boolean remove = false;
266     }
267 }