Skip to content

Commit

Permalink
Improvements and unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Oct 12, 2023
1 parent 85d5576 commit d7cbda3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.orcid.persistence.jpa.entities.EmailDomainEntity;
import org.orcid.persistence.jpa.entities.EmailDomainEntity.DomainCategory;

import com.google.common.net.InternetDomainName;

public class EmailDomainManagerImpl implements EmailDomainManager {

@Resource(name = "emailDomainDao")
Expand All @@ -22,6 +24,9 @@ public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory ca
if (emailDomain == null || emailDomain.isBlank()) {
throw new IllegalArgumentException("Email Domain must not be empty");
}
if(!InternetDomainName.isValid(emailDomain)) {
throw new IllegalArgumentException("Email Domain '" + emailDomain + "' is invalid");
}
if (category == null) {
throw new IllegalArgumentException("Category must not be empty");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.orcid.core.common.manager;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.orcid.core.common.manager.impl.EmailDomainManagerImpl;
import org.orcid.persistence.dao.EmailDomainDao;
import org.orcid.test.TargetProxyHelper;

public class EmailDomainManagerTest {
@Mock
private EmailDomainDao emailDomainDaoMock;

@Mock
private EmailDomainDao emailDomainDaoReadOnlyMock;

EmailDomainManager edm = new EmailDomainManagerImpl();

@Before
public void before(){
TargetProxyHelper.injectIntoProxy(edm, "emailDomainDao", emailDomainDaoMock);
TargetProxyHelper.injectIntoProxy(edm, "emailDomainDaoReadOnly", emailDomainDaoReadOnlyMock);
}

@Test
public void createEmailDomainTest() {
// Check null domain
// Check empty domain
// Check null category
// Check good value
}

@Test
public void updateCategory() {

}

@Test
public void findByEmailDoman() {

}

@Test
public void findByCategory() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
import org.orcid.persistence.dao.EmailDomainDao;
import org.orcid.persistence.jpa.entities.EmailDomainEntity;
import org.orcid.persistence.jpa.entities.EmailDomainEntity.DomainCategory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

public class EmailDomainDaoImpl extends GenericDaoImpl<EmailDomainEntity, Long> implements EmailDomainDao {

private static final Logger LOG = LoggerFactory.getLogger(EmailDomainDaoImpl.class);

public EmailDomainDaoImpl() {
super(EmailDomainEntity.class);
}

@Override
@Transactional
public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory category) {
LOG.debug("Creating domain {} with category {}", emailDomain, category);
EmailDomainEntity e = new EmailDomainEntity();
e.setEmailDomain(emailDomain);
e.setCategory(category);
Expand All @@ -30,9 +35,10 @@ public EmailDomainEntity createEmailDomain(String emailDomain, DomainCategory ca
@Override
@Transactional
public boolean updateCategory(long id, DomainCategory category) {
LOG.debug("Updating domain with id {} with category {}", id, category);
Query query = entityManager.createNativeQuery("UPDATE email_domain SET category=:category WHERE id = :id");
query.setParameter("id", id);
query.setParameter("category", category);
query.setParameter("category", category.toString());
return query.executeUpdate() > 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvParser;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

public class EmailDomainLoader {

Expand Down Expand Up @@ -82,7 +80,7 @@ private void process() {
EmailDomainEntity newEde = emailDomainManager.createEmailDomain(elementDomain, category);
newEntities += 1;
LOG.info("New EmailDomainEntity created for domain {} with id {}", elementDomain, newEde.getId());
} else if(!elementDomain.equalsIgnoreCase(ede.getCategory().name())) {
} else if(!elementCategory.equalsIgnoreCase(ede.getCategory().toString())) {
boolean updated = emailDomainManager.updateCategory(ede.getId(), category);
if(updated) {
LOG.info("Email category has been update for email domain {} from {} to {}", elementDomain, ede.getCategory(), elementCategory);
Expand All @@ -96,7 +94,7 @@ private void process() {
}

public static void main(String[] args) throws IOException {
String filePath = "C:/Users/angel/Documents/ORCID/development/tmp/deleteme/email_domains/file.csv";
String filePath = args[0];
EmailDomainLoader edl = new EmailDomainLoader(filePath);
edl.execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.net.InternetDomainName;

@Controller("emailDomainController")
@RequestMapping(value = { "/email-domain" })
public class EmailDomainController {
Expand All @@ -23,7 +21,7 @@ public class EmailDomainController {
@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 || !InternetDomainName.isValid(domain)) {
if(domain == null || domain.length() > 64) {
return "{'error':'domain lenght too long or invalid'}";
}

Expand Down

0 comments on commit d7cbda3

Please sign in to comment.