| 
 | 1 | +/*  | 
 | 2 | + * Copyright (C) 2015 Red Hat, Inc.  | 
 | 3 | + *  | 
 | 4 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | + * you may not use this file except in compliance with the License.  | 
 | 6 | + * You may obtain a copy of the License at  | 
 | 7 | + *  | 
 | 8 | + *         http://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | + *  | 
 | 10 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 11 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | + * See the License for the specific language governing permissions and  | 
 | 14 | + * limitations under the License.  | 
 | 15 | + */  | 
 | 16 | + | 
 | 17 | +package io.fabric8.kubernetes.client.dsl.internal;  | 
 | 18 | + | 
 | 19 | +import io.fabric8.kubernetes.client.http.AsyncBody;  | 
 | 20 | +import io.fabric8.kubernetes.client.http.HttpClient;  | 
 | 21 | +import io.fabric8.kubernetes.client.http.HttpRequest;  | 
 | 22 | +import io.fabric8.kubernetes.client.http.HttpResponse;  | 
 | 23 | +import io.fabric8.kubernetes.client.http.TestAsyncBody;  | 
 | 24 | +import io.fabric8.kubernetes.client.http.TestHttpResponse;  | 
 | 25 | +import io.fabric8.kubernetes.client.impl.BaseClient;  | 
 | 26 | +import io.fabric8.kubernetes.client.utils.KubernetesSerialization;  | 
 | 27 | +import org.junit.jupiter.api.BeforeEach;  | 
 | 28 | +import org.junit.jupiter.api.Test;  | 
 | 29 | +import org.mockito.Mockito;  | 
 | 30 | + | 
 | 31 | +import java.io.ByteArrayOutputStream;  | 
 | 32 | +import java.net.HttpURLConnection;  | 
 | 33 | +import java.net.MalformedURLException;  | 
 | 34 | +import java.net.URL;  | 
 | 35 | +import java.util.concurrent.CompletableFuture;  | 
 | 36 | +import java.util.concurrent.CountDownLatch;  | 
 | 37 | +import java.util.concurrent.Executor;  | 
 | 38 | +import java.util.concurrent.Executors;  | 
 | 39 | +import java.util.concurrent.TimeUnit;  | 
 | 40 | + | 
 | 41 | +import static org.assertj.core.api.Assertions.assertThat;  | 
 | 42 | +import static org.mockito.Mockito.mock;  | 
 | 43 | +import static org.mockito.Mockito.spy;  | 
 | 44 | +import static org.mockito.Mockito.when;  | 
 | 45 | + | 
 | 46 | +class LogWatchCallbackTest {  | 
 | 47 | +  private OperationContext context;  | 
 | 48 | +  private Executor executor = Executors.newFixedThreadPool(2);  | 
 | 49 | +  private URL url;  | 
 | 50 | +  private HttpClient httpClientMock;  | 
 | 51 | + | 
 | 52 | +  @BeforeEach  | 
 | 53 | +  void setUp() throws MalformedURLException {  | 
 | 54 | +    BaseClient mock = mock(BaseClient.class, Mockito.RETURNS_SELF);  | 
 | 55 | +    Mockito.when(mock.adapt(BaseClient.class).getKubernetesSerialization()).thenReturn(new KubernetesSerialization());  | 
 | 56 | +    final OperationContext operationContext = new OperationContext().withClient(mock);  | 
 | 57 | +    when(mock.getExecutor()).thenReturn(this.executor);  | 
 | 58 | +    this.context = operationContext;  | 
 | 59 | + | 
 | 60 | +    this.url = new URL("http://url_called");  | 
 | 61 | +    this.httpClientMock = spy(HttpClient.class);  | 
 | 62 | +    var httpRequestMock = mock(HttpRequest.class);  | 
 | 63 | +    var builderMock = mock(HttpRequest.Builder.class);  | 
 | 64 | + | 
 | 65 | +    Mockito.when(httpClientMock.newHttpRequestBuilder()).thenReturn(builderMock);  | 
 | 66 | +    Mockito.when(builderMock.url(url)).thenReturn(builderMock);  | 
 | 67 | +    Mockito.when(builderMock.build()).thenReturn(httpRequestMock);  | 
 | 68 | + | 
 | 69 | +  }  | 
 | 70 | + | 
 | 71 | +  @Test  | 
 | 72 | +  void withOutputStreamCloseEventTest() throws InterruptedException {  | 
 | 73 | + | 
 | 74 | +    var future = new CompletableFuture<HttpResponse<AsyncBody>>();  | 
 | 75 | +    var reached = new CountDownLatch(1);  | 
 | 76 | + | 
 | 77 | +    Mockito.when(httpClientMock.consumeBytes(Mockito.any(), Mockito.any())).thenReturn(future);  | 
 | 78 | + | 
 | 79 | +    ByteArrayOutputStream baos = new ByteArrayOutputStream();  | 
 | 80 | +    LogWatchCallback logWatch = new LogWatchCallback(baos, this.context);  | 
 | 81 | +    logWatch.callAndWait(httpClientMock, url);  | 
 | 82 | + | 
 | 83 | +    logWatch.onClose().thenAccept((Throwable t) -> {  | 
 | 84 | +      reached.countDown();  | 
 | 85 | +    });  | 
 | 86 | +    future.complete(  | 
 | 87 | +        new TestHttpResponse<AsyncBody>().withCode(HttpURLConnection.HTTP_GONE).withBody(new TestAsyncBody()));  | 
 | 88 | + | 
 | 89 | +    assertThat(reached.await(1, TimeUnit.SECONDS)).isTrue();  | 
 | 90 | +    logWatch.close();  | 
 | 91 | +  }  | 
 | 92 | + | 
 | 93 | +  @Test  | 
 | 94 | +  void withOutputStreamCloseEventOnFailureTest() throws InterruptedException {  | 
 | 95 | + | 
 | 96 | +    var future = new CompletableFuture<HttpResponse<AsyncBody>>();  | 
 | 97 | +    var reached = new CountDownLatch(1);  | 
 | 98 | + | 
 | 99 | +    Mockito.when(httpClientMock.consumeBytes(Mockito.any(), Mockito.any())).thenReturn(future);  | 
 | 100 | + | 
 | 101 | +    LogWatchCallback logWatch = new LogWatchCallback(new ByteArrayOutputStream(), this.context);  | 
 | 102 | +    logWatch.callAndWait(httpClientMock, url);  | 
 | 103 | + | 
 | 104 | +    final Throwable[] tReturned = new Throwable[1];  | 
 | 105 | +    logWatch.onClose().thenAccept((Throwable t) -> {  | 
 | 106 | +      tReturned[0] = t;  | 
 | 107 | +      reached.countDown();  | 
 | 108 | +    });  | 
 | 109 | + | 
 | 110 | +    var th = new Throwable("any exception");  | 
 | 111 | +    future.completeExceptionally(th);  | 
 | 112 | + | 
 | 113 | +    assertThat(reached.await(1, TimeUnit.SECONDS)).isTrue();  | 
 | 114 | +    assertThat(tReturned[0]).isEqualTo(th);  | 
 | 115 | + | 
 | 116 | +    logWatch.close();  | 
 | 117 | +  }  | 
 | 118 | +}  | 
0 commit comments