View Javadoc
1   /*
2    * Copyright 2019-2021 the original author or authors.
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    *      https://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 nl.altindag.ssl.util;
18  
19  import nl.altindag.ssl.exception.GenericIOException;
20  import org.junit.jupiter.api.Test;
21  import org.junit.jupiter.api.extension.ExtendWith;
22  import org.mockito.Mockito;
23  import org.mockito.junit.jupiter.MockitoExtension;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.IOException;
27  
28  import static org.assertj.core.api.Assertions.assertThat;
29  import static org.assertj.core.api.Assertions.assertThatThrownBy;
30  import static org.mockito.Mockito.doThrow;
31  
32  /**
33   * @author Hakan Altindag
34   */
35  @ExtendWith(MockitoExtension.class)
36  class IOUtilsShould {
37  
38      @Test
39      void getContent() {
40          ByteArrayInputStream inputStream = new ByteArrayInputStream("Hello".getBytes());
41          String content = IOUtils.getContent(inputStream);
42          assertThat(content).isEqualTo("Hello");
43      }
44  
45      @Test
46      void bla() throws IOException {
47          ByteArrayInputStream inputStream = Mockito.spy(new ByteArrayInputStream("Hello".getBytes()));
48          doThrow(new IOException("Could not read the content")).when(inputStream).close();
49  
50          assertThatThrownBy(() -> IOUtils.getContent(inputStream))
51                  .isInstanceOf(GenericIOException.class)
52                  .hasRootCauseMessage("Could not read the content");
53      }
54  
55  }