2 * ============LICENSE_START=======================================================
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
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.common.sitemanager.utils;
23 import static org.junit.Assert.assertEquals;
25 import java.io.BufferedWriter;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.util.Properties;
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;
37 public class PersistenceUnitPropertiesProviderTest {
38 private static final String PROPERTIES_FILE_NAME = "file.properties";
40 private static final String COMMENTS = "";
43 public TemporaryFolder temporaryFolder = new TemporaryFolder();
45 @Test(expected = PropertyFileProcessingException.class)
46 public void test_getProperties_null_throwException() {
47 final Printable printable = new PrintableImpl();
49 PersistenceUnitPropertiesProvider.getProperties(null, printable);
52 @Test(expected = PropertyFileProcessingException.class)
53 public void test_getProperties_emptyString_throwException() {
54 final Printable printable = new PrintableImpl();
56 PersistenceUnitPropertiesProvider.getProperties("", printable);
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);
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);
78 public void test_getProperties_PropertyFileValidProperties() throws IOException {
79 final Printable printable = new PrintableImpl();
80 final File file = temporaryFolder.newFile(PROPERTIES_FILE_NAME);
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);
90 assertEquals(properties, actualProperties);
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);