2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2020 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.brms;
23 import static org.junit.Assert.assertNotNull;
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;
41 public class BrmsPushTest {
43 private static final String VALID_FILE = "src/test/resources/config.properties";
44 private static final String INVALID_FILE = "src/test/resources/failure.properties";
46 private static EntityManagerFactory emf;
49 * Creates the test DB and keeps it open until all tests complete.
51 * @throws Exception if an error occurs
54 public static void setUpBeforeClass() throws Exception {
55 Properties props = new Properties();
56 try (FileInputStream inp = new FileInputStream(VALID_FILE)) {
59 props.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML,
60 props.getProperty(BrmsPush.BRMSPERSISTENCE));
62 emf = Persistence.createEntityManagerFactory("BRMSGW", props);
69 public static void tearDownAfterClass() {
73 @Test(expected = PolicyException.class)
74 public void brmsHandlerFailTest() throws PolicyException {
75 new BrmsHandler(null);
78 @Test(expected = PolicyException.class)
79 public void brmsHandlerFailTest1() throws PolicyException {
80 new BrmsHandler("src/test/resources/filenotexists.txt");
83 @Test(expected = PolicyException.class)
84 public void testBrmsHandlerFail2() throws PolicyException {
85 final PropertyChange prop = new PropertyChange();
86 prop.key = "defaultName";
88 final List<PropertyChange> props = new LinkedList<>();
90 setFailureProperties(props);
91 new BrmsHandler(INVALID_FILE);
94 @Test(expected = PolicyException.class)
95 public void testBrmsHandlerFail3() throws PolicyException {
96 PropertyChange prop = new PropertyChange();
97 prop.key = "repositoryID";
99 final List<PropertyChange> props = new LinkedList<>();
101 prop = new PropertyChange();
102 prop.key = "RESOURCE_NAME";
105 setFailureProperties(props);
106 new BrmsHandler(INVALID_FILE);
109 @Test(expected = PolicyException.class)
110 public void testBrmsHandlerFail4() throws PolicyException {
111 final PropertyChange prop = new PropertyChange();
112 prop.key = "repositoryURL";
114 final List<PropertyChange> props = new LinkedList<>();
116 setFailureProperties(props);
117 new BrmsHandler(INVALID_FILE);
120 @Test(expected = PolicyException.class)
121 public void testBrmsHandlerFail5() throws PolicyException {
122 final PropertyChange prop = new PropertyChange();
123 prop.key = "repositoryName";
125 final List<PropertyChange> props = new LinkedList<>();
127 setFailureProperties(props);
128 new BrmsHandler(INVALID_FILE);
131 @Test(expected = PolicyException.class)
132 public void testBrmsHandlerFail6() 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";
138 final List<PropertyChange> props = new LinkedList<>();
140 prop = new PropertyChange();
141 prop.key = "repositoryUsername";
144 setFailureProperties(props);
145 new BrmsHandler(INVALID_FILE);
148 @Test(expected = PolicyException.class)
149 public void testBrmsHandlerFail7() throws PolicyException {
150 final PropertyChange prop = new PropertyChange();
151 prop.key = "repositoryPassword";
153 final List<PropertyChange> props = new LinkedList<>();
155 setFailureProperties(props);
156 new BrmsHandler(INVALID_FILE);
159 @Test(expected = PolicyException.class)
160 public void testBrmsHandlerFail8() throws PolicyException {
161 final PropertyChange prop = new PropertyChange();
162 prop.key = "policyKeyID";
164 final List<PropertyChange> props = new LinkedList<>();
166 setFailureProperties(props);
167 new BrmsHandler(INVALID_FILE);
170 @Test(expected = PolicyException.class)
171 public void testBrmsHandlerFail9() throws PolicyException {
172 PropertyChange prop = new PropertyChange();
176 final List<PropertyChange> props = new LinkedList<>();
178 prop = new PropertyChange();
179 prop.key = "brms.dependency.version";
182 prop = new PropertyChange();
183 prop.key = "groupNames";
186 setFailureProperties(props);
187 new BrmsHandler(INVALID_FILE);
190 @Test(expected = PolicyException.class)
191 public void testBrmsHandlerFail10() throws PolicyException {
192 final PropertyChange prop = new PropertyChange();
193 prop.key = "groupNames";
196 final List<PropertyChange> props = new LinkedList<>();
198 setFailureProperties(props);
199 new BrmsHandler(INVALID_FILE);
202 @Test(expected = PolicyException.class)
203 public void testBrmsHandlerFail11() throws PolicyException {
204 final PropertyChange prop = new PropertyChange();
205 prop.key = "default.groupID";
207 final List<PropertyChange> props = new LinkedList<>();
209 setFailureProperties(props);
210 new BrmsHandler(INVALID_FILE);
213 @Test(expected = PolicyException.class)
214 public void testBrmsHandlerFail12() throws PolicyException {
215 final PropertyChange prop = new PropertyChange();
216 prop.key = "default.artifactID";
218 final List<PropertyChange> props = new LinkedList<>();
220 setFailureProperties(props);
221 new BrmsHandler(INVALID_FILE);
224 @Test(expected = PolicyException.class)
225 public void testBrmsHandlerFail13() throws PolicyException {
226 PropertyChange prop = new PropertyChange();
227 prop.key = "NOTIFICATION_TYPE";
228 prop.value = "dmaap";
230 final List<PropertyChange> props = new LinkedList<>();
232 prop = new PropertyChange();
233 prop.key = "NOTIFICATION_SERVERS";
236 setFailureProperties(props);
237 new BrmsHandler(INVALID_FILE);
241 public void brmsHandlerTest() throws PolicyException {
242 assertNotNull(new BrmsHandler(VALID_FILE));
245 private void setFailureProperties(final List<PropertyChange> properties) throws PolicyException {
246 final Properties validProp = new Properties();
248 validProp.load(new FileInputStream(VALID_FILE));
249 for (final PropertyChange prop : properties) {
251 validProp.remove(prop.key);
253 validProp.setProperty(prop.key, prop.value);
256 validProp.store(new FileOutputStream(INVALID_FILE), null);
257 } catch (final IOException e) {
258 throw new PolicyException(e);
262 class PropertyChange {
263 public String key = null;
264 public String value = null;
265 public Boolean remove = false;