Search in sources :

Example 96 with GlobalProperty

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, ""));
}
Also used : Locale(java.util.Locale) GlobalProperty(org.openmrs.GlobalProperty) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 97 with GlobalProperty

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());
}
Also used : Locale(java.util.Locale) GlobalProperty(org.openmrs.GlobalProperty) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 98 with GlobalProperty

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) InputStream(java.io.InputStream) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GlobalProperty(org.openmrs.GlobalProperty) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) GZIPFilter(org.openmrs.web.filter.GZIPFilter) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedReader(java.io.BufferedReader) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) BaseWebContextSensitiveTest(org.openmrs.web.test.BaseWebContextSensitiveTest)

Example 99 with GlobalProperty

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));
}
Also used : HashMap(java.util.HashMap) DatabaseUpdateException(org.openmrs.util.DatabaseUpdateException) InputRequiredException(org.openmrs.util.InputRequiredException) APIException(org.openmrs.api.APIException) ModuleMustStartException(org.openmrs.module.ModuleMustStartException) MessageException(org.openmrs.notification.MessageException) GlobalProperty(org.openmrs.GlobalProperty) Role(org.openmrs.Role) AdministrationService(org.openmrs.api.AdministrationService) Privilege(org.openmrs.Privilege) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 100 with GlobalProperty

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
}
Also used : GlobalProperty(org.openmrs.GlobalProperty) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

GlobalProperty (org.openmrs.GlobalProperty)110 Test (org.junit.Test)80 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)77 Locale (java.util.Locale)14 OrderUtilTest (org.openmrs.order.OrderUtilTest)14 Encounter (org.openmrs.Encounter)12 ArrayList (java.util.ArrayList)11 User (org.openmrs.User)11 Patient (org.openmrs.Patient)10 AdministrationService (org.openmrs.api.AdministrationService)10 BindException (org.springframework.validation.BindException)10 DrugOrder (org.openmrs.DrugOrder)9 Errors (org.springframework.validation.Errors)9 CareSetting (org.openmrs.CareSetting)7 OrderType (org.openmrs.OrderType)7 Date (java.util.Date)6 EncounterType (org.openmrs.EncounterType)5 APIException (org.openmrs.api.APIException)5 MutableMessageSource (org.openmrs.messagesource.MutableMessageSource)4 MutableResourceBundleMessageSource (org.openmrs.messagesource.impl.MutableResourceBundleMessageSource)4