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