-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed "use-config" when Kafkactl config is overrided by KAFKACTL_CONF…
…IG (#27) * Fixed use config when Kafkactl config is override by var env * Update get config directory * Update get config directory * Update get config directory * Update get config directory * Update get config directory * Update get config directory * Update unit tests * Update unit tests * Update unit tests * Update unit tests * Update unit tests set property * Update readme
- Loading branch information
Loïc GREFFIER
authored
Mar 9, 2023
1 parent
a583a64
commit 77eb403
Showing
12 changed files
with
233 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/michelin/kafkactl/services/SystemService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.michelin.kafkactl.services; | ||
|
||
public class SystemService { | ||
private SystemService() { } | ||
|
||
/** | ||
* Get a system environment variable by name | ||
* @param name The var name | ||
* @return The system environment variable | ||
*/ | ||
public static String getEnv(String name) { | ||
return System.getenv(name); | ||
} | ||
|
||
/** | ||
* Get a system property by name | ||
* @param name The property name | ||
* @return The system property name | ||
*/ | ||
public static String getProperty(String name) { | ||
return System.getProperty(name); | ||
} | ||
|
||
/** | ||
* Set a system property | ||
* @param key The name | ||
* @param value The value | ||
*/ | ||
public static void setProperty(String key, String value) { | ||
System.setProperty(key, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/test/java/com/michelin/kafkactl/KafkactlConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.michelin.kafkactl; | ||
|
||
import com.michelin.kafkactl.services.SystemService; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
import static com.michelin.kafkactl.KafkactlConfig.KAFKACTL_CONFIG; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
class KafkactlConfigTest { | ||
private final KafkactlConfig kafkactlConfig = new KafkactlConfig(); | ||
|
||
@Test | ||
void shouldGetConfigDirectoryFromUserHome() { | ||
try (MockedStatic<SystemService> mocked = mockStatic(SystemService.class)) { | ||
mocked.when(() -> SystemService.getEnv(KAFKACTL_CONFIG)) | ||
.thenReturn(""); | ||
mocked.when(() -> SystemService.getProperty(any())) | ||
.thenAnswer(answer -> System.getProperty(answer.getArgument(0))); | ||
|
||
String actual = kafkactlConfig.getConfigDirectory(); | ||
assertEquals(System.getProperty("user.home") + "/.kafkactl", actual); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldGetConfigDirectoryFromKafkactlConfig() { | ||
try (MockedStatic<SystemService> mocked = mockStatic(SystemService.class)) { | ||
mocked.when(() -> SystemService.getEnv(KAFKACTL_CONFIG)) | ||
.thenReturn("src/main/resources/config.yml"); | ||
|
||
String actual = kafkactlConfig.getConfigDirectory(); | ||
assertEquals("src" + System.getProperty("file.separator") + "main" + System.getProperty("file.separator") + "resources", | ||
actual); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldGetConfigDirectoryFromKafkactlConfigNoParent() { | ||
try (MockedStatic<SystemService> mocked = mockStatic(SystemService.class)) { | ||
mocked.when(() -> SystemService.getEnv(KAFKACTL_CONFIG)) | ||
.thenReturn("config.yml"); | ||
|
||
String actual = kafkactlConfig.getConfigDirectory(); | ||
assertEquals(".", actual); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldGetConfigDirectoryFromKafkactlConfigParentIsCurrent() { | ||
try (MockedStatic<SystemService> mocked = mockStatic(SystemService.class)) { | ||
mocked.when(() -> SystemService.getEnv(KAFKACTL_CONFIG)) | ||
.thenReturn("./config.yml"); | ||
|
||
String actual = kafkactlConfig.getConfigDirectory(); | ||
assertEquals(".", actual); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldGetConfigPathFromUserHome() { | ||
try (MockedStatic<SystemService> mocked = mockStatic(SystemService.class)) { | ||
mocked.when(() -> SystemService.getEnv(KAFKACTL_CONFIG)) | ||
.thenReturn(""); | ||
mocked.when(() -> SystemService.getProperty(any())) | ||
.thenAnswer(answer -> System.getProperty(answer.getArgument(0))); | ||
|
||
String actual = kafkactlConfig.getConfigPath(); | ||
assertEquals(System.getProperty("user.home") + "/.kafkactl/config.yml", actual); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldGetConfigPathFromKafkactlConfig() { | ||
try (MockedStatic<SystemService> mocked = mockStatic(SystemService.class)) { | ||
mocked.when(() -> SystemService.getEnv(KAFKACTL_CONFIG)) | ||
.thenReturn("src/main/resources/config.yml"); | ||
|
||
String actual = kafkactlConfig.getConfigPath(); | ||
assertEquals("src/main/resources/config.yml", actual); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldGetConfigPathFromKafkactlConfigNoParent() { | ||
try (MockedStatic<SystemService> mocked = mockStatic(SystemService.class)) { | ||
mocked.when(() -> SystemService.getEnv(KAFKACTL_CONFIG)) | ||
.thenReturn("config.yml"); | ||
|
||
String actual = kafkactlConfig.getConfigPath(); | ||
assertEquals("config.yml", actual); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldGetConfigPathFromKafkactlConfigParentIsCurrent() { | ||
try (MockedStatic<SystemService> mocked = mockStatic(SystemService.class)) { | ||
mocked.when(() -> SystemService.getEnv(KAFKACTL_CONFIG)) | ||
.thenReturn("./config.yml"); | ||
|
||
String actual = kafkactlConfig.getConfigPath(); | ||
assertEquals("./config.yml", actual); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.