001/* 002 * acme4j - Java ACME client 003 * 004 * Copyright (C) 2017 Richard "Shred" Körber 005 * http://acme4j.shredzone.org 006 * 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 013 */ 014package org.shredzone.acme4j; 015 016import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; 017import static org.assertj.core.api.Assertions.assertThat; 018import static org.shredzone.acme4j.toolbox.TestUtils.url; 019 020import java.net.URI; 021 022import org.assertj.core.api.AutoCloseableSoftAssertions; 023import org.junit.jupiter.api.Test; 024import org.shredzone.acme4j.toolbox.JSONBuilder; 025import org.shredzone.acme4j.toolbox.TestUtils; 026 027/** 028 * Unit tests for {@link Problem}. 029 */ 030public class ProblemTest { 031 032 @Test 033 public void testProblem() { 034 var baseUrl = url("https://example.com/acme/1"); 035 var original = TestUtils.getJSON("problem"); 036 037 var problem = new Problem(original, baseUrl); 038 039 assertThatJson(problem.asJSON().toString()).isEqualTo(original.toString()); 040 041 try (var softly = new AutoCloseableSoftAssertions()) { 042 softly.assertThat(problem.getType()).isEqualTo(URI.create("urn:ietf:params:acme:error:malformed")); 043 softly.assertThat(problem.getTitle().orElseThrow()) 044 .isEqualTo("Some of the identifiers requested were rejected"); 045 softly.assertThat(problem.getDetail().orElseThrow()) 046 .isEqualTo("Identifier \"abc12_\" is malformed"); 047 softly.assertThat(problem.getInstance().orElseThrow()) 048 .isEqualTo(URI.create("https://example.com/documents/error.html")); 049 softly.assertThat(problem.getIdentifier()).isEmpty(); 050 softly.assertThat(problem.toString()).isEqualTo( 051 "Identifier \"abc12_\" is malformed (" 052 + "Invalid underscore in DNS name \"_example.com\" ‒ " 053 + "This CA will not issue for \"example.net\")"); 054 055 var subs = problem.getSubProblems(); 056 softly.assertThat(subs).isNotNull().hasSize(2); 057 058 var p1 = subs.get(0); 059 softly.assertThat(p1.getType()).isEqualTo(URI.create("urn:ietf:params:acme:error:malformed")); 060 softly.assertThat(p1.getTitle()).isEmpty(); 061 softly.assertThat(p1.getDetail().orElseThrow()) 062 .isEqualTo("Invalid underscore in DNS name \"_example.com\""); 063 softly.assertThat(p1.getIdentifier().orElseThrow().getDomain()).isEqualTo("_example.com"); 064 softly.assertThat(p1.toString()).isEqualTo("Invalid underscore in DNS name \"_example.com\""); 065 066 var p2 = subs.get(1); 067 softly.assertThat(p2.getType()).isEqualTo(URI.create("urn:ietf:params:acme:error:rejectedIdentifier")); 068 softly.assertThat(p2.getTitle()).isEmpty(); 069 softly.assertThat(p2.getDetail().orElseThrow()) 070 .isEqualTo("This CA will not issue for \"example.net\""); 071 softly.assertThat(p2.getIdentifier().orElseThrow().getDomain()).isEqualTo("example.net"); 072 softly.assertThat(p2.toString()).isEqualTo("This CA will not issue for \"example.net\""); 073 } 074 } 075 076 /** 077 * Test that {@link Problem#toString()} always returns the most specific message. 078 */ 079 @Test 080 public void testToString() { 081 var baseUrl = url("https://example.com/acme/1"); 082 var typeUri = URI.create("urn:ietf:params:acme:error:malformed"); 083 084 var jb = new JSONBuilder(); 085 086 jb.put("type", typeUri); 087 var p1 = new Problem(jb.toJSON(), baseUrl); 088 assertThat(p1.toString()).isEqualTo(typeUri.toString()); 089 090 jb.put("title", "Some of the identifiers requested were rejected"); 091 var p2 = new Problem(jb.toJSON(), baseUrl); 092 assertThat(p2.toString()).isEqualTo("Some of the identifiers requested were rejected"); 093 094 jb.put("detail", "Identifier \"abc12_\" is malformed"); 095 var p3 = new Problem(jb.toJSON(), baseUrl); 096 assertThat(p3.toString()).isEqualTo("Identifier \"abc12_\" is malformed"); 097 } 098 099}