From 47a67654b0fb0712ae192af055b665f551da620a Mon Sep 17 00:00:00 2001 From: amontenegro Date: Thu, 12 Oct 2023 13:51:50 -0600 Subject: [PATCH] More testing needed --- .../core/utils/cache/redis/RedisClient.java | 4 + .../manager/EmailDomainManagerTest.java | 121 +++++++++++++++--- .../jpa/entities/EmailDomainEntity.java | 8 ++ .../controllers/EmailDomainController.java | 10 +- 4 files changed, 123 insertions(+), 20 deletions(-) diff --git a/orcid-core/src/main/java/org/orcid/core/utils/cache/redis/RedisClient.java b/orcid-core/src/main/java/org/orcid/core/utils/cache/redis/RedisClient.java index 99952cfc018..42affcf8f47 100644 --- a/orcid-core/src/main/java/org/orcid/core/utils/cache/redis/RedisClient.java +++ b/orcid-core/src/main/java/org/orcid/core/utils/cache/redis/RedisClient.java @@ -66,6 +66,10 @@ public RedisClient(String redisHost, int redisPort, String password, int cacheEx @PostConstruct private void init() { + if(!enabled) { + LOG.debug("Redis is not enabled, so, it will not be initilized"); + return; + } try { JedisClientConfig config = DefaultJedisClientConfig.builder().connectionTimeoutMillis(this.clientTimeoutInMillis).timeoutMillis(this.clientTimeoutInMillis) .socketTimeoutMillis(this.clientTimeoutInMillis).password(this.redisPassword).ssl(true).build(); diff --git a/orcid-core/src/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java b/orcid-core/src/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java index 1c6b8d86baf..bc347b9977f 100644 --- a/orcid-core/src/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java +++ b/orcid-core/src/test/java/org/orcid/core/common/manager/EmailDomainManagerTest.java @@ -1,10 +1,24 @@ package org.orcid.core.common.manager; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; + import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.orcid.core.common.manager.impl.EmailDomainManagerImpl; import org.orcid.persistence.dao.EmailDomainDao; +import org.orcid.persistence.jpa.entities.EmailDomainEntity; +import org.orcid.persistence.jpa.entities.EmailDomainEntity.DomainCategory; import org.orcid.test.TargetProxyHelper; public class EmailDomainManagerTest { @@ -13,35 +27,112 @@ public class EmailDomainManagerTest { @Mock private EmailDomainDao emailDomainDaoReadOnlyMock; - + EmailDomainManager edm = new EmailDomainManagerImpl(); - + @Before - public void before(){ + public void before() { + MockitoAnnotations.initMocks(this); TargetProxyHelper.injectIntoProxy(edm, "emailDomainDao", emailDomainDaoMock); TargetProxyHelper.injectIntoProxy(edm, "emailDomainDaoReadOnly", emailDomainDaoReadOnlyMock); + + EmailDomainEntity e1 = new EmailDomainEntity("gmail.com", DomainCategory.PERSONAL); + EmailDomainEntity e2 = new EmailDomainEntity("yahoo.com", DomainCategory.PERSONAL); + EmailDomainEntity e3 = new EmailDomainEntity("orcid.org", DomainCategory.PROFESSIONAL); + + when(emailDomainDaoReadOnlyMock.findByCategory(eq(DomainCategory.PERSONAL))).thenReturn(List.of(e1, e2)); + when(emailDomainDaoReadOnlyMock.findByCategory(eq(DomainCategory.PROFESSIONAL))).thenReturn(List.of(e3)); + + when(emailDomainDaoReadOnlyMock.findByEmailDoman("gmail.com")).thenReturn(e1); } - + + @Test(expected = IllegalArgumentException.class) + public void createEmailDomain_NullDomainTest() { + edm.createEmailDomain(null, DomainCategory.PROFESSIONAL); + } + + @Test(expected = IllegalArgumentException.class) + public void createEmailDomain_EmptyDomainTest() { + edm.createEmailDomain(" ", DomainCategory.PROFESSIONAL); + } + + @Test(expected = IllegalArgumentException.class) + public void createEmailDomain_InvalidDomainTest() { + edm.createEmailDomain("$$$", DomainCategory.PROFESSIONAL); + } + + @Test(expected = IllegalArgumentException.class) + public void createEmailDomain_NullCategoryTest() { + edm.createEmailDomain("orcid.org", null); + } + @Test public void createEmailDomainTest() { - // Check null domain - // Check empty domain - // Check null category - // Check good value + edm.createEmailDomain("orcid.org", DomainCategory.PROFESSIONAL); + verify(emailDomainDaoMock, times(1)).createEmailDomain(eq("orcid.org"), eq(DomainCategory.PROFESSIONAL)); + } + + @Test(expected = IllegalArgumentException.class) + public void updateCategory_nullCategoryTest() { + edm.updateCategory(0, null); } @Test - public void updateCategory() { - + public void updateCategoryTest() { + edm.updateCategory(0, DomainCategory.PERSONAL); + verify(emailDomainDaoMock, times(1)).updateCategory(eq(Long.valueOf(0)), eq(DomainCategory.PERSONAL)); } - @Test - public void findByEmailDoman() { - + @Test(expected = IllegalArgumentException.class) + public void findByEmailDoman_NullDomainTest() { + edm.findByEmailDoman(null); } + @Test(expected = IllegalArgumentException.class) + public void findByEmailDoman_EmptyDomainTest() { + edm.findByEmailDoman(" "); + } + @Test - public void findByCategory() { - + public void findByEmailDoman_NothingFoundTest() { + assertNull(edm.findByEmailDoman("other.com")); + } + + @Test + public void findByEmailDomanTest() { + EmailDomainEntity ede = edm.findByEmailDoman("gmail.com"); + assertNotNull(ede); + assertEquals("gmail.com", ede.getEmailDomain()); + assertEquals(DomainCategory.PERSONAL, ede.getCategory()); + } + + @Test(expected = IllegalArgumentException.class) + public void findByCategory_NullCategoryTest() { + edm.findByEmailDoman(null); + } + + @Test + public void findByCategory_NothingFoundTest() { + assertTrue(edm.findByCategory(DomainCategory.UNDEFINED).isEmpty()); + } + + @Test + public void findByCategory_OneResultTest() { + List personal = edm.findByCategory(DomainCategory.PROFESSIONAL); + assertNotNull(personal); + assertEquals(1, personal.size()); + assertEquals(DomainCategory.PROFESSIONAL, personal.get(0).getCategory()); + assertEquals("orcid.org", personal.get(0).getEmailDomain()); + } + + @Test + public void findByCategory_TwoResultsTest() { + List personal = edm.findByCategory(DomainCategory.PERSONAL); + assertNotNull(personal); + assertEquals(2, personal.size()); + assertEquals(DomainCategory.PERSONAL, personal.get(0).getCategory()); + assertEquals("gmail.com", personal.get(0).getEmailDomain()); + assertEquals(DomainCategory.PERSONAL, personal.get(1).getCategory()); + assertEquals("yahoo.com", personal.get(1).getEmailDomain()); } } diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainEntity.java b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainEntity.java index 628a1836b81..06ea938e449 100644 --- a/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainEntity.java +++ b/orcid-persistence/src/main/java/org/orcid/persistence/jpa/entities/EmailDomainEntity.java @@ -29,7 +29,15 @@ public static enum DomainCategory {PERSONAL, PROFESSIONAL, UNDEFINED} private String emailDomain; private DomainCategory category; + public EmailDomainEntity() { + + } + public EmailDomainEntity(String emailDomain, DomainCategory category) { + this.emailDomain = emailDomain; + this.category = category; + } + @Override @Id @GeneratedValue(strategy = GenerationType.AUTO, generator = "email_domain_seq") diff --git a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/EmailDomainController.java b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/EmailDomainController.java index 6d224f8f490..6329bb16a36 100644 --- a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/EmailDomainController.java +++ b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/EmailDomainController.java @@ -4,6 +4,7 @@ import javax.ws.rs.core.MediaType; import org.orcid.core.common.manager.EmailDomainManager; +import org.orcid.core.utils.OrcidStringUtils; import org.orcid.persistence.jpa.entities.EmailDomainEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @@ -19,12 +20,11 @@ public class EmailDomainController { private EmailDomainManager emailDomainManager; @RequestMapping(value = "/find-category", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON) - public @ResponseBody String findCategory(@RequestParam("domain") String domain) { - - if(domain == null || domain.length() > 64) { + public @ResponseBody String findCategory(@RequestParam("domain") String domain) { + if(domain == null || domain.isBlank() || domain.length() > 64) { return "{'error':'domain lenght too long or invalid'}"; - } - + } + domain = OrcidStringUtils.stripHtml(domain); EmailDomainEntity ede = emailDomainManager.findByEmailDoman(domain); if(ede == null) { return "{'category':'" + EmailDomainEntity.DomainCategory.UNDEFINED.name() + "'}";