Skip to content

Commit

Permalink
test(mpconfig): Have DirConfigSource.findDir() covered by tests.
Browse files Browse the repository at this point in the history
Introducing some mocks to enable unit testing of findDir().
Needed to extend the base class constructors to be able to inject the mock.

Relates to payara#5006

P.S. Crossing fingers this runs on Windoze.
  • Loading branch information
poikilotherm committed Jan 27, 2021
1 parent dc78c29 commit f7c2046
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*/
package fish.payara.nucleus.microprofile.config.source;

import fish.payara.nucleus.microprofile.config.spi.ConfigProviderResolverImpl;
import org.eclipse.microprofile.config.spi.ConfigSource;

import java.io.File;
Expand Down Expand Up @@ -231,8 +232,9 @@ public DirConfigSource() {
}

// Used for testing only with explicit dependency injection
DirConfigSource(Path directory) {
super(true);
// Used for testing only with explicit dependency injection
DirConfigSource(Path directory, ConfigProviderResolverImpl configService) {
super(configService);
this.directory = directory;
}

Expand Down Expand Up @@ -274,7 +276,7 @@ public String getName() {
return "Directory";
}

private Path findDir() throws IOException {
Path findDir() throws IOException {
String path = configService.getMPConfig().getSecretDir();
List<Path> candidates = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ public PayaraConfigSource() {
configService = null;
}

/**
* Should only be used for test purposes
* @param configService Usually a mocked implementation
*/
PayaraConfigSource(ConfigProviderResolverImpl configService) {
this.domainConfiguration = null;
this.configService = configService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*/
package fish.payara.nucleus.microprofile.config.source;

import fish.payara.nucleus.microprofile.config.spi.ConfigProviderResolverImpl;
import fish.payara.nucleus.microprofile.config.spi.MicroprofileConfigConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -65,18 +67,21 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

public class DirConfigSourceTest {

private static Path testDirectory;
private static DirConfigSource source;
private static ScheduledExecutorService exec = Executors.newScheduledThreadPool(3);
private static ConfigProviderResolverImpl configService;

@BeforeClass
public static void setUp() throws IOException {
testDirectory = Files.createTempDirectory("microprofile-config-test-");
configService = mock(ConfigProviderResolverImpl.class);
// create & load
source = new DirConfigSource(testDirectory);
source = new DirConfigSource(testDirectory, configService);
}

@AfterClass
Expand All @@ -89,6 +94,33 @@ public static void tearDown() throws IOException, InterruptedException {
.forEach(File::delete);
}

@Test
public void testFindDir_AbsolutePath() throws IOException {
// given
MicroprofileConfigConfiguration config = mock(MicroprofileConfigConfiguration.class);
when(configService.getMPConfig()).thenReturn(config);
when(config.getSecretDir()).thenReturn(testDirectory.toString());
// when
Path sut = source.findDir();
// then
assertEquals(testDirectory, sut);
}

@Test
public void testFindDir_RelativePath() throws IOException {
// given
System.setProperty("com.sun.aas.instanceRoot", testDirectory.toString());
MicroprofileConfigConfiguration config = mock(MicroprofileConfigConfiguration.class);
when(configService.getMPConfig()).thenReturn(config);
when(config.getSecretDir()).thenReturn(".");

// when
Path sut = source.findDir();

// then
assertEquals(testDirectory, sut);
}

@Test
public void testIsAptDir() throws IOException {
// given
Expand Down

0 comments on commit f7c2046

Please sign in to comment.