From 963058fb26cecaa45fdbc22dea61c0e5a32f7a3e Mon Sep 17 00:00:00 2001 From: Matija Obreza Date: Thu, 27 Mar 2014 13:24:01 +0100 Subject: [PATCH] Mock EasySMTA connector --- .../org/genesys2/server/service/EasySMTA.java | 97 +++++++++++++++++++ .../service/impl/EasySMTAConnector.java | 81 ++-------------- .../service/impl/EasySMTAMockConnector.java | 46 +++++++++ 3 files changed, 150 insertions(+), 74 deletions(-) create mode 100644 src/main/java/org/genesys2/server/service/EasySMTA.java create mode 100644 src/main/java/org/genesys2/server/service/impl/EasySMTAMockConnector.java diff --git a/src/main/java/org/genesys2/server/service/EasySMTA.java b/src/main/java/org/genesys2/server/service/EasySMTA.java new file mode 100644 index 000000000..5fd193d43 --- /dev/null +++ b/src/main/java/org/genesys2/server/service/EasySMTA.java @@ -0,0 +1,97 @@ +/** + * Copyright 2014 Global Crop Diversity Trust + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +package org.genesys2.server.service; + + +public interface EasySMTA { + EasySMTAUserData getUserData(String emailAddress); + + /** + * Data returned by Easy-SMTA + * + * @author matijaobreza + */ + public static class EasySMTAUserData { + private String pid; + private String firstName; + private String lastName; + private String email; + private String organization; + private String countryIsoCode3; + private String address; + private String shipAddress; + private String shipCountry; + + public EasySMTAUserData() { + } + + public EasySMTAUserData(String pid, String firstName, String lastName, String email, String organization, String countryIsoCode3, String address, + String shipAddress, String shipCountry) { + this.pid = pid; + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + this.organization = organization; + this.countryIsoCode3 = countryIsoCode3; + this.address = address; + this.shipAddress = shipAddress; + this.shipCountry = shipCountry; + } + + @Override + public String toString() { + return "ITPGRFA.PID email=" + email + " first=" + firstName + " last=" + lastName + ", org=" + organization + ", country=" + countryIsoCode3; + } + + public String getPid() { + return pid; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public String getEmail() { + return email; + } + + public String getOrganization() { + return organization; + } + + public String getCountryIsoCode3() { + return countryIsoCode3; + } + + public String getAddress() { + return address; + } + + public String getShipAddress() { + return shipAddress; + } + + public String getShipCountry() { + return shipCountry; + } + } + +} diff --git a/src/main/java/org/genesys2/server/service/impl/EasySMTAConnector.java b/src/main/java/org/genesys2/server/service/impl/EasySMTAConnector.java index 3bb584387..f08b2e0e3 100644 --- a/src/main/java/org/genesys2/server/service/impl/EasySMTAConnector.java +++ b/src/main/java/org/genesys2/server/service/impl/EasySMTAConnector.java @@ -34,14 +34,17 @@ import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; +import org.genesys2.server.service.EasySMTA; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +@Profile("aws") @Component -public class EasySMTAConnector { +public class EasySMTAConnector implements EasySMTA { private static final Log LOG = LogFactory.getLog(EasySMTAConnector.class); @Value("${itpgrfa.easysmta.url}") @@ -53,7 +56,8 @@ public class EasySMTAConnector { @Value("${itpgrfa.easysmta.password}") private String serverPassword; - public EasySMTAUserData getUserData(String emailAddress) { + @Override + public EasySMTA.EasySMTAUserData getUserData(String emailAddress) { final HttpClient httpclient = new DefaultHttpClient(); LOG.info("Checking EasySMTA at " + serverUrl + " for email: " + emailAddress); @@ -97,7 +101,7 @@ public class EasySMTAConnector { } else { - return new EasySMTAUserData(tree.get("pid").asText(), tree.get("name").asText(), tree.get("surname").asText(), tree.get("email").asText(), + return new EasySMTA.EasySMTAUserData(tree.get("pid").asText(), tree.get("name").asText(), tree.get("surname").asText(), tree.get("email").asText(), tree.get("orgName").asText(), tree.get("country").asText(), tree.get("address").asText(), tree.get("shipAddress").asText(), tree .get("shipCountry").asText()); } @@ -115,75 +119,4 @@ public class EasySMTAConnector { return null; } - - /** - * Data returned by Easy-SMTA - * - * @author matijaobreza - */ - public static class EasySMTAUserData { - private String pid; - private String firstName; - private String lastName; - private String email; - private String organization; - private String countryIsoCode3; - private String address; - private String shipAddress; - private String shipCountry; - - public EasySMTAUserData(String pid, String firstName, String lastName, String email, String organization, String countryIsoCode3, String address, - String shipAddress, String shipCountry) { - this.pid = pid; - this.firstName = firstName; - this.lastName = lastName; - this.email = email; - this.organization = organization; - this.countryIsoCode3 = countryIsoCode3; - this.address = address; - this.shipAddress = shipAddress; - this.shipCountry = shipCountry; - } - - @Override - public String toString() { - return "ITPGRFA.PID email=" + email + " first=" + firstName + " last=" + lastName + ", org=" + organization + ", country=" + countryIsoCode3; - } - - public String getPid() { - return pid; - } - - public String getFirstName() { - return firstName; - } - - public String getLastName() { - return lastName; - } - - public String getEmail() { - return email; - } - - public String getOrganization() { - return organization; - } - - public String getCountryIsoCode3() { - return countryIsoCode3; - } - - public String getAddress() { - return address; - } - - public String getShipAddress() { - return shipAddress; - } - - public String getShipCountry() { - return shipCountry; - } - } } diff --git a/src/main/java/org/genesys2/server/service/impl/EasySMTAMockConnector.java b/src/main/java/org/genesys2/server/service/impl/EasySMTAMockConnector.java new file mode 100644 index 000000000..edf8ccd8f --- /dev/null +++ b/src/main/java/org/genesys2/server/service/impl/EasySMTAMockConnector.java @@ -0,0 +1,46 @@ +/** + * Copyright 2014 Global Crop Diversity Trust + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +package org.genesys2.server.service.impl; + +import org.apache.commons.lang.RandomStringUtils; +import org.apache.commons.lang.math.RandomUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.genesys2.server.service.EasySMTA; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Profile({ "dev", "cacher" }) +@Component +public class EasySMTAMockConnector implements EasySMTA { + private static final Log LOG = LogFactory.getLog(EasySMTAMockConnector.class); + + @Override + public EasySMTA.EasySMTAUserData getUserData(String emailAddress) { + + LOG.info("Mock EasySMTA connector for email: " + emailAddress); + + if (RandomUtils.nextBoolean()) { + LOG.info("Valid mock EasySMTA check"); + return new EasySMTA.EasySMTAUserData(RandomStringUtils.randomAlphanumeric(6), "Name", "Surname", emailAddress, "Organization", "DEU", "Address 1", + "Address 2", "DEU"); + } else { + LOG.info("Invalid mock EasySMTA check"); + return null; + } + } +} -- GitLab