refactoring tests in Common-App-Api util
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / util / HttpUtilTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.util;
22
23 import com.google.gson.GsonBuilder;
24 import com.google.gson.JsonSyntaxException;
25 import fj.data.Either;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.openecomp.sdc.fe.config.Configuration;
29
30 import javax.servlet.ReadListener;
31 import javax.servlet.ServletInputStream;
32 import javax.servlet.http.HttpServletRequest;
33 import java.io.ByteArrayInputStream;
34 import java.io.IOException;
35 import java.io.InputStream;
36
37 import static junit.framework.TestCase.assertEquals;
38 import static junit.framework.TestCase.assertTrue;
39 import static org.mockito.Mockito.when;
40
41 public class HttpUtilTest {
42
43     @Test
44     public void validateGetObjectFromJsonReturnsValidObjectFromValidJason() throws IOException {
45
46         final Configuration testObject = new Configuration();
47         testObject.setVersion("1.0.test");
48         testObject.setThreadpoolSize(5);
49
50         final String testObjectAsJson = new GsonBuilder().setPrettyPrinting().create().toJson(testObject);
51
52         final ServletInputStream servletInputStream = new TestServletInputStream(testObjectAsJson);
53
54         final HttpServletRequest request =
55                 Mockito.mock(HttpServletRequest.class);
56         when(request.getInputStream()).thenReturn(servletInputStream);
57
58
59         Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(request, Configuration.class);
60
61         assertTrue(result.isLeft());
62
63         Configuration returnedConfiguration = result.left().value();
64
65         assertEquals(returnedConfiguration.getVersion(),testObject.getVersion());
66         assertEquals(returnedConfiguration.getBeProtocol(),testObject.getBeProtocol());
67         assertEquals(returnedConfiguration.getThreadpoolSize(),testObject.getThreadpoolSize());
68     }
69
70     @Test
71     public void validateGetObjectFromJsonReturnsExceptionIfStreamThrowIOException() throws IOException {
72
73         final String testException = "test exception";
74
75         final HttpServletRequest request =
76                 Mockito.mock(HttpServletRequest.class);
77         when(request.getInputStream()).thenThrow(new IOException(testException));
78
79         Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(request, Configuration.class);
80
81         assertTrue(result.isRight());
82         assertEquals(result.right().value().getMessage(), testException);
83     }
84
85     @Test
86     public void validateGetObjectFromJsonReturnsExceptionIfInputStringIsInvalid() throws IOException {
87         final ServletInputStream servletInputStream = new TestServletInputStream("Wrong Json Object");
88
89         final HttpServletRequest request =
90                 Mockito.mock(HttpServletRequest.class);
91         when(request.getInputStream()).thenReturn(servletInputStream);
92
93
94         Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(request, Configuration.class);
95
96         assertTrue(result.isRight());
97         assertEquals(result.right().value().getClass(), JsonSyntaxException.class);
98     }
99
100     @Test
101     public void validateGetObjectFromJsonReturnsExceptionIfInputIsNull() throws IOException {
102         Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(null, Configuration.class);
103
104         assertTrue(result.isRight());
105         assertEquals(result.right().value().getClass(), NullPointerException.class);
106     }
107
108     class TestServletInputStream extends ServletInputStream {
109
110         private InputStream testStream;
111
112         TestServletInputStream(String testJson) {
113             testStream = new ByteArrayInputStream(testJson.getBytes());
114         }
115
116
117         @Override
118         public boolean isFinished() {
119             return false;
120         }
121
122         @Override
123         public boolean isReady() {
124             return false;
125         }
126
127         @Override
128         public void setReadListener(ReadListener readListener) { }
129
130         @Override
131         public int read() throws IOException {
132             return testStream.read();
133         }
134     }
135 }