Skip to content

Commit

Permalink
Use adminService from test setup in AdminServiceTest
Browse files Browse the repository at this point in the history
we prepare the adminService in the @before method for every test so
there is no need to get it from the Context again in each test
  • Loading branch information
teleivo committed Apr 9, 2018
1 parent baa0e6c commit 0ac5840
Showing 1 changed file with 53 additions and 56 deletions.
109 changes: 53 additions & 56 deletions api/src/test/java/org/openmrs/api/AdministrationServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
import org.springframework.validation.BindException;

/**
* TODO clean up and finish this test class. Should test all methods in the
* {@link AdministrationService}
* Tests the {@link AdministrationService} using a database.
* Unit tests are {@link AdministrationServiceUnitTest}.
*/
public class AdministrationServiceTest extends BaseContextSensitiveTest {

Expand Down Expand Up @@ -309,133 +309,130 @@ public void getGlobalPropertiesByPrefix_shouldReturnAllRelevantGlobalPropertiesI

@Test
public void getAllowedLocales_shouldNotFailIfNotGlobalPropertyForLocalesAllowedDefinedYet() {
Context.getAdministrationService().purgeGlobalProperty(
adminService.purgeGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST));
Context.getAdministrationService().getAllowedLocales();
adminService.getAllowedLocales();
}

@Test
public void getGlobalPropertyByUuid_shouldFindObjectGivenValidUuid() {
String uuid = "4f55827e-26fe-102b-80cb-0017a47871b3";
GlobalProperty prop = Context.getAdministrationService().getGlobalPropertyByUuid(uuid);
GlobalProperty prop = adminService.getGlobalPropertyByUuid(uuid);
assertEquals("locale.allowed.list", prop.getProperty());
}

@Test
public void getGlobalPropertyByUuid_shouldReturnNullIfNoObjectFoundWithGivenUuid() {
assertNull(Context.getAdministrationService().getGlobalPropertyByUuid("some invalid uuid"));
assertNull(adminService.getGlobalPropertyByUuid("some invalid uuid"));
}

@Test
public void saveGlobalProperties_shouldNotFailWithEmptyList() {
Context.getAdministrationService().saveGlobalProperties(new ArrayList<>());
adminService.saveGlobalProperties(new ArrayList<>());
}

@Test
public void saveGlobalProperties_shouldSaveAllGlobalPropertiesToTheDatabase() {
// get the current global properties
List<GlobalProperty> globalProperties = Context.getAdministrationService().getAllGlobalProperties();
List<GlobalProperty> globalProperties = adminService.getAllGlobalProperties();

// and now add some new ones to it
globalProperties.add(new GlobalProperty("new prop1", "new prop value1", "desc"));
globalProperties.add(new GlobalProperty("new prop2", "new prop value2", "desc"));

Context.getAdministrationService().saveGlobalProperties(globalProperties);
adminService.saveGlobalProperties(globalProperties);

assertEquals("new prop value1", Context.getAdministrationService().getGlobalProperty("new prop1"));
assertEquals("new prop value2", Context.getAdministrationService().getGlobalProperty("new prop2"));
assertEquals("new prop value1", adminService.getGlobalProperty("new prop1"));
assertEquals("new prop value2", adminService.getGlobalProperty("new prop2"));
}

@Test
public void saveGlobalProperties_shouldAssignUuidToAllNewProperties() {
// get the current global properties
List<GlobalProperty> globalProperties = Context.getAdministrationService().getAllGlobalProperties();
List<GlobalProperty> globalProperties = adminService.getAllGlobalProperties();

// and now add a new one to it and save it
globalProperties.add(new GlobalProperty("new prop", "new prop value", "desc"));
Context.getAdministrationService().saveGlobalProperties(globalProperties);
adminService.saveGlobalProperties(globalProperties);

assertNotNull(Context.getAdministrationService().getGlobalPropertyObject("new prop").getUuid());
assertNotNull(adminService.getGlobalPropertyObject("new prop").getUuid());
}

@Test
public void getAllGlobalProperties_shouldReturnAllGlobalPropertiesInTheDatabase() {
executeDataSet(ADMIN_INITIAL_DATA_XML);
assertEquals(21, Context.getAdministrationService().getAllGlobalProperties().size());
assertEquals(21, adminService.getAllGlobalProperties().size());
}

@Test
public void getAllowedLocales_shouldReturnAtLeastOneLocaleIfNoLocalesDefinedInDatabaseYet() {
assertTrue(Context.getAdministrationService().getAllowedLocales().size() > 0);
assertTrue(adminService.getAllowedLocales().size() > 0);
}

@Test
public void getGlobalPropertyObject_shouldReturnNullWhenNoGlobalPropertyMatchGivenPropertyName() {
executeDataSet(ADMIN_INITIAL_DATA_XML);
assertNull(Context.getAdministrationService().getGlobalPropertyObject("magicResistSkill"));
assertNull(adminService.getGlobalPropertyObject("magicResistSkill"));
}

@Test
public void getImplementationId_shouldReturnNullIfNoImplementationIdIsDefinedYet() {
executeDataSet(ADMIN_INITIAL_DATA_XML);
assertNull(Context.getAdministrationService().getImplementationId());
assertNull(adminService.getImplementationId());
}

@Test
@Ignore
//TODO: This test fails for some reason
public void getPresentationLocales_shouldReturnAtLeastOneLocaleIfNoLocalesDefinedInDatabaseYet() {
assertTrue(Context.getAdministrationService().getPresentationLocales().size() > 0);
assertTrue(adminService.getPresentationLocales().size() > 0);
}

@Test
public void getPresentationLocales_shouldNotReturnMoreLocalesThanMessageSourceServiceLocales() {
assertFalse(Context.getAdministrationService().getPresentationLocales().size() > Context
assertFalse(adminService.getPresentationLocales().size() > Context
.getMessageSourceService().getLocales().size());
}

@Test
public void getSystemVariables_shouldReturnAllRegisteredSystemVariables() {
// The method implementation adds 11 system variables
assertEquals(11, Context.getAdministrationService().getSystemVariables().size());
assertEquals(11, adminService.getSystemVariables().size());
}

@Test
public void purgeGlobalProperty_shouldDeleteGlobalPropertyFromDatabase() {
executeDataSet(ADMIN_INITIAL_DATA_XML);
AdministrationService as = Context.getAdministrationService();

assertEquals(21, as.getAllGlobalProperties().size());
as.purgeGlobalProperty(as.getGlobalPropertyObject("a_valid_gp_key"));
assertEquals(20, as.getAllGlobalProperties().size());
assertEquals(21, adminService.getAllGlobalProperties().size());
adminService.purgeGlobalProperty(adminService.getGlobalPropertyObject("a_valid_gp_key"));
assertEquals(20, adminService.getAllGlobalProperties().size());
}

@Test
public void saveGlobalProperty_shouldCreateGlobalPropertyInDatabase() {
executeDataSet(ADMIN_INITIAL_DATA_XML);
AdministrationService as = Context.getAdministrationService();

as.saveGlobalProperty(new GlobalProperty("detectHiddenSkill", "100"));
assertNotNull(as.getGlobalProperty("detectHiddenSkill"));
adminService.saveGlobalProperty(new GlobalProperty("detectHiddenSkill", "100"));
assertNotNull(adminService.getGlobalProperty("detectHiddenSkill"));
}

@Test
public void saveGlobalProperty_shouldOverwriteGlobalPropertyIfExists() {
executeDataSet(ADMIN_INITIAL_DATA_XML);
AdministrationService as = Context.getAdministrationService();

GlobalProperty gp = as.getGlobalPropertyObject("a_valid_gp_key");
GlobalProperty gp = adminService.getGlobalPropertyObject("a_valid_gp_key");
assertEquals("correct-value", gp.getPropertyValue());
gp.setPropertyValue("new-even-more-correct-value");
as.saveGlobalProperty(gp);
assertEquals("new-even-more-correct-value", as.getGlobalProperty("a_valid_gp_key"));
adminService.saveGlobalProperty(gp);
assertEquals("new-even-more-correct-value", adminService.getGlobalProperty("a_valid_gp_key"));
}

@Test
public void getAllowedLocales_shouldNotReturnDuplicatesEvenIfTheGlobalPropertyHasThem() {
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB,fr,es,en_GB"));
assertEquals(3, Context.getAdministrationService().getAllowedLocales().size());
assertEquals(3, adminService.getAllowedLocales().size());
}

@Test
Expand Down Expand Up @@ -548,15 +545,15 @@ public void saveGlobalProperty_shouldSaveAGlobalPropertyWhoseTypedValueIsHandled
@Test
public void getSearchLocales_shouldExcludeNotAllowedLocales() {
//given
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_US, en_GB, pl, es"));

User user = Context.getAuthenticatedUser();
user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "es_CL, en_US, pl");
Context.getUserService().saveUser(user);

//when
List<Locale> searchLocales = Context.getAdministrationService().getSearchLocales();
List<Locale> searchLocales = adminService.getSearchLocales();

//then
assertTrue("en_US", searchLocales.contains(new Locale("en", "US")));
Expand All @@ -568,15 +565,15 @@ public void getSearchLocales_shouldExcludeNotAllowedLocales() {
@Test
public void getSearchLocales_shouldIncludeCurrentlySelectedFullLocaleAndLangugage() {
//given
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB"));
User user = Context.getAuthenticatedUser();
user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "");
Context.getUserService().saveUser(user);
Context.setLocale(new Locale("en", "GB"));

//when
List<Locale> searchLocales = Context.getAdministrationService().getSearchLocales();
List<Locale> searchLocales = adminService.getSearchLocales();

//then
assertEquals(Context.getLocale(), searchLocales.get(0));
Expand All @@ -586,15 +583,15 @@ public void getSearchLocales_shouldIncludeCurrentlySelectedFullLocaleAndLangugag
@Test
public void getSearchLocales_shouldIncludeUsersProficientLocales() {
//given
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, en_US, pl"));

User user = Context.getAuthenticatedUser();
user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "en_GB, en_US");
Context.getUserService().saveUser(user);

//when
List<Locale> searchLocales = Context.getAdministrationService().getSearchLocales();
List<Locale> searchLocales = adminService.getSearchLocales();

//then
assertTrue("en_GB", searchLocales.contains(new Locale("en", "GB")));
Expand All @@ -605,13 +602,13 @@ public void getSearchLocales_shouldIncludeUsersProficientLocales() {
@Test(expected = APIException.class)
public void validate_shouldThrowThrowAPIExceptionIfTheInputIsNull() {
BindException errors = new BindException(new Object(), "");
Context.getAdministrationService().validate(null, errors);
adminService.validate(null, errors);
}

@Test
public void getPresentationLocales_shouldReturnOnlyCountryLocaleIfBothCountryLocaleAndLanguageLocaleAreSpecifiedInAllowedList()
{
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, es, es_CL"));

List<Locale> locales = new ArrayList<>();
Expand All @@ -627,7 +624,7 @@ public void getPresentationLocales_shouldReturnOnlyCountryLocaleIfBothCountryLoc
MutableMessageSource mutableMessageSource = Context.getMessageSourceService().getActiveMessageSource();
Context.getMessageSourceService().setActiveMessageSource(mutableResourceBundleMessageSource);

Set<Locale> presentationLocales = Context.getAdministrationService().getPresentationLocales();
Set<Locale> presentationLocales = adminService.getPresentationLocales();

Context.getMessageSourceService().setActiveMessageSource(mutableMessageSource);

Expand All @@ -639,7 +636,7 @@ public void getPresentationLocales_shouldReturnOnlyCountryLocaleIfBothCountryLoc
@Test
public void getPresentationLocales_shouldReturnAllCountryLocalesIfLanguageLocaleAndNoCountryLocalesAreSpecifiedInAllowedList()
{
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, es"));

List<Locale> locales = new ArrayList<>();
Expand All @@ -656,7 +653,7 @@ public void getPresentationLocales_shouldReturnAllCountryLocalesIfLanguageLocale
MutableMessageSource mutableMessageSource = Context.getMessageSourceService().getActiveMessageSource();
Context.getMessageSourceService().setActiveMessageSource(mutableResourceBundleMessageSource);

Set<Locale> presentationLocales = Context.getAdministrationService().getPresentationLocales();
Set<Locale> presentationLocales = adminService.getPresentationLocales();

Context.getMessageSourceService().setActiveMessageSource(mutableMessageSource);

Expand All @@ -669,7 +666,7 @@ public void getPresentationLocales_shouldReturnAllCountryLocalesIfLanguageLocale
@Test
public void getPresentationLocales_shouldReturnLanguageLocaleIfCountryLocaleIsSpecifiedInAllowedListButCountryLocaleMessageFileIsMissing()
{
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, es_CL"));

List<Locale> locales = new ArrayList<>();
Expand All @@ -684,7 +681,7 @@ public void getPresentationLocales_shouldReturnLanguageLocaleIfCountryLocaleIsSp
MutableMessageSource mutableMessageSource = Context.getMessageSourceService().getActiveMessageSource();
Context.getMessageSourceService().setActiveMessageSource(mutableResourceBundleMessageSource);

Set<Locale> presentationLocales = Context.getAdministrationService().getPresentationLocales();
Set<Locale> presentationLocales = adminService.getPresentationLocales();

Context.getMessageSourceService().setActiveMessageSource(mutableMessageSource);

Expand All @@ -696,7 +693,7 @@ public void getPresentationLocales_shouldReturnLanguageLocaleIfCountryLocaleIsSp
@Test
public void getPresentationLocales_shouldReturnLanguageLocaleIfItIsSpecifiedInAllowedListAndThereAreNoCountryLocaleMessageFilesAvailable()
{
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, es"));

List<Locale> locales = new ArrayList<>();
Expand All @@ -711,7 +708,7 @@ public void getPresentationLocales_shouldReturnLanguageLocaleIfItIsSpecifiedInAl
MutableMessageSource mutableMessageSource = Context.getMessageSourceService().getActiveMessageSource();
Context.getMessageSourceService().setActiveMessageSource(mutableResourceBundleMessageSource);

Set<Locale> presentationLocales = Context.getAdministrationService().getPresentationLocales();
Set<Locale> presentationLocales = adminService.getPresentationLocales();

Context.getMessageSourceService().setActiveMessageSource(mutableMessageSource);

Expand All @@ -725,7 +722,7 @@ public void getPresentationLocales_shouldPreserveInsertionOrderInSetReturnedByMe
{
String globalPropertyLocaleListAllowedData = "en_GB, es, ja_JP, it_IT, pl_PL";
//The order of languages and locales is described above and should be followed bt `presentationLocales` Set
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, globalPropertyLocaleListAllowedData));

List<Locale> locales = new ArrayList<>();
Expand All @@ -742,7 +739,7 @@ public void getPresentationLocales_shouldPreserveInsertionOrderInSetReturnedByMe
MutableMessageSource mutableMessageSource = Context.getMessageSourceService().getActiveMessageSource();
Context.getMessageSourceService().setActiveMessageSource(mutableResourceBundleMessageSource);

List<Locale> presentationLocales = new ArrayList<>(Context.getAdministrationService().getPresentationLocales());
List<Locale> presentationLocales = new ArrayList<>(adminService.getPresentationLocales());

Context.getMessageSourceService().setActiveMessageSource(mutableMessageSource);

Expand All @@ -756,15 +753,15 @@ public void getPresentationLocales_shouldPreserveInsertionOrderInSetReturnedByMe
@Test
public void getSearchLocales_shouldCacheResultsForAnUser() {
//given
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, en_US, pl"));

User user = Context.getAuthenticatedUser();
user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "en_GB, en_US");
Context.getUserService().saveUser(user);

//when
Context.getAdministrationService().getSearchLocales();
adminService.getSearchLocales();

List<Locale> cachedSearchLocales = getCachedSearchLocalesForCurrentUser();

Expand All @@ -777,20 +774,20 @@ public void getSearchLocales_shouldCacheResultsForAnUser() {
@Test
public void saveGlobalProperty_shouldEvictCachedResults() {
//given
Context.getAdministrationService().saveGlobalProperty(
adminService.saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, en_US, pl"));

User user = Context.getAuthenticatedUser();
user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, "en_GB, en_US");
Context.getUserService().saveUser(user);

//sanity check that cache has been populated
Context.getAdministrationService().getSearchLocales();
adminService.getSearchLocales();
List<Locale> cachedSearchLocales = getCachedSearchLocalesForCurrentUser();
assertThat(cachedSearchLocales, hasItem(new Locale("en", "US")));

//evict cache
Context.getAdministrationService().saveGlobalProperty(new GlobalProperty("test", "TEST"));
adminService.saveGlobalProperty(new GlobalProperty("test", "TEST"));

assertThat(getCacheForCurrentUser(), nullValue());
}
Expand Down

0 comments on commit 0ac5840

Please sign in to comment.