Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improved error message and for properties file missing #10591

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ private static Properties loadProperties(@NonNull final String resourceName) {
// It is important to use the Thread's context class loader because the resource we want to load might
// be part of another module.
try (final var in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName)) {
if (in == null) {
throw new UncheckedIOException(
"Property file " + resourceName + " could not be found", new IOException());
}
final var props = new Properties();
props.load(in);
return props;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.node.config.sources;

import static org.junit.jupiter.api.Assertions.*;

import java.io.UncheckedIOException;
import java.lang.reflect.Method;
import java.util.Properties;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class PropertyConfigSourceTest {

private Properties properties;

@BeforeEach
void setUp() {
properties = new Properties();
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
}

@Test
void testExistentPropertyConfigSource() {
// given
int ordinal = 1; // used to determine priority of config source

// when
PropertyConfigSource propertyConfigSource = new PropertyConfigSource(properties, ordinal);

// then
assertEquals(2, propertyConfigSource.getPropertyNames().size());
assertEquals("value1", propertyConfigSource.getValue("key1"));
assertEquals("value2", propertyConfigSource.getValue("key2"));
assertEquals(ordinal, propertyConfigSource.getOrdinal());
}

@Test
void testPropertyConfigSourceWhenPropertyFileNotFound() {
// given
String nonExistentFile = "non-existent-file.properties";

// then
assertThrows(UncheckedIOException.class, () -> new PropertyConfigSource(nonExistentFile, 1));
}

@Test
void testLoadProperties() throws Exception {
// given
String existingFile = "bootstrap.properties";

// when
Method method = PropertyConfigSource.class.getDeclaredMethod("loadProperties", String.class);
method.setAccessible(true);

// then
// Test successful path
Properties properties = (Properties) method.invoke(null, existingFile);
assertNotNull(properties);
}

@AfterEach
void tearDown() {
properties = null;
}
}
Loading