SerializeRRS implements Function<Object, String> { @Override public String apply(Object in) { ResourceRecordSet rrs = ResourceRecordSet.class.cast(checkNotNull(in, "rrs")); StringBuilder builder = new StringBuilder().append("<ResourceRecordSet>"); builder.append("<Name>").append(rrs.getName()).append("</Name>"); builder.append("<Type>").append(rrs.getType()).append("</Type>"); if (rrs instanceof RecordSubset) { String id = RecordSubset.class.cast(rrs).getId(); builder.append("<SetIdentifier>").append(id).append("</SetIdentifier>"); } if (rrs instanceof Weighted) builder.append("<Weight>").append(Weighted.class.cast(rrs).getWeight()).append("</Weight>"); if (rrs instanceof Latency) builder.append("<Region>").append(Latency.class.cast(rrs).getRegion()).append("</Region>"); if (rrs.getAliasTarget().isPresent()) { builder.append("<AliasTarget>"); builder.append("<HostedZoneId>").append(rrs.getAliasTarget().get().getZoneId()).append("</HostedZoneId>"); builder.append("<DNSName>").append(rrs.getAliasTarget().get().getDNSName()).append("</DNSName>"); builder.append("</AliasTarget>"); } else { builder.append("<TTL>").append(rrs.getTTL().or(300)).append("</TTL>"); builder.append("<ResourceRecords>"); for (String record : rrs.getValues()) builder.append("<ResourceRecord>").append("<Value>").append(record).append("</Value>") .append("</ResourceRecord>"); builder.append("</ResourceRecords>"); } return builder.append("</ResourceRecordSet>").toString(); }  @Override String apply(Object in);  }
@Test void roundRobinRRSetToXML() { assertEquals( new SerializeRRS().apply(ResourceRecordSet.builder() .name("dom1.foo.com.") .type("A") .add("1.2.3.4") .add("5.6.7.8").build()), "<ResourceRecordSet><Name>dom1.foo.com.</Name><Type>A</Type><TTL>300</TTL><ResourceRecords><ResourceRecord><Value>1.2.3.4</Value></ResourceRecord><ResourceRecord><Value>5.6.7.8</Value></ResourceRecord></ResourceRecords></ResourceRecordSet>"); }
@Test void roundWeightedRRSetToXML() { assertEquals(new SerializeRRS().apply(Weighted.builder() .id("dom1") .weight(1) .name("dom.foo.com.") .type("CNAME") .add("dom1.foo.com.").build()), "<ResourceRecordSet><Name>dom.foo.com.</Name><Type>CNAME</Type><SetIdentifier>dom1</SetIdentifier><Weight>1</Weight><TTL>300</TTL><ResourceRecords><ResourceRecord><Value>dom1.foo.com.</Value></ResourceRecord></ResourceRecords></ResourceRecordSet>"); }