use of org.openmrs.GlobalProperty in project openmrs-core by openmrs.
the class LocaleUtilityTest method getLocalesInOrder_shouldReturnASetOfLocalesWithNoDuplicates.
/**
* @see LocaleUtility#getLocalesInOrder()
*/
@Test
public void getLocalesInOrder_shouldReturnASetOfLocalesWithNoDuplicates() {
GlobalProperty gp = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "lu_UG, lu, sw_KE, en_US, en, en, en_GB, sw_KE", "Test Allowed list of locales");
Context.getAdministrationService().saveGlobalProperty(gp);
GlobalProperty defaultLocale = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "lu", "Test Allowed list of locales");
Context.getAdministrationService().saveGlobalProperty(defaultLocale);
Locale lu_UG = new Locale("lu", "UG");
Context.setLocale(lu_UG);
// note that unique list of locales should be lu_UG, lu, sw_KE, en_US, en
Assert.assertEquals(6, LocaleUtility.getLocalesInOrder().size());
Context.getAdministrationService().saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, ""));
}
use of org.openmrs.GlobalProperty in project openmrs-core by openmrs.
the class LocaleUtilityTest method getLocalesInOrder_shouldReturnASetOfLocalesWithAPredictableOrder.
/**
* @see LocaleUtility#getLocalesInOrder()
*/
@Test
public void getLocalesInOrder_shouldReturnASetOfLocalesWithAPredictableOrder() {
GlobalProperty gp = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "lu, sw_KE, en_US, en_GB", "Test Allowed list of locales");
Context.getAdministrationService().saveGlobalProperty(gp);
Locale lu_UG = new Locale("lu", "UG");
Context.setLocale(lu_UG);
Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder();
Iterator<Locale> it = localesInOrder.iterator();
Assert.assertEquals(new Locale("lu", "UG"), it.next());
Assert.assertEquals(LocaleUtility.getDefaultLocale(), it.next());
Assert.assertEquals(new Locale("lu"), it.next());
Assert.assertEquals(new Locale("sw", "KE"), it.next());
Assert.assertEquals(new Locale("en", "US"), it.next());
Assert.assertEquals(new Locale("en"), it.next());
}
use of org.openmrs.GlobalProperty in project openmrs-core by openmrs.
the class GZIPFilterTest method zipRequestWrapperTest_shouldReturnTrueIfUnzippedContentReadFromWrapperIsTheSameAsContentBeforeZipping.
/**
* @see org.openmrs.web.filter.GZIPFilter#doFilterInternal(HttpServletRequest,HttpServletResponse, javax.servlet.FilterChain)
*/
@Test
public void zipRequestWrapperTest_shouldReturnTrueIfUnzippedContentReadFromWrapperIsTheSameAsContentBeforeZipping() throws Exception {
GlobalProperty property = new GlobalProperty("gzip.acceptCompressedRequestsForPaths", ".*");
Context.getAdministrationService().saveGlobalProperty(property);
MockHttpServletRequest req = new MockHttpServletRequest();
req.setContextPath("http://gzipservletpath");
req.addHeader("Content-encoding", "gzip");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
GZIPOutputStream gzOutput = new GZIPOutputStream(stream);
PrintWriter pwriter = new PrintWriter(gzOutput);
pwriter.write("message string");
pwriter.flush();
gzOutput.finish();
req.setContent(stream.toByteArray());
MockHttpServletResponse resp = new MockHttpServletResponse();
FilterChain fil = mock(FilterChain.class);
GZIPFilter gzipFilter = new GZIPFilter();
gzipFilter.doFilterInternal(req, resp, fil);
final ArgumentCaptor<HttpServletRequest> argumentCaptor = ArgumentCaptor.forClass(HttpServletRequest.class);
Mockito.verify(fil).doFilter(argumentCaptor.capture(), Mockito.any(HttpServletResponse.class));
HttpServletRequest requestArgument = argumentCaptor.getValue();
try {
InputStream iStream = requestArgument.getInputStream();
InputStreamReader iReader = new InputStreamReader(iStream);
BufferedReader bufReader = new BufferedReader(iReader);
String outputMessage = bufReader.readLine();
Assert.assertThat(outputMessage, is("message string"));
} catch (IOException e) {
throw new RuntimeException();
}
}
use of org.openmrs.GlobalProperty in project openmrs-core by openmrs.
the class Context method checkCoreDataset.
/**
* Runs through the core data (e.g. privileges, roles, and global properties) and adds them if
* necessary.
*/
public static void checkCoreDataset() {
// setting core roles
try {
Context.addProxyPrivilege(PrivilegeConstants.MANAGE_ROLES);
Set<String> currentRoleNames = new HashSet<>();
for (Role role : Context.getUserService().getAllRoles()) {
currentRoleNames.add(role.getRole().toUpperCase());
}
Map<String, String> map = OpenmrsUtil.getCoreRoles();
for (Map.Entry<String, String> entry : map.entrySet()) {
String roleName = entry.getKey();
if (!currentRoleNames.contains(roleName.toUpperCase())) {
Role role = new Role();
role.setRole(roleName);
role.setDescription(entry.getValue());
Context.getUserService().saveRole(role);
}
}
} catch (Exception e) {
log.error("Error while setting core roles for openmrs system", e);
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_ROLES);
}
// setting core privileges
try {
Context.addProxyPrivilege(PrivilegeConstants.MANAGE_PRIVILEGES);
Set<String> currentPrivilegeNames = new HashSet<>();
for (Privilege privilege : Context.getUserService().getAllPrivileges()) {
currentPrivilegeNames.add(privilege.getPrivilege().toUpperCase());
}
Map<String, String> map = OpenmrsUtil.getCorePrivileges();
for (Map.Entry<String, String> entry : map.entrySet()) {
String privilegeName = entry.getKey();
if (!currentPrivilegeNames.contains(privilegeName.toUpperCase())) {
Privilege p = new Privilege();
p.setPrivilege(privilegeName);
p.setDescription(entry.getValue());
Context.getUserService().savePrivilege(p);
}
}
} catch (Exception e) {
log.error("Error while setting core privileges", e);
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_PRIVILEGES);
}
// setting core global properties
try {
Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
Set<String> currentPropNames = new HashSet<>();
Map<String, GlobalProperty> propsMissingDescription = new HashMap<>();
Map<String, GlobalProperty> propsMissingDatatype = new HashMap<>();
for (GlobalProperty prop : Context.getAdministrationService().getAllGlobalProperties()) {
currentPropNames.add(prop.getProperty().toUpperCase());
if (prop.getDescription() == null) {
propsMissingDescription.put(prop.getProperty().toUpperCase(), prop);
}
if (prop.getDatatypeClassname() == null) {
propsMissingDatatype.put(prop.getProperty().toUpperCase(), prop);
}
}
for (GlobalProperty coreProp : OpenmrsConstants.CORE_GLOBAL_PROPERTIES()) {
String corePropName = coreProp.getProperty().toUpperCase();
// if the prop doesn't exist, save it
if (!currentPropNames.contains(corePropName)) {
Context.getAdministrationService().saveGlobalProperty(coreProp);
// add to list in case
currentPropNames.add(corePropName);
// of duplicates
} else {
// if the prop is missing its description, update it
GlobalProperty propToUpdate = propsMissingDescription.get(corePropName);
if (propToUpdate != null) {
propToUpdate.setDescription(coreProp.getDescription());
Context.getAdministrationService().saveGlobalProperty(propToUpdate);
}
// set missing datatypes
propToUpdate = propsMissingDatatype.get(corePropName);
if (propToUpdate != null && coreProp.getDatatypeClassname() != null) {
propToUpdate.setDatatypeClassname(coreProp.getDatatypeClassname());
propToUpdate.setDatatypeConfig(coreProp.getDatatypeConfig());
propToUpdate.setPreferredHandlerClassname(coreProp.getPreferredHandlerClassname());
propToUpdate.setHandlerConfig(coreProp.getHandlerConfig());
Context.getAdministrationService().saveGlobalProperty(propToUpdate);
}
}
}
} catch (Exception e) {
log.error("Error while setting core global properties", e);
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
}
// setting default validation rule
AdministrationService as = Context.getAdministrationService();
Boolean disableValidation = Boolean.valueOf(as.getGlobalProperty(OpenmrsConstants.GP_DISABLE_VALIDATION, "false"));
ValidateUtil.setDisableValidation(disableValidation);
PersonName.setFormat(Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LAYOUT_NAME_FORMAT));
Allergen.setOtherNonCodedConceptUuid(Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_ALLERGEN_OTHER_NON_CODED_UUID));
}
use of org.openmrs.GlobalProperty in project openmrs-core by openmrs.
the class ModuleUtilTest method checkMandatoryModulesStarted_shouldThrowModuleExceptionIfAMandatoryModuleIsNotStarted.
/**
* @see org.openmrs.module.ModuleUtil#checkMandatoryModulesStarted()
*/
@Test(expected = MandatoryModuleException.class)
public void checkMandatoryModulesStarted_shouldThrowModuleExceptionIfAMandatoryModuleIsNotStarted() {
// given
assertThat(ModuleFactory.getStartedModules(), empty());
GlobalProperty gp1 = new GlobalProperty("module1.mandatory", "true");
Context.getAdministrationService().saveGlobalProperty(gp1);
// when
ModuleUtil.checkMandatoryModulesStarted();
// then exception
}
Aggregations