1 /* 2 * Copyright 2021 Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * * Neither the name of Google LLC nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package com.google.auth.oauth2; 33 34 import static org.junit.Assert.assertEquals; 35 import static org.junit.Assert.assertNull; 36 37 import com.google.auth.TestUtils; 38 import java.io.IOException; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.junit.runners.JUnit4; 42 43 /** Tests for {@link OAuthException}. */ 44 @RunWith(JUnit4.class) 45 public final class OAuthExceptionTest { 46 47 private static final String FULL_MESSAGE_FORMAT = "Error code %s: %s - %s"; 48 private static final String ERROR_DESCRIPTION_FORMAT = "Error code %s: %s"; 49 private static final String BASE_MESSAGE_FORMAT = "Error code %s"; 50 51 @Test getMessage_fullFormat()52 public void getMessage_fullFormat() { 53 OAuthException e = new OAuthException("errorCode", "errorDescription", "errorUri"); 54 55 assertEquals("errorCode", e.getErrorCode()); 56 assertEquals("errorDescription", e.getErrorDescription()); 57 assertEquals("errorUri", e.getErrorUri()); 58 59 String expectedMessage = 60 String.format(FULL_MESSAGE_FORMAT, "errorCode", "errorDescription", "errorUri"); 61 assertEquals(expectedMessage, e.getMessage()); 62 } 63 64 @Test getMessage_descriptionFormat()65 public void getMessage_descriptionFormat() { 66 OAuthException e = new OAuthException("errorCode", "errorDescription", /* errorUri= */ null); 67 68 assertEquals("errorCode", e.getErrorCode()); 69 assertEquals("errorDescription", e.getErrorDescription()); 70 assertNull(e.getErrorUri()); 71 72 String expectedMessage = 73 String.format(ERROR_DESCRIPTION_FORMAT, "errorCode", "errorDescription"); 74 assertEquals(expectedMessage, e.getMessage()); 75 } 76 77 @Test getMessage_baseFormat()78 public void getMessage_baseFormat() { 79 OAuthException e = 80 new OAuthException("errorCode", /* errorDescription= */ null, /* errorUri= */ null); 81 82 assertEquals("errorCode", e.getErrorCode()); 83 assertNull(e.getErrorDescription()); 84 assertNull(e.getErrorUri()); 85 86 String expectedMessage = String.format(BASE_MESSAGE_FORMAT, "errorCode"); 87 assertEquals(expectedMessage, e.getMessage()); 88 } 89 90 @Test createFromHttpResponseException()91 public void createFromHttpResponseException() throws IOException { 92 OAuthException e = 93 OAuthException.createFromHttpResponseException( 94 TestUtils.buildHttpResponseException("errorCode", "errorDescription", "errorUri")); 95 96 assertEquals("errorCode", e.getErrorCode()); 97 assertEquals("errorDescription", e.getErrorDescription()); 98 assertEquals("errorUri", e.getErrorUri()); 99 100 String expectedMessage = 101 String.format(FULL_MESSAGE_FORMAT, "errorCode", "errorDescription", "errorUri"); 102 assertEquals(expectedMessage, e.getMessage()); 103 } 104 105 @Test createFromHttpResponseException_descriptionFormat()106 public void createFromHttpResponseException_descriptionFormat() throws IOException { 107 OAuthException e = 108 OAuthException.createFromHttpResponseException( 109 TestUtils.buildHttpResponseException( 110 "errorCode", "errorDescription", /* errorUri= */ null)); 111 112 assertEquals("errorCode", e.getErrorCode()); 113 assertEquals("errorDescription", e.getErrorDescription()); 114 assertNull(e.getErrorUri()); 115 116 String expectedMessage = 117 String.format(ERROR_DESCRIPTION_FORMAT, "errorCode", "errorDescription"); 118 assertEquals(expectedMessage, e.getMessage()); 119 } 120 121 @Test createFromHttpResponseException_baseFormat()122 public void createFromHttpResponseException_baseFormat() throws IOException { 123 OAuthException e = 124 OAuthException.createFromHttpResponseException( 125 TestUtils.buildHttpResponseException( 126 "errorCode", /* errorDescription= */ null, /* errorUri= */ null)); 127 128 assertEquals("errorCode", e.getErrorCode()); 129 assertNull(e.getErrorDescription()); 130 assertNull(e.getErrorUri()); 131 132 String expectedMessage = String.format(BASE_MESSAGE_FORMAT, "errorCode"); 133 assertEquals(expectedMessage, e.getMessage()); 134 } 135 } 136