### Question:
DnsMessage { @Override public String toString() { if (toStringCache != null) return toStringCache; StringBuilder sb = new StringBuilder("DnsMessage"); asBuilder().writeToStringBuilder(sb); toStringCache = sb.toString(); return toStringCache; } protected  DnsMessage(Builder builder);  DnsMessage(byte[] data); private  DnsMessage(DnsMessage message); byte[] toArray(); DatagramPacket asDatagram(InetAddress address, int port); void writeTo(OutputStream outputStream); void writeTo(OutputStream outputStream, boolean writeLength); ByteBuffer getInByteBuffer(); Question getQuestion(); List<Question> copyQuestions(); List<Record<? extends Data>> copyAnswers(); List<Record<? extends Data>> copyAuthority(); Edns getEdns(); @SuppressWarnings("unchecked") Record<OPT> getOptPseudoRecord(); boolean isDnssecOk(); @Override String toString(); String asTerminalOutput(); Set<D> getAnswersFor(Question q); long getAnswersMinTtl(); Builder asBuilder(); DnsMessage asNormalizedVersion(); Builder getResponseBuilder(RESPONSE_CODE responseCode); @Override int hashCode(); List<Record<D>> filterAnswerSectionBy(Class<D> type); List<Record<D>> filterAuthoritySectionBy(Class<D> type); List<Record<D>> filterAdditionalSectionBy(Class<D> type); Record<D> getFirstOfTypeFromAnswerSection(Class<D> type); Record<D> getFirstOfTypeFromAuthoritySection(Class<D> type); Record<D> getFirstOfTypeFromAdditionalSection(Class<D> type); @Override boolean equals(Object other); static Builder builder(); final int id; final OPCODE opcode; final RESPONSE_CODE responseCode; final boolean qr; final boolean authoritativeAnswer; final boolean truncated; final boolean recursionDesired; final boolean recursionAvailable; final boolean authenticData; final boolean checkingDisabled; final List<Question> questions; final List<Record<? extends Data>> answerSection; final List<Record<? extends Data>> authoritySection; final List<Record<? extends Data>> additionalSection; final int optRrPosition; final long receiveTimestamp; }### Answer:
@Test public void testALookup() throws Exception { DnsMessage m = getMessageFromResource("sun-a"); assertFalse(m.authoritativeAnswer); List<Record<? extends Data>> answers = m.answerSection; assertEquals(2, answers.size()); Record<? extends Data> cname = answers.get(0); Record<? extends Data> a = answers.get(1); assertTrue(cname.getPayload() instanceof RRWithTarget); assertEquals(TYPE.CNAME, cname.getPayload().getType()); assertCsEquals("legacy-sun.oraclegha.com", ((RRWithTarget) cname.getPayload()).target); assertCsEquals("legacy-sun.oraclegha.com", a.name); assertTrue(a.getPayload() instanceof A); assertEquals(TYPE.A, a.getPayload().getType()); assertCsEquals("156.151.59.35", a.getPayload().toString()); }
@Test public void testAAAALookup() throws Exception { DnsMessage m = getMessageFromResource("google-aaaa"); assertFalse(m.authoritativeAnswer); List<Record<? extends Data>> answers = m.answerSection; assertEquals(1, answers.size()); Record<? extends Data> answer = answers.get(0); assertCsEquals("google.com", answer.name); assertTrue(answer.getPayload() instanceof AAAA); assertEquals(TYPE.AAAA, answer.getPayload().getType()); assertCsEquals("2a00:1450:400c:c02:0:0:0:8a", answer.getPayload().toString()); }