1694cf178ca1c8b2a67daef47a504f4e8523d306
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * site-manager
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. 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.common.sitemanager.utils;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.BufferedWriter;
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.util.Properties;
30
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.TemporaryFolder;
34 import org.onap.policy.common.sitemanager.exception.MissingPropertyException;
35 import org.onap.policy.common.sitemanager.exception.PropertyFileProcessingException;
36
37 public class PersistenceUnitPropertiesProviderTest {
38     private static final String PROPERTIES_FILE_NAME = "file.properties";
39
40     private static final String COMMENTS = "";
41
42     @Rule
43     public TemporaryFolder temporaryFolder = new TemporaryFolder();
44
45     @Test(expected = PropertyFileProcessingException.class)
46     public void test_getProperties_null_throwException() {
47         final Printable printable = new PrintableImpl();
48
49         PersistenceUnitPropertiesProvider.getProperties(null, printable);
50     }
51
52     @Test(expected = PropertyFileProcessingException.class)
53     public void test_getProperties_emptyString_throwException() {
54         final Printable printable = new PrintableImpl();
55
56         PersistenceUnitPropertiesProvider.getProperties("", printable);
57     }
58
59     @Test(expected = MissingPropertyException.class)
60     public void test_getProperties_emptyPropertyFile_throwException() throws IOException {
61         final Printable printable = new PrintableImpl();
62         final File file = temporaryFolder.newFile(PROPERTIES_FILE_NAME);
63         creatPropertyFile(file, new Properties());
64         PersistenceUnitPropertiesProvider.getProperties(file.toString(), printable);
65     }
66
67     @Test(expected = MissingPropertyException.class)
68     public void test_getProperties_PropertyFileWithMissingProperties_throwException() throws IOException {
69         final Printable printable = new PrintableImpl();
70         final File file = temporaryFolder.newFile(PROPERTIES_FILE_NAME);
71         final Properties properties = new Properties();
72         properties.put(Constants.JDBC_DRIVER_PROPERTY_NAME, "org.h2");
73         creatPropertyFile(file, properties);
74         PersistenceUnitPropertiesProvider.getProperties(file.toString(), printable);
75     }
76
77     @Test
78     public void test_getProperties_PropertyFileValidProperties() throws IOException {
79         final Printable printable = new PrintableImpl();
80         final File file = temporaryFolder.newFile(PROPERTIES_FILE_NAME);
81
82         final Properties properties = new Properties();
83         properties.put(Constants.JDBC_DRIVER_PROPERTY_NAME, "Driver");
84         properties.put(Constants.JDBC_URL_PROPERTY_NAME, "inMem:Database");
85         properties.put(Constants.JDBC_USER_PROPERTY_NAME, "test");
86         properties.put(Constants.JDBC_PASSWORD_PROPERTY_NAME, "test");
87         creatPropertyFile(file, properties);
88         final Properties actualProperties = PersistenceUnitPropertiesProvider.getProperties(file.toString(), printable);
89
90         assertEquals(properties, actualProperties);
91     }
92
93     private void creatPropertyFile(final File file, final Properties properties) throws IOException {
94         try (final BufferedWriter bufferedWriter = Files.newBufferedWriter(file.toPath());) {
95             properties.store(bufferedWriter, COMMENTS);
96         }
97     }
98
99 }