• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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  *     * Neither the name of Google LLC nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 package com.google.api.gax.rpc;
31 
32 import com.google.api.gax.rpc.StatusCode.Code;
33 import com.google.api.gax.rpc.testing.FakeStatusCode;
34 import com.google.common.truth.Truth;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.JUnit4;
38 
39 @RunWith(JUnit4.class)
40 public class ApiExceptionFactoryTest {
41 
42   @Test
cancelled()43   public void cancelled() {
44     Truth.assertThat(createException(Code.CANCELLED)).isInstanceOf(CancelledException.class);
45     Truth.assertThat(createExceptionWithMessage(Code.CANCELLED))
46         .isInstanceOf(CancelledException.class);
47   }
48 
49   @Test
notFound()50   public void notFound() {
51     Truth.assertThat(createException(Code.NOT_FOUND)).isInstanceOf(NotFoundException.class);
52     Truth.assertThat(createExceptionWithMessage(Code.NOT_FOUND))
53         .isInstanceOf(NotFoundException.class);
54   }
55 
56   @Test
unknown()57   public void unknown() {
58     Truth.assertThat(createException(Code.UNKNOWN)).isInstanceOf(UnknownException.class);
59     Truth.assertThat(createExceptionWithMessage(Code.UNKNOWN)).isInstanceOf(UnknownException.class);
60   }
61 
62   @Test
invalidArgument()63   public void invalidArgument() {
64     Truth.assertThat(createException(Code.INVALID_ARGUMENT))
65         .isInstanceOf(InvalidArgumentException.class);
66     Truth.assertThat(createExceptionWithMessage(Code.INVALID_ARGUMENT))
67         .isInstanceOf(InvalidArgumentException.class);
68   }
69 
70   @Test
deadlineExceeded()71   public void deadlineExceeded() {
72     Truth.assertThat(createException(Code.DEADLINE_EXCEEDED))
73         .isInstanceOf(DeadlineExceededException.class);
74     Truth.assertThat(createExceptionWithMessage(Code.DEADLINE_EXCEEDED))
75         .isInstanceOf(DeadlineExceededException.class);
76   }
77 
78   @Test
alreadyExists()79   public void alreadyExists() {
80     Truth.assertThat(createException(Code.ALREADY_EXISTS))
81         .isInstanceOf(AlreadyExistsException.class);
82     Truth.assertThat(createExceptionWithMessage(Code.ALREADY_EXISTS))
83         .isInstanceOf(AlreadyExistsException.class);
84   }
85 
86   @Test
permissionDenied()87   public void permissionDenied() {
88     Truth.assertThat(createException(Code.PERMISSION_DENIED))
89         .isInstanceOf(PermissionDeniedException.class);
90     Truth.assertThat(createExceptionWithMessage(Code.PERMISSION_DENIED))
91         .isInstanceOf(PermissionDeniedException.class);
92   }
93 
94   @Test
resourceExhausted()95   public void resourceExhausted() {
96     Truth.assertThat(createException(Code.RESOURCE_EXHAUSTED))
97         .isInstanceOf(ResourceExhaustedException.class);
98     Truth.assertThat(createExceptionWithMessage(Code.RESOURCE_EXHAUSTED))
99         .isInstanceOf(ResourceExhaustedException.class);
100   }
101 
102   @Test
failedPrecondition()103   public void failedPrecondition() {
104     Truth.assertThat(createException(Code.FAILED_PRECONDITION))
105         .isInstanceOf(FailedPreconditionException.class);
106     Truth.assertThat(createExceptionWithMessage(Code.FAILED_PRECONDITION))
107         .isInstanceOf(FailedPreconditionException.class);
108   }
109 
110   @Test
aborted()111   public void aborted() {
112     Truth.assertThat(createException(Code.ABORTED)).isInstanceOf(AbortedException.class);
113     Truth.assertThat(createExceptionWithMessage(Code.ABORTED)).isInstanceOf(AbortedException.class);
114   }
115 
116   @Test
outOfRange()117   public void outOfRange() {
118     Truth.assertThat(createException(Code.OUT_OF_RANGE)).isInstanceOf(OutOfRangeException.class);
119     Truth.assertThat(createExceptionWithMessage(Code.OUT_OF_RANGE))
120         .isInstanceOf(OutOfRangeException.class);
121   }
122 
123   @Test
internal()124   public void internal() {
125     Truth.assertThat(createException(Code.INTERNAL)).isInstanceOf(InternalException.class);
126     Truth.assertThat(createExceptionWithMessage(Code.INTERNAL))
127         .isInstanceOf(InternalException.class);
128   }
129 
130   @Test
unavailable()131   public void unavailable() {
132     Truth.assertThat(createException(Code.UNAVAILABLE)).isInstanceOf(UnavailableException.class);
133     Truth.assertThat(createExceptionWithMessage(Code.UNAVAILABLE))
134         .isInstanceOf(UnavailableException.class);
135   }
136 
137   @Test
dataLoss()138   public void dataLoss() {
139     Truth.assertThat(createException(Code.DATA_LOSS)).isInstanceOf(DataLossException.class);
140     Truth.assertThat(createExceptionWithMessage(Code.DATA_LOSS))
141         .isInstanceOf(DataLossException.class);
142   }
143 
144   @Test
unauthenticated()145   public void unauthenticated() {
146     Truth.assertThat(createException(Code.UNAUTHENTICATED))
147         .isInstanceOf(UnauthenticatedException.class);
148     Truth.assertThat(createExceptionWithMessage(Code.UNAUTHENTICATED))
149         .isInstanceOf(UnauthenticatedException.class);
150   }
151 
152   @Test
unimplemented()153   public void unimplemented() {
154     Truth.assertThat(createException(Code.UNIMPLEMENTED))
155         .isInstanceOf(UnimplementedException.class);
156     Truth.assertThat(createExceptionWithMessage(Code.UNIMPLEMENTED))
157         .isInstanceOf(UnimplementedException.class);
158   }
159 
160   @Test
unknown_default()161   public void unknown_default() {
162     Truth.assertThat(createException(Code.OK)).isInstanceOf(UnknownException.class);
163     Truth.assertThat(createExceptionWithMessage(Code.OK)).isInstanceOf(UnknownException.class);
164   }
165 
createException(StatusCode.Code statusCode)166   private ApiException createException(StatusCode.Code statusCode) {
167     return ApiExceptionFactory.createException(
168         new RuntimeException(), FakeStatusCode.of(statusCode), false);
169   }
170 
createExceptionWithMessage(StatusCode.Code statusCode)171   private ApiException createExceptionWithMessage(StatusCode.Code statusCode) {
172     return ApiExceptionFactory.createException(
173         "message", new RuntimeException(), FakeStatusCode.of(statusCode), false);
174   }
175 }
176