2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.common.util;
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;
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;
37 import static junit.framework.TestCase.assertEquals;
38 import static junit.framework.TestCase.assertTrue;
39 import static org.mockito.Mockito.when;
41 public class HttpUtilTest {
44 public void validateGetObjectFromJsonReturnsValidObjectFromValidJason() throws IOException {
46 final Configuration testObject = new Configuration();
47 testObject.setVersion("1.0.test");
48 testObject.setThreadpoolSize(5);
50 final String testObjectAsJson = new GsonBuilder().setPrettyPrinting().create().toJson(testObject);
52 final ServletInputStream servletInputStream = new TestServletInputStream(testObjectAsJson);
54 final HttpServletRequest request =
55 Mockito.mock(HttpServletRequest.class);
56 when(request.getInputStream()).thenReturn(servletInputStream);
59 Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(request, Configuration.class);
61 assertTrue(result.isLeft());
63 Configuration returnedConfiguration = result.left().value();
65 assertEquals(returnedConfiguration.getVersion(),testObject.getVersion());
66 assertEquals(returnedConfiguration.getBeProtocol(),testObject.getBeProtocol());
67 assertEquals(returnedConfiguration.getThreadpoolSize(),testObject.getThreadpoolSize());
71 public void validateGetObjectFromJsonReturnsExceptionIfStreamThrowIOException() throws IOException {
73 final String testException = "test exception";
75 final HttpServletRequest request =
76 Mockito.mock(HttpServletRequest.class);
77 when(request.getInputStream()).thenThrow(new IOException(testException));
79 Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(request, Configuration.class);
81 assertTrue(result.isRight());
82 assertEquals(result.right().value().getMessage(), testException);
86 public void validateGetObjectFromJsonReturnsExceptionIfInputStringIsInvalid() throws IOException {
87 final ServletInputStream servletInputStream = new TestServletInputStream("Wrong Json Object");
89 final HttpServletRequest request =
90 Mockito.mock(HttpServletRequest.class);
91 when(request.getInputStream()).thenReturn(servletInputStream);
94 Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(request, Configuration.class);
96 assertTrue(result.isRight());
97 assertEquals(result.right().value().getClass(), JsonSyntaxException.class);
101 public void validateGetObjectFromJsonReturnsExceptionIfInputIsNull() throws IOException {
102 Either<Configuration , Exception> result = HttpUtil.getObjectFromJson(null, Configuration.class);
104 assertTrue(result.isRight());
105 assertEquals(result.right().value().getClass(), NullPointerException.class);
108 class TestServletInputStream extends ServletInputStream {
110 private InputStream testStream;
112 TestServletInputStream(String testJson) {
113 testStream = new ByteArrayInputStream(testJson.getBytes());
118 public boolean isFinished() {
123 public boolean isReady() {
128 public void setReadListener(ReadListener readListener) { }
131 public int read() throws IOException {
132 return testStream.read();