001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2016 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.exception;
015
016import static org.assertj.core.api.Assertions.assertThat;
017import static org.shredzone.acme4j.toolbox.TestUtils.createProblem;
018
019import java.net.MalformedURLException;
020import java.net.URI;
021import java.net.URL;
022
023import org.junit.jupiter.api.Test;
024
025/**
026 * Unit tests for {@link AcmeUserActionRequiredException}.
027 */
028public class AcmeUserActionRequiredExceptionTest {
029
030    /**
031     * Test that parameters are correctly returned.
032     */
033    @Test
034    public void testAcmeUserActionRequiredException() throws MalformedURLException {
035        var type = URI.create("urn:ietf:params:acme:error:userActionRequired");
036        var detail = "Accept new TOS";
037        var tosUri = URI.create("http://example.com/agreement.pdf");
038        var instanceUrl = new URL("http://example.com/howToAgree.html");
039
040        var problem = createProblem(type, detail, instanceUrl);
041
042        var ex = new AcmeUserActionRequiredException(problem, tosUri);
043
044        assertThat(ex.getType()).isEqualTo(type);
045        assertThat(ex.getMessage()).isEqualTo(detail);
046        assertThat(ex.getTermsOfServiceUri().orElseThrow()).isEqualTo(tosUri);
047        assertThat(ex.getInstance()).isEqualTo(instanceUrl);
048        assertThat(ex.toString()).isEqualTo("Please visit " + instanceUrl + " - details: " + detail);
049    }
050
051    /**
052     * Test that optional parameters are null-safe.
053     */
054    @Test
055    public void testNullAcmeUserActionRequiredException() throws MalformedURLException {
056        var type = URI.create("urn:ietf:params:acme:error:userActionRequired");
057        var detail = "Call our service";
058        var instanceUrl = new URL("http://example.com/howToContactUs.html");
059
060        var problem = createProblem(type, detail, instanceUrl);
061
062        var ex = new AcmeUserActionRequiredException(problem, null);
063
064        assertThat(ex.getType()).isEqualTo(type);
065        assertThat(ex.getMessage()).isEqualTo(detail);
066        assertThat(ex.getTermsOfServiceUri()).isEmpty();
067        assertThat(ex.getInstance()).isEqualTo(instanceUrl);
068        assertThat(ex.toString()).isEqualTo("Please visit " + instanceUrl + " - details: " + detail);
069    }
070
071}