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;
018import static org.shredzone.acme4j.toolbox.TestUtils.url;
019
020import java.net.URI;
021import java.time.Duration;
022import java.time.Instant;
023import java.util.Arrays;
024
025import org.junit.jupiter.api.Test;
026
027/**
028 * Unit tests for {@link AcmeRateLimitedException}.
029 */
030public class AcmeRateLimitedExceptionTest {
031
032    /**
033     * Test that parameters are correctly returned.
034     */
035    @Test
036    public void testAcmeRateLimitedException() {
037        var type = URI.create("urn:ietf:params:acme:error:rateLimited");
038        var detail = "Too many requests per minute";
039        var retryAfter = Instant.now().plus(Duration.ofMinutes(1));
040        var documents = Arrays.asList(
041                        url("http://example.com/doc1.html"),
042                        url("http://example.com/doc2.html"));
043
044        var problem = createProblem(type, detail, null);
045
046        var ex = new AcmeRateLimitedException(problem, retryAfter, documents);
047
048        assertThat(ex.getType()).isEqualTo(type);
049        assertThat(ex.getMessage()).isEqualTo(detail);
050        assertThat(ex.getRetryAfter().orElseThrow()).isEqualTo(retryAfter);
051        assertThat(ex.getDocuments()).containsAll(documents);
052    }
053
054    /**
055     * Test that optional parameters are null-safe.
056     */
057    @Test
058    public void testNullAcmeRateLimitedException() {
059        var type = URI.create("urn:ietf:params:acme:error:rateLimited");
060        var detail = "Too many requests per minute";
061
062        var problem = createProblem(type, detail, null);
063
064        var ex = new AcmeRateLimitedException(problem, null, null);
065
066        assertThat(ex.getType()).isEqualTo(type);
067        assertThat(ex.getMessage()).isEqualTo(detail);
068        assertThat(ex.getRetryAfter()).isEmpty();
069        assertThat(ex.getDocuments()).isEmpty();
070    }
071
072}