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;
24 import java.io.BufferedWriter;
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;
35 public class PersistenceUnitPropertiesProviderTest {
36 private static final String PROPERTIES_FILE_NAME = "file.properties";
38 private static final String COMMENTS = "";
41 public TemporaryFolder temporaryFolder = new TemporaryFolder();
43 @Test(expected = PropertyFileProcessingException.class)
44 public void test_getProperties_null_throwException() {
45 final Printable printable = new PrintableImpl();
47 PersistenceUnitPropertiesProvider.getProperties(null, printable);
50 @Test(expected = PropertyFileProcessingException.class)
51 public void test_getProperties_emptyString_throwException() {
52 final Printable printable = new PrintableImpl();
54 PersistenceUnitPropertiesProvider.getProperties("", printable);
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);
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);
76 public void test_getProperties_PropertyFileValidProperties_throwException() throws IOException {
77 final Printable printable = new PrintableImpl();
78 final File file = temporaryFolder.newFile(PROPERTIES_FILE_NAME);
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);
88 assertEquals(properties, actualProperties);
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);