Permalink
Browse files

Support of hostAttr parsing in infodomain command response

  • Loading branch information...
sonaljain-github authored and Bin Chen committed Sep 20, 2018
1 parent e08d90c commit 3c2a48846be402764e46dbb3f26a86edda36e562
@@ -10,7 +10,7 @@
/**
* Use this to access domain object information as provided in an EPP domain
* info response compliant with RFC5730 and RFC5731. Such a service element is
* info response compliant with RFC5730 and RFC5731. Such a service element is
* sent by a compliant EPP server in response to a valid domain info command,
* implemented by the DomainInfoCommand class.
*
@@ -39,6 +39,13 @@
protected static final String DOM_CON_TECH_EXPR = DOM_CON_EXPR.replaceFirst("TYPE", "tech");
protected static final String DOM_CON_ADMIN_EXPR = DOM_CON_EXPR.replaceFirst("TYPE", "admin");
protected static final String DOM_CON_BILLING_EXPR = DOM_CON_EXPR.replaceFirst("TYPE", "billing");
private static final String DOMAIN_HOST_ATTR_COUNT_EXPR =
"count(" + DOM_INF_DATA_EXPR + "/domain:ns/domain:hostAttr)";
private static final String DOMAIN_ATTR_EXPR_IDX = DOM_INF_DATA_EXPR + "/domain:ns/domain:hostAttr[IDX]";
private static final String DOMAIN_TXT_EXPR = "/text()";
private static final String HOS_ADDR_IP_EXPR = "/@ip";
private static final String HOST_ATTR_IDX_EXPR = "/domain:hostAddr[IDX]";
private static final String HOST_ATTR_EXPR = "/domain:hostAddr";
private static final long serialVersionUID = -5948394715740177139L;
@@ -47,6 +54,7 @@
private String[] delHosts, subHosts;
private GregorianCalendar exDate;
private String exDateStr;
private Host[] nameserverHosts;
public DomainInfoResponse() {
super(StandardObjectType.DOMAIN);
@@ -128,6 +136,13 @@ public String getRegistrantID() {
return subHosts;
}
/**
* @return the nameserverHosts
*/
public Host[] getNameserverHosts() {
return nameserverHosts;
}
@Override
public void fromXML(XMLDocument xmlDoc) {
super.fromXML(xmlDoc);
@@ -145,6 +160,23 @@ public void fromXML(XMLDocument xmlDoc) {
exDate = EPPDateFormatter.fromXSDateTime(exDateStr);
}
delHosts = xmlDoc.getNodeValues(DOM_NS_EXPR);
int attrCount = xmlDoc.getNodeCount(DOMAIN_HOST_ATTR_COUNT_EXPR);
nameserverHosts = new Host[attrCount];
for (int k = 0; k < attrCount; k++) {
String hostAttrExpr = ReceiveSE.replaceIndex(DOMAIN_ATTR_EXPR_IDX, k + 1);
String hostName = xmlDoc.getNodeValue(hostAttrExpr + "/domain:hostName" + DOMAIN_TXT_EXPR);
String hostAddrExpr = hostAttrExpr + HOST_ATTR_IDX_EXPR;
int addrCount = xmlDoc.getNodeCount("count(" + hostAttrExpr + HOST_ATTR_EXPR + ")");
InetAddress[] inetAddresses = new InetAddress[addrCount];
for (int i = 0; i < addrCount; i++) {
String qry = ReceiveSE.replaceIndex(hostAddrExpr, i + 1);
String addr = xmlDoc.getNodeValue(qry + DOMAIN_TXT_EXPR);
String version = xmlDoc.getNodeValue(qry + HOS_ADDR_IP_EXPR);
inetAddresses[i] = new InetAddress(IPVersion.value(version), addr);
}
nameserverHosts[k] = new Host(hostName, inetAddresses);
}
subHosts = xmlDoc.getNodeValues(DOM_HOST_EXPR);
techContacts = xmlDoc.getNodeValues(DOM_CON_TECH_EXPR);
@@ -173,6 +205,10 @@ public String toString() {
retval += "(nameserver-hosts = " + arrayToString(delHosts, ",") + ")";
}
if (nameserverHosts != null) {
retval += "(nameserver-hosts = " + arrayToString(nameserverHosts, ",") + ")";
}
if (techContacts != null) {
retval += "(tech-contacts = " + arrayToString(techContacts, ",") + ")";
}
@@ -188,4 +224,3 @@ public String toString() {
return retval;
}
}
@@ -1,5 +1,7 @@
package com.ausregistry.jtoolkit2.se;
import java.util.Arrays;
import org.w3c.dom.Element;
import com.ausregistry.jtoolkit2.xml.XMLWriter;
@@ -80,4 +82,16 @@ public Element appendToElement(XMLWriter xmlWriter, Element parent) {
}
return parent;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Host [" + (name != null ? "name=" + name + ", " : "")
+ (addresses != null ? "addresses=" + Arrays.toString(addresses) : "") + "]";
}
}
@@ -6,9 +6,9 @@
/**
* Host Internet addresses are configured and viewed via instances of this
* class. InetAddress instances may be supplied to the HostCreateCommand and
* class. InetAddress instances may be supplied to the HostCreateCommand and
* HostUpdateCommand (indirectly via HostAddRem) constructors in order to
* assign or remove Internet addresses to and from host objects. They are also
* assign or remove Internet addresses to and from host objects. They are also
* used to view Internet address information retrieved from a HostInfoResponse
* instance.
*/
@@ -56,5 +56,15 @@ public Element appendToElement(XMLWriter xmlWriter, Element parent) {
return xmlWriter.appendChild(parent, "addr", textRep, "ip", getVersion());
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "InetAddress [" + (version != null ? "version=" + version + ", " : "")
+ (textRep != null ? "textRep=" + textRep : "") + "]";
}
}
@@ -1,10 +1,14 @@
package com.ausregistry.jtoolkit2.se;
import static com.ausregistry.jtoolkit2.se.DomainInfoResponseTest.DomainInfoResponseBuilder.infoResponseBuilder;
import static com.ausregistry.jtoolkit2.test.infrastructure.HostMatcher.matchHost;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
@@ -191,23 +195,44 @@ public void testLdhOnlyGetName() throws Exception {
assertEquals("example.com", re.getUserFormName());
}
static class DomainInfoResponseBuilder {
@Test
public void testGetNameserversHostAttribute() throws Exception {
// Preparing host[] to compare with parsed response host array
InetAddress[] inetAddresses = new InetAddress[2];
inetAddresses[0] = new InetAddress("132.16.56.29");
inetAddresses[1] = new InetAddress(IPVersion.IPv6, "FE80:CD00:0:CDE:1257:0:211E:729C");
Host host1 = new Host("ns1.example.com.au", inetAddresses);
Host host2 = new Host("ns2.example.com.au");
XMLDocument doc = PARSER.parse(infoResponseBuilder("example.com.au").withHosts(host1, host2).build());
DomainInfoResponse response = new DomainInfoResponse();
response.fromXML(doc);
assertNull("hostObj is not response so it should be null", response.getNameservers());
Host[] hosts = response.getNameserverHosts();
assertNotNull(hosts);
assertThat(hosts, arrayContaining(matchHost(host1), matchHost(host2)));
}
static final class DomainInfoResponseBuilder {
private final String domainName;
private boolean isIdn = false;
private String userForm;
private String canonicalForm;
private String variantUserForm;
private String variantDnsForm;
static DomainInfoResponseBuilder infoResponseBuilder(String domainName) {
return new DomainInfoResponseBuilder(domainName);
}
private Host[] nameservers;
private DomainInfoResponseBuilder(String domainName) {
this.domainName = domainName;
}
static DomainInfoResponseBuilder infoResponseBuilder(String domainName) {
return new DomainInfoResponseBuilder(domainName);
}
private DomainInfoResponseBuilder withIdn(String userForm, String canonicalName) {
this.userForm = userForm;
this.canonicalForm = canonicalName;
@@ -221,6 +246,11 @@ private DomainInfoResponseBuilder withVariant(String variantUserForm, String var
return this;
}
private DomainInfoResponseBuilder withHosts(Host... nameservers) {
this.nameservers = nameservers;
return this;
}
private String build() {
final StringBuilder result = new StringBuilder();
result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
@@ -240,8 +270,20 @@ private String build() {
result.append("<registrant>EXAMPLE</registrant>");
result.append("<contact type=\"tech\">EXAMPLE</contact>");
result.append("<ns>");
result.append("<hostObj>ns1.example.com.au</hostObj>");
result.append("<hostObj>ns2.example.com.au</hostObj>");
if (nameservers != null && nameservers.length > 0) {
for (Host nameserver : nameservers) {
result.append("<hostAttr><hostName>").append(nameserver.getName()).append("</hostName>");
for (InetAddress address : nameserver.getAddresses()) {
result.append("<hostAddr ip=\"").append(address.getVersion()).append("\">")
.append(address.getTextRep()).append("</hostAddr>");
}
result.append("</hostAttr>");
}
} else {
result.append("<hostObj>ns1.example.com.au</hostObj>");
result.append("<hostObj>ns2.example.com.au</hostObj>");
}
result.append("</ns>");
result.append("<host>ns1.example.com.au</host>");
result.append("<host>ns2.exmaple.com.au</host>");
@@ -0,0 +1,39 @@
package com.ausregistry.jtoolkit2.test.infrastructure;
import static com.ausregistry.jtoolkit2.test.infrastructure.InetAddressMatcher.matchIpAddress;
import java.util.Arrays;
import com.ausregistry.jtoolkit2.se.Host;
import org.hamcrest.CustomTypeSafeMatcher;
public class HostMatcher extends CustomTypeSafeMatcher<Host> {
private final Host expectedHost;
public HostMatcher(Host expectedHost) {
super(String.format("a host object named [%s] with ip addresses %s", expectedHost.getName(),
Arrays.toString(expectedHost.getAddresses())));
this.expectedHost = expectedHost;
}
public static HostMatcher matchHost(Host expectedHost) {
return new HostMatcher(expectedHost);
}
@Override
protected boolean matchesSafely(Host item) {
if (!expectedHost.getName().equals(item.getName())) {
return false;
} else if (expectedHost.getAddresses().length != item.getAddresses().length) {
return false;
} else {
for (int i = 0; i < expectedHost.getAddresses().length; i++) {
if (!matchIpAddress(expectedHost.getAddresses()[i]).matchesSafely(item.getAddresses()[i])) {
return false;
}
}
}
return true;
}
}
@@ -0,0 +1,25 @@
package com.ausregistry.jtoolkit2.test.infrastructure;
import com.ausregistry.jtoolkit2.se.InetAddress;
import org.hamcrest.CustomTypeSafeMatcher;
public class InetAddressMatcher extends CustomTypeSafeMatcher<InetAddress> {
private final InetAddress expectedIpAddress;
public InetAddressMatcher(InetAddress expectedIpAddress) {
super(String.format("a type %s IP address [%s]", expectedIpAddress.getVersion(),
expectedIpAddress.getTextRep()));
this.expectedIpAddress = expectedIpAddress;
}
public static InetAddressMatcher matchIpAddress(InetAddress expectedIpAddress) {
return new InetAddressMatcher(expectedIpAddress);
}
@Override
protected boolean matchesSafely(InetAddress item) {
return expectedIpAddress.getVersion().equals(item.getVersion())
&& expectedIpAddress.getTextRep().equals(item.getTextRep());
}
}

0 comments on commit 3c2a488

Please sign in to comment.