|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Red Hat, Inc. |
| 3 | + * Distributed under license by Red Hat, Inc. All rights reserved. |
| 4 | + * This program is made available under the terms of the |
| 5 | + * Eclipse Public License v2.0 which accompanies this distribution, |
| 6 | + * and is available at http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat, Inc. - initial API and implementation |
| 10 | + ******************************************************************************/ |
| 11 | +package org.jboss.tools.intellij.openshift.utils; |
| 12 | + |
| 13 | +import io.fabric8.kubernetes.client.KubernetesClientException; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.net.HttpURLConnection; |
| 18 | + |
| 19 | +import static org.fest.assertions.Assertions.assertThat; |
| 20 | + |
| 21 | +public class KubernetesClientExceptionUtilsTest { |
| 22 | + |
| 23 | + private final KubernetesClientException notFoundClientException = new KubernetesClientException("Dagobah was not found", HttpURLConnection.HTTP_NOT_FOUND, null); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void should_be_forbidden() { |
| 27 | + KubernetesClientException e = new KubernetesClientException("it's forbidden to contradict the emperor", HttpURLConnection.HTTP_FORBIDDEN, null); |
| 28 | + assertThat(KubernetesClientExceptionUtils.isForbidden(e)).isTrue(); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void should_not_be_forbidden() { |
| 33 | + assertThat(KubernetesClientExceptionUtils.isForbidden(notFoundClientException)).isFalse(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void should_be_unauthorized() { |
| 38 | + KubernetesClientException e = new KubernetesClientException("yoda is unauthorized to enter the jedi temple", HttpURLConnection.HTTP_UNAUTHORIZED, null); |
| 39 | + assertThat(KubernetesClientExceptionUtils.isUnauthorized(e)).isTrue(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void should_not_be_unauthorized() { |
| 44 | + assertThat(KubernetesClientExceptionUtils.isForbidden(notFoundClientException)).isFalse(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void should_be_host_down() { |
| 49 | + IOException hostDownException = new IOException("Host is down"); |
| 50 | + KubernetesClientException e = new KubernetesClientException("the rebel base is down", hostDownException); |
| 51 | + assertThat(KubernetesClientExceptionUtils.isHostDown(e)).isTrue(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void should_not_be_host_down() { |
| 56 | + IOException hostRunningException = new IOException("Host is up and running"); |
| 57 | + KubernetesClientException e = new KubernetesClientException("the rebel base is up and running", hostRunningException); |
| 58 | + assertThat(KubernetesClientExceptionUtils.isHostDown(e)).isFalse(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void should_not_be_host_down_null_cause() { |
| 63 | + KubernetesClientException e = new KubernetesClientException("the rebel base is up and running", null); |
| 64 | + assertThat(KubernetesClientExceptionUtils.isHostDown(e)).isFalse(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void should_not_be_host_down_different_cause() { |
| 69 | + KubernetesClientException e = new KubernetesClientException("the rebel base is up and running", notFoundClientException); |
| 70 | + assertThat(KubernetesClientExceptionUtils.isHostDown(e)).isFalse(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void should_be_connection_reset() { |
| 75 | + IOException connectionResetException = new IOException("Is always fear that the connection reset."); |
| 76 | + KubernetesClientException e = new KubernetesClientException("the connection to the rebel base was cut down", connectionResetException); |
| 77 | + assertThat(KubernetesClientExceptionUtils.isConnectionReset(e)).isTrue(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void should_not_be_connection_reset() { |
| 82 | + IOException connectionEstablishedException = new IOException("Connection is established"); |
| 83 | + KubernetesClientException e = new KubernetesClientException("the connection to the rebel base is stable", connectionEstablishedException); |
| 84 | + assertThat(KubernetesClientExceptionUtils.isConnectionReset(e)).isFalse(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void should_not_be_connection_reset_null_cause() { |
| 89 | + KubernetesClientException e = new KubernetesClientException("the connection to the rebel base is stable", null); |
| 90 | + assertThat(KubernetesClientExceptionUtils.isConnectionReset(e)).isFalse(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void should_not_be_connection_reset_different_cause() { |
| 95 | + KubernetesClientException e = new KubernetesClientException("the connection to the rebel base is stable", notFoundClientException); |
| 96 | + assertThat(KubernetesClientExceptionUtils.isConnectionReset(e)).isFalse(); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void should_be_could_not_connect() { |
| 101 | + IOException couldNotConnectException = new IOException("Alarming: failed to connect to Dagobah"); |
| 102 | + KubernetesClientException e = new KubernetesClientException("Operation: connection to the rebel base failed", couldNotConnectException); |
| 103 | + assertThat(KubernetesClientExceptionUtils.isCouldNotConnect(e)).isTrue(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void should_not_be_could_not_connect_null_cause() { |
| 108 | + KubernetesClientException e = new KubernetesClientException("Operation: connection to the rebel base failed", null); |
| 109 | + assertThat(KubernetesClientExceptionUtils.isCouldNotConnect(e)).isFalse(); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void should_not_be_could_not_connect_different_cause() { |
| 114 | + KubernetesClientException e = new KubernetesClientException("Operation: connection to the rebel base failed", notFoundClientException); |
| 115 | + assertThat(KubernetesClientExceptionUtils.isCouldNotConnect(e)).isFalse(); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments