-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OA-40: Add property to control starting of the module (#24)
- Loading branch information
1 parent
62ade3c
commit 548f1d9
Showing
8 changed files
with
232 additions
and
62 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
78 changes: 78 additions & 0 deletions
78
api/src/main/java/org/openmrs/module/oauth2login/PropertyUtils.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,78 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.oauth2login; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.openmrs.util.OpenmrsUtil; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Properties; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class PropertyUtils { | ||
|
||
protected static final Log LOG = LogFactory.getLog(PropertyUtils.class); | ||
|
||
private static final Pattern ENV_PATTERN = Pattern.compile("\\$\\{([^}]+)}"); | ||
|
||
public static Properties getProperties(Path path) throws IOException { | ||
Properties props = new Properties(); | ||
try (InputStream inputStream = Files.newInputStream(path)) { | ||
props.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); | ||
for (String key : props.stringPropertyNames()) { | ||
String value = props.getProperty(key); | ||
props.setProperty(key, resolveEnvVariables(value)); | ||
} | ||
} | ||
return props; | ||
} | ||
|
||
/** | ||
* Resolves environment variables in the given string. | ||
* <p> | ||
* This method searches for placeholders in the format ${ENV_VAR} within the input string and | ||
* replaces them with the corresponding environment variable values. It first checks system | ||
* properties and then environment variables for the value of each placeholder. | ||
* | ||
* @param value the input string containing placeholders for environment variables | ||
* @return the input string with all environment variables resolved | ||
*/ | ||
public static String resolveEnvVariables(String value) { | ||
Matcher matcher = ENV_PATTERN.matcher(value); | ||
StringBuffer buffer = new StringBuffer(); | ||
while (matcher.find()) { | ||
String envVar = matcher.group(1); | ||
String envValue = System.getProperty(envVar, System.getenv(envVar)); | ||
matcher.appendReplacement(buffer, Matcher.quoteReplacement(envValue != null ? envValue : matcher.group(0))); | ||
} | ||
matcher.appendTail(buffer); | ||
return buffer.toString(); | ||
} | ||
|
||
public static Path getOAuth2PropertiesPath() { | ||
Path path = Paths.get(OpenmrsUtil.getApplicationDataDirectory(), "oauth2.properties"); | ||
if (!path.toFile().exists()) { | ||
LOG.error("the property file doesn't exist " + path.toAbsolutePath()); | ||
} | ||
return path; | ||
} | ||
|
||
public static Properties getOAuth2Properties() throws IOException { | ||
return getProperties(getOAuth2PropertiesPath()); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
api/src/test/java/org/openmrs/module/oauth2login/OAuth2LoginActivatorTest.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,108 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.oauth2login; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.openmrs.module.ModuleException; | ||
import org.openmrs.module.ModuleFactory; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
import static org.mockito.Mockito.times; | ||
import static org.powermock.api.mockito.PowerMockito.mock; | ||
import static org.powermock.api.mockito.PowerMockito.verifyStatic; | ||
import static org.powermock.api.mockito.PowerMockito.when; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({ PropertyUtils.class, ModuleFactory.class }) | ||
public class OAuth2LoginActivatorTest { | ||
|
||
private OAuth2LoginActivator activator; | ||
|
||
@Before | ||
public void setup() { | ||
activator = new OAuth2LoginActivator(); | ||
PowerMockito.mockStatic(PropertyUtils.class); | ||
PowerMockito.mockStatic(ModuleFactory.class); | ||
} | ||
|
||
@Test | ||
public void willStart_shouldStopAndUnloadModuleIfOAuth2IsDisabled() throws IOException { | ||
// Setup | ||
Properties mockProperties = mock(Properties.class); | ||
when(mockProperties.getProperty(OAuth2LoginConstants.OAUTH2_ENABLED_PROPERTY, "true")).thenReturn("false"); | ||
when(PropertyUtils.getOAuth2Properties()).thenReturn(mockProperties); | ||
|
||
// Replay | ||
activator.willStart(); | ||
|
||
// Verify | ||
verifyStatic(); | ||
ModuleFactory.stopModule(ModuleFactory.getModuleById(OAuth2LoginConstants.MODULE_ARTIFACT_ID)); | ||
|
||
verifyStatic(); | ||
ModuleFactory.unloadModule(ModuleFactory.getModuleById(OAuth2LoginConstants.MODULE_ARTIFACT_ID)); | ||
} | ||
|
||
@Test | ||
public void willStart_shouldStartModuleIfOAuth2IsEnabled() throws IOException { | ||
// Setup | ||
Properties mockProperties = mock(Properties.class); | ||
when(mockProperties.getProperty(OAuth2LoginConstants.OAUTH2_ENABLED_PROPERTY, "true")).thenReturn("true"); | ||
when(PropertyUtils.getOAuth2Properties()).thenReturn(mockProperties); | ||
|
||
// Replay | ||
activator.willStart(); | ||
|
||
// Verify | ||
verifyStatic(times(0)); | ||
ModuleFactory.stopModule(ModuleFactory.getModuleById(OAuth2LoginConstants.MODULE_ARTIFACT_ID)); | ||
|
||
verifyStatic(times(0)); | ||
ModuleFactory.unloadModule(ModuleFactory.getModuleById(OAuth2LoginConstants.MODULE_ARTIFACT_ID)); | ||
} | ||
|
||
@Test | ||
public void willStart_shouldStopAndUnloadModuleIfModuleIsAlreadyStarted() throws IOException { | ||
// Setup | ||
Properties mockProperties = mock(Properties.class); | ||
when(mockProperties.getProperty(OAuth2LoginConstants.OAUTH2_ENABLED_PROPERTY, "true")).thenReturn("false"); | ||
when(PropertyUtils.getOAuth2Properties()).thenReturn(mockProperties); | ||
when(ModuleFactory.isModuleStarted(OAuth2LoginConstants.MODULE_ARTIFACT_ID)).thenReturn(true); | ||
|
||
// Replay | ||
activator.willStart(); | ||
|
||
// Verify | ||
verifyStatic(); | ||
ModuleFactory.isModuleStarted(OAuth2LoginConstants.MODULE_ARTIFACT_ID); | ||
|
||
verifyStatic(times(2)); | ||
ModuleFactory.stopModule(ModuleFactory.getModuleById(OAuth2LoginConstants.MODULE_ARTIFACT_ID)); | ||
|
||
verifyStatic(times(2)); | ||
ModuleFactory.unloadModule(ModuleFactory.getModuleById(OAuth2LoginConstants.MODULE_ARTIFACT_ID)); | ||
} | ||
|
||
@Test(expected = ModuleException.class) | ||
public void willStart_shouldThrowModuleExceptionIfPropertiesFileCannotBeLoaded() throws IOException { | ||
OAuth2LoginActivator activator = new OAuth2LoginActivator(); | ||
PropertyUtils propertyUtils = mock(PropertyUtils.class); | ||
when(PropertyUtils.getOAuth2Properties()).thenThrow(new IOException()); | ||
|
||
activator.willStart(); | ||
} | ||
} |
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
Oops, something went wrong.