Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>adminui</artifactId>
<version>1.7.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>adminui-api</artifactId>
Expand All @@ -33,12 +33,14 @@
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
<type>jar</type>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openmrs.web</groupId>
<artifactId>openmrs-web</artifactId>
<type>jar</type>
<scope>provided</scope>
</dependency>

<dependency>
Expand All @@ -64,11 +66,6 @@

<!-- End OpenMRS core -->

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>providermanagement-api</artifactId>
</dependency>

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>uiframework-api</artifactId>
Expand All @@ -78,6 +75,18 @@
<groupId>org.openmrs.module</groupId>
<artifactId>legacyui-omod</artifactId>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
70 changes: 24 additions & 46 deletions api/src/test/java/org/openmrs/module/adminui/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
package org.openmrs.module.adminui;

import org.apache.commons.beanutils.PropertyUtils;
import org.hamcrest.Matcher;
import org.junit.Assert;
import org.mockito.ArgumentMatcher;
import org.openmrs.util.OpenmrsUtil;

import java.util.Collection;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Various utils to help with testing
Expand All @@ -29,9 +26,10 @@ public class TestUtils {
/**
* To test things like: assertContainsElementWithProperty(listOfPatients, "patientId", 2)
*
* @param collection
* @param property
* @param value
* @param collection the collection to search
* @param property the name of the bean property to evaluate
* @param value the expected value of the property
* @throws AssertionError if no matching element is found
*/
public static void assertContainsElementWithProperty(Collection<?> collection, String property, Object value) {
for (Object o : collection) {
Expand All @@ -40,50 +38,30 @@ public static void assertContainsElementWithProperty(Collection<?> collection, S
return;
}
} catch (Exception ex) {
// pass
// Skip this element
}
}
Assert.fail("Collection does not contain an element with " + property + " = " + value + ". Collection: "
+ collection);
}

public static <T> ArgumentMatcher<T> isCollectionOfExactlyElementsWithProperties(final String property,
final Object... expectedPropertyValues) {
return new ArgumentMatcher<T>() {

@Override
public boolean matches(Object o) {
assertTrue(o instanceof Collection);
Collection actual = (Collection) o;
assertThat(actual.size(), is(expectedPropertyValues.length));
for (Object expectedPropertyValue : expectedPropertyValues) {
assertContainsElementWithProperty(actual, property, expectedPropertyValue);
}
return true;
}
};
fail("Collection does not contain an element with " + property + " = " + value + ". Collection: " + collection);
}

/**
* Creates an argument matcher that tests equality based on the equals method, the developer
* doesn't have to type cast the returned argument when pass it to
* {@link org.mockito.Mockito#argThat(org.hamcrest.Matcher)} as it would be the case if we used
* {@link org.mockito.internal.matchers.Equals} matcher
* Asserts that the given collection contains exactly the expected property values, with no extra or missing values.
* The assertion passes if the collection contains the same number of elements as the number of expected values,
* and each expected property value is found in at least one element of the collection.
* This does not consider order or duplicates; it treats both actual and expected values as sets.
*
* @param object
* @return Matcher
* @param collection the collection to check
* @param property the bean property to evaluate for each element
* @param expectedPropertyValues the expected values of the property across the collection
* @throws AssertionError if the size does not match or any expected value is missing
*/
@SuppressWarnings("unchecked")
public static <T> Matcher<T> equalsMatcher(final T object) {
return new ArgumentMatcher<T>() {
public static void assertCollectionHasExactlyElementsWithProperty(Collection<?> collection, String property, Object... expectedPropertyValues) {
assertNotNull(collection, "Collection is null");
assertEquals(expectedPropertyValues.length, collection.size(), "Collection size does not match expected");

/**
* @see org.mockito.ArgumentMatcher#matches(Object)
*/
@Override
public boolean matches(Object arg) {
return OpenmrsUtil.nullSafeEquals(object, (T) arg);
}
};
for (Object expectedValue : expectedPropertyValues) {
assertContainsElementWithProperty(collection, property, expectedValue);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@
*/
package org.openmrs.module.adminui.account;

import static org.junit.Assert.assertNotNull;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openmrs.Provider;
import org.openmrs.User;
import org.openmrs.api.ProviderService;
import org.openmrs.api.UserService;
import org.openmrs.api.context.Context;
import org.openmrs.module.providermanagement.Provider;
import org.openmrs.module.providermanagement.api.ProviderManagementService;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.openmrs.test.jupiter.BaseModuleContextSensitiveTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class AccountServiceComponentTest extends BaseModuleContextSensitiveTest {
public class AccountServiceComponentTest extends BaseModuleContextSensitiveTest{

private AccountService accountService;

private UserService userService;

private ProviderService providerService;

private ProviderManagementService providerManagementService;

@Before
@BeforeEach
public void setup() {
userService = Context.getUserService();
accountService = Context.getService(AccountService.class);
providerManagementService = Context.getService(ProviderManagementService.class);
providerService = Context.getProviderService();
}

/**
Expand All @@ -48,7 +48,7 @@ public void saveAccount_shouldSaveNewAccountWithAProviderAndUserAccount() throws
user.addRole(userService.getRole("Application Role: Archives"));//capability

Provider provider = new Provider();
provider.setProviderRole(providerManagementService.getProviderRole(1001));
provider.setProviderRole(providerService.getProviderRole(1001));
Account account = new Account(null);
account.setFamilyName("some");
account.setGivenName("tester");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
*/
package org.openmrs.module.adminui.account;

import static org.junit.Assert.assertThat;
import org.junit.jupiter.api.Test;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openmrs.Person;
import org.openmrs.Provider;
import org.openmrs.Role;
Expand All @@ -28,18 +28,10 @@
import org.openmrs.api.PersonService;
import org.openmrs.api.ProviderService;
import org.openmrs.api.UserService;
import org.openmrs.api.context.Context;
import org.openmrs.module.adminui.AdminUiConstants;
import org.openmrs.module.adminui.TestUtils;
import org.openmrs.module.providermanagement.api.ProviderManagementService;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Context.class)
@PowerMockIgnore("jdk.internal.reflect.*")
@ExtendWith(MockitoExtension.class)
public class AccountServiceTest {

private AccountServiceImpl accountService;
Expand All @@ -50,26 +42,20 @@ public class AccountServiceTest {

private ProviderService providerService;

private ProviderManagementService providerManagementService;

private AdministrationService adminService;
@Before

@BeforeEach
public void setup() {
PowerMockito.mockStatic(Context.class);
userService = mock(UserService.class);
personService = mock(PersonService.class);
providerService = mock(ProviderService.class);
providerManagementService = mock(ProviderManagementService.class);
adminService = mock(AdministrationService.class);

accountService = new AccountServiceImpl();
accountService.setUserService(userService);
accountService.setPersonService(personService);
accountService.setProviderService(providerService);
accountService.setAdminService(adminService);
when(Context.getUserService()).thenReturn(userService);
when(Context.getProviderService()).thenReturn(providerService);
}

/**
Expand Down Expand Up @@ -107,7 +93,7 @@ public void getAllAccounts_shouldGetAllUniqueAccounts() throws Exception {
when(userService.getAllUsers()).thenReturn(Arrays.asList(user1, user2, user3, daemonUser));
when(providerService.getAllProviders()).thenReturn(Arrays.asList(provider1, provider2, unknownProvider));

Assert.assertEquals(3, accountService.getAllAccounts().size());
assertEquals(3, accountService.getAllAccounts().size());
}

/**
Expand All @@ -122,28 +108,29 @@ public void getAllCapabilities_shouldReturnAllRolesWithTheCapabilityPrefix() thr

when(userService.getAllRoles()).thenReturn(Arrays.asList(role1, role2, role3));
List<Role> capabilities = accountService.getAllCapabilities();
Assert.assertEquals(2, capabilities.size());
assertThat(capabilities, TestUtils.isCollectionOfExactlyElementsWithProperties("role",
AdminUiConstants.ROLE_PREFIX_CAPABILITY + "role1", AdminUiConstants.ROLE_PREFIX_CAPABILITY + "role3"));
assertEquals(2, capabilities.size());
TestUtils.assertCollectionHasExactlyElementsWithProperty(
capabilities, "role", AdminUiConstants.ROLE_PREFIX_CAPABILITY + "role1",
AdminUiConstants.ROLE_PREFIX_CAPABILITY + "role3"
);
}

/**
* @verifies return all roles with the privilege level prefix
* @see AccountService#getAllPrivilegeLevels()
*/
@Test
public void getAllPrivilegeLevels_shouldReturnAllRolesWithThePrivilegeLevelPrefix() throws Exception {
public void getAllPrivilegeLevels_shouldReturnAllRolesWithThePrivilegeLevelPrefix() {
Role role1 = new Role(AdminUiConstants.ROLE_PREFIX_PRIVILEGE_LEVEL + "role1");
Role role3 = new Role("role2");
Role role2 = new Role(AdminUiConstants.ROLE_PREFIX_PRIVILEGE_LEVEL + "role3");

when(userService.getAllRoles()).thenReturn(Arrays.asList(role1, role2, role3));
List<Role> privilegeLevels = accountService.getAllPrivilegeLevels();
Assert.assertEquals(2, privilegeLevels.size());
assertThat(
privilegeLevels,
TestUtils.isCollectionOfExactlyElementsWithProperties("role", AdminUiConstants.ROLE_PREFIX_PRIVILEGE_LEVEL
+ "role1", AdminUiConstants.ROLE_PREFIX_PRIVILEGE_LEVEL + "role3"));
assertEquals(2, privilegeLevels.size());
TestUtils.assertCollectionHasExactlyElementsWithProperty(
privilegeLevels, "role", AdminUiConstants.ROLE_PREFIX_PRIVILEGE_LEVEL + "role1",
AdminUiConstants.ROLE_PREFIX_PRIVILEGE_LEVEL + "role3"
);
}

}
Loading