Search in sources :

Example 1 with LockAccounts

use of org.orcid.pojo.LockAccounts in project ORCID-Source by ORCID.

the class AdminControllerTest method testLockAccounts.

@Test
public void testLockAccounts() {
    ProfileEntityCacheManager profileEntityCacheManager = Mockito.mock(ProfileEntityCacheManager.class);
    ProfileEntityManager profileEntityManager = Mockito.mock(ProfileEntityManager.class);
    EmailManager emailManager = Mockito.mock(EmailManager.class);
    AdminController adminController = new AdminController();
    ReflectionTestUtils.setField(adminController, "profileEntityManager", profileEntityManager);
    ReflectionTestUtils.setField(adminController, "emailManager", emailManager);
    ReflectionTestUtils.setField(adminController, "profileEntityCacheManager", profileEntityCacheManager);
    String commaSeparatedValues = "some,orcid,ids,or,emails,to,test,with,reviewed";
    Mockito.when(emailManager.emailExists(Mockito.anyString())).thenReturn(true);
    Mockito.when(emailManager.emailExists(Mockito.eq("some"))).thenReturn(false);
    Mockito.when(emailManager.emailExists(Mockito.eq("orcid"))).thenReturn(false);
    Map<String, String> map = new HashMap<String, String>();
    map.put("ids", "ids");
    map.put("or", "or");
    map.put("emails", "emails");
    map.put("to", "to");
    map.put("test", "test");
    map.put("with", "with");
    map.put("reviewed", "reviewed");
    Mockito.when(emailManager.findOricdIdsByCommaSeparatedEmails(Mockito.anyString())).thenReturn(map);
    Mockito.when(profileEntityManager.orcidExists(Mockito.anyString())).thenReturn(true);
    Mockito.when(profileEntityManager.orcidExists(Mockito.eq("some"))).thenReturn(false);
    Mockito.when(profileEntityManager.orcidExists(Mockito.eq("orcid"))).thenReturn(false);
    Mockito.when(profileEntityCacheManager.retrieve(Mockito.anyString())).thenAnswer(new Answer<ProfileEntity>() {

        @Override
        public ProfileEntity answer(InvocationOnMock invocation) throws Throwable {
            String ar1 = invocation.getArgument(0);
            ProfileEntity p = new ProfileEntity();
            p.setId(ar1);
            if (ar1.equals("ids") || ar1.equals("or")) {
                p.setRecordLocked(true);
            } else {
                p.setRecordLocked(false);
            }
            if (ar1.contentEquals("reviewed")) {
                p.setReviewed(true);
            } else {
                p.setReviewed(false);
            }
            return p;
        }
    });
    LockAccounts lockAccounts = new LockAccounts();
    lockAccounts.setOrcidsToLock(commaSeparatedValues);
    lockAccounts.setLockReason(LockReason.SPAM.getLabel());
    Map<String, Set<String>> results = adminController.lockAccounts(lockAccounts);
    assertEquals(2, results.get("notFoundList").size());
    assertTrue(results.get("notFoundList").contains("some"));
    assertTrue(results.get("notFoundList").contains("orcid"));
    assertEquals(2, results.get("alreadyLockedList").size());
    assertTrue(results.get("alreadyLockedList").contains("ids"));
    assertTrue(results.get("alreadyLockedList").contains("or"));
    assertEquals(4, results.get("lockSuccessfulList").size());
    assertTrue(results.get("lockSuccessfulList").contains("emails"));
    assertTrue(results.get("lockSuccessfulList").contains("to"));
    assertTrue(results.get("lockSuccessfulList").contains("test"));
    assertTrue(results.get("lockSuccessfulList").contains("with"));
    assertEquals(1, results.get("reviewedList").size());
    assertTrue(results.get("reviewedList").contains("reviewed"));
    Mockito.verify(emailManager, Mockito.times(9)).emailExists(Mockito.anyString());
    Mockito.verify(profileEntityManager, Mockito.times(4)).lockProfile(Mockito.anyString(), Mockito.anyString(), isNull());
}
Also used : ProfileEntityCacheManager(org.orcid.core.manager.ProfileEntityCacheManager) Set(java.util.Set) HashMap(java.util.HashMap) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) LockAccounts(org.orcid.pojo.LockAccounts) EmailManager(org.orcid.core.manager.EmailManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ProfileEntityManager(org.orcid.core.manager.ProfileEntityManager) Test(org.junit.Test) BaseControllerTest(org.orcid.frontend.web.util.BaseControllerTest)

Aggregations

HashMap (java.util.HashMap)1 Set (java.util.Set)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 EmailManager (org.orcid.core.manager.EmailManager)1 ProfileEntityCacheManager (org.orcid.core.manager.ProfileEntityCacheManager)1 ProfileEntityManager (org.orcid.core.manager.ProfileEntityManager)1 BaseControllerTest (org.orcid.frontend.web.util.BaseControllerTest)1 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)1 LockAccounts (org.orcid.pojo.LockAccounts)1