From 6e1773c260290e4fc29da2b1505bc63d3ec3595d Mon Sep 17 00:00:00 2001 From: Tomasz Golabek Date: Thu, 25 Jul 2019 14:22:53 +0200 Subject: Additional unit tests Some tests for catalog-fe Change-Id: I383021c7d1e85131adebd559e4763e12e36cba67 Issue-ID: SDC-2326 Signed-off-by: Tomasz Golabek --- .../openecomp/sdc/fe/impl/HttpRequestInfoTest.java | 533 ++------------------- .../sdc/fe/listen/MyObjectMapperProviderTest.java | 38 +- .../sdc/fe/servlets/ConfigMgrServletTest.java | 100 ++++ 3 files changed, 152 insertions(+), 519 deletions(-) create mode 100644 catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigMgrServletTest.java (limited to 'catalog-fe/src/test/java') diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HttpRequestInfoTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HttpRequestInfoTest.java index 72a2284a36..bfc45528c4 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HttpRequestInfoTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HttpRequestInfoTest.java @@ -16,524 +16,49 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * Modifications copyright (c) 2019 Nokia + * ================================================================================ */ package org.openecomp.sdc.fe.impl; -import org.junit.Test; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; -import javax.servlet.*; -import javax.servlet.http.*; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.security.Principal; -import java.util.Collection; -import java.util.Enumeration; -import java.util.Locale; +import java.util.HashMap; import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import org.apache.commons.io.IOUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +@RunWith(MockitoJUnitRunner.class) public class HttpRequestInfoTest { - private HttpRequestInfo createTestSubject() { - return new HttpRequestInfo(new HttpServletRequestTest(), null, ""); - } - - @Test - public void testGetHeaders() { - HttpRequestInfo testSubject; - Map result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getHeaders(); - } - - @Test - public void testSetHeaders() { - HttpRequestInfo testSubject; - Map headers = null; - - // default test - testSubject = createTestSubject(); - testSubject.setHeaders(headers); - } - - @Test - public void testGetRequestURL() { - HttpRequestInfo testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getRequestURL(); - } - - @Test - public void testSetRequestURL() { - HttpRequestInfo testSubject; - String requestURL = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setRequestURL(requestURL); - } - - @Test - public void testGetRequestData() throws IOException { - HttpRequestInfo testSubject; - - // default test - testSubject = createTestSubject(); - InputStream result = testSubject.getRequestData(); - if (result != null) { - result.close(); - } - } - - @Test - public void testSetRequestData() { - HttpRequestInfo testSubject; - InputStream requestData = null; - - // default test - testSubject = createTestSubject(); - testSubject.setRequestData(requestData); - } + @Mock + private HttpServletRequest request; + private Map headersMap = new HashMap<>(); @Test - public void testGetOriginServletContext() { - HttpRequestInfo testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getOriginServletContext(); + public void shouldHaveGettersAndSetters() { + Assert.assertThat(HttpRequestInfo.class, hasValidGettersAndSetters()); } @Test - public void testSetOriginServletContext() { - HttpRequestInfo testSubject; - String originServletContext = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setOriginServletContext(originServletContext); - } - - private class HttpServletRequestTest implements HttpServletRequest { - - private HttpServletRequestTest() { - - } - @Override - public Object getAttribute(String name) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Enumeration getAttributeNames() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getCharacterEncoding() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void setCharacterEncoding(String env) { - // TODO Auto-generated method stub - - } - - @Override - public int getContentLength() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long getContentLengthLong() { - return 0; - } - - @Override - public String getContentType() { - // TODO Auto-generated method stub - return null; - } - - @Override - public ServletInputStream getInputStream() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getParameter(String name) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Enumeration getParameterNames() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String[] getParameterValues(String name) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Map getParameterMap() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getProtocol() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getScheme() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getServerName() { - // TODO Auto-generated method stub - return null; - } - - @Override - public int getServerPort() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public BufferedReader getReader() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getRemoteAddr() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getRemoteHost() { - // TODO Auto-generated method stub - return null; - } - - @Override - public void setAttribute(String name, Object o) { - // TODO Auto-generated method stub - - } - - @Override - public void removeAttribute(String name) { - // TODO Auto-generated method stub - - } - - @Override - public Locale getLocale() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Enumeration getLocales() { - // TODO Auto-generated method stub - return null; - } - - @Override - public boolean isSecure() { - // TODO Auto-generated method stub - return false; - } - - @Override - public RequestDispatcher getRequestDispatcher(String path) { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getRealPath(String path) { - // TODO Auto-generated method stub - return null; - } - - @Override - public int getRemotePort() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public String getLocalName() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getLocalAddr() { - // TODO Auto-generated method stub - return null; - } - - @Override - public int getLocalPort() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public ServletContext getServletContext() { - // TODO Auto-generated method stub - return null; - } - - @Override - public AsyncContext startAsync() { - // TODO Auto-generated method stub - return null; - } - - @Override - public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) { - // TODO Auto-generated method stub - return null; - } - - @Override - public boolean isAsyncStarted() { - // TODO Auto-generated method stub - return false; - } - - @Override - public boolean isAsyncSupported() { - // TODO Auto-generated method stub - return false; - } - - @Override - public AsyncContext getAsyncContext() { - // TODO Auto-generated method stub - return null; - } - - @Override - public DispatcherType getDispatcherType() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getAuthType() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Cookie[] getCookies() { - // TODO Auto-generated method stub - return null; - } - - @Override - public long getDateHeader(String name) { - // TODO Auto-generated method stub - return 0; - } - - @Override - public String getHeader(String name) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Enumeration getHeaders(String name) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Enumeration getHeaderNames() { - // TODO Auto-generated method stub - return null; - } - - @Override - public int getIntHeader(String name) { - // TODO Auto-generated method stub - return 0; - } - - @Override - public String getMethod() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getPathInfo() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getPathTranslated() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getContextPath() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getQueryString() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getRemoteUser() { - // TODO Auto-generated method stub - return null; - } - - @Override - public boolean isUserInRole(String role) { - // TODO Auto-generated method stub - return false; - } - - @Override - public Principal getUserPrincipal() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getRequestedSessionId() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getRequestURI() { - // TODO Auto-generated method stub - return null; - } - - @Override - public StringBuffer getRequestURL() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getServletPath() { - // TODO Auto-generated method stub - return null; - } - - @Override - public HttpSession getSession(boolean create) { - // TODO Auto-generated method stub - return null; - } - - @Override - public HttpSession getSession() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String changeSessionId() { - return null; - } - - @Override - public boolean isRequestedSessionIdValid() { - // TODO Auto-generated method stub - return false; - } - - @Override - public boolean isRequestedSessionIdFromCookie() { - // TODO Auto-generated method stub - return false; - } - - @Override - public boolean isRequestedSessionIdFromURL() { - // TODO Auto-generated method stub - return false; - } - - @Override - public boolean isRequestedSessionIdFromUrl() { - // TODO Auto-generated method stub - return false; - } - - @Override - public boolean authenticate(HttpServletResponse httpServletResponse) { - // TODO Auto-generated method stub - return false; - } - - @Override - public void login(String s, String s1) { - // TODO Auto-generated method stub - } - - @Override - public void logout() { - // TODO Auto-generated method stub - } - - @Override - public Collection getParts() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Part getPart(String s) { - // TODO Auto-generated method stub - return null; - } - - @Override - public T upgrade(Class aClass) throws IOException, ServletException { - return null; - } - + public void shouldHaveValidConstructor() throws IOException { + String any_url = "ANY_URL"; + Mockito.when(request.getRequestURI()).thenReturn(any_url); + String context = "PATH"; + Mockito.when(request.getContextPath()).thenReturn(context); + String data = "ABC"; + HttpRequestInfo httpRequestInfo = new HttpRequestInfo(request, headersMap, data); + byte[] requestData = IOUtils.toByteArray(httpRequestInfo.getRequestData()); + Assert.assertArrayEquals(requestData, data.getBytes()); + Assert.assertEquals(httpRequestInfo.getRequestURL(), any_url); + Assert.assertEquals(httpRequestInfo.getOriginServletContext(), context); } } diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/listen/MyObjectMapperProviderTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/listen/MyObjectMapperProviderTest.java index 9b6acd6203..4a390d3e0c 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/listen/MyObjectMapperProviderTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/listen/MyObjectMapperProviderTest.java @@ -16,33 +16,41 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * Modifications copyright (c) 2019 Nokia + * ================================================================================ */ package org.openecomp.sdc.fe.listen; +import com.fasterxml.jackson.core.JsonProcessingException; +import java.io.Serializable; +import org.junit.Assert; import org.junit.Test; import com.fasterxml.jackson.databind.ObjectMapper; - public class MyObjectMapperProviderTest { - private MyObjectMapperProvider createTestSubject() { - return new MyObjectMapperProvider(); + private class AnyModel implements Serializable { + private String field1; + + AnyModel(String field1) { + this.field1 = field1; + } + + public String getField1() { + return field1; + } } - @Test - public void testGetContext() throws Exception { - MyObjectMapperProvider testSubject; - Class type = null; - ObjectMapper result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getContext(type); + public void shouldSerializeItPretty() throws JsonProcessingException { + String prettyJson = "{\n" + + " \"field1\" : \"Field1\"\n" + + "}"; + + ObjectMapper objectMapper = new MyObjectMapperProvider().getContext(MyObjectMapperProviderTest.class); + String serialized = objectMapper.writeValueAsString(new AnyModel("Field1")); + Assert.assertEquals(serialized, prettyJson); } - - - } diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigMgrServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigMgrServletTest.java new file mode 100644 index 0000000000..5406863fc0 --- /dev/null +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigMgrServletTest.java @@ -0,0 +1,100 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.openecomp.sdc.fe.servlets; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.rest.api.RestConfigurationInfo; +import org.openecomp.sdc.fe.config.Configuration; +import org.openecomp.sdc.fe.config.ConfigurationManager; + +@RunWith(MockitoJUnitRunner.class) +public class ConfigMgrServletTest { + + private static final String VERSION_1 = "VERSION 1"; + private static final String PROTOCOL = "PROTO"; + @Mock + private HttpServletRequest request; + @Mock + private HttpSession session; + @Mock + private ServletContext context; + @Mock + private ConfigurationManager configManager; + + @Before + public void setUp() { + Mockito.when(request.getSession()).thenReturn(session); + Mockito.when(session.getServletContext()).thenReturn(context); + Mockito.when(context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configManager); + } + + @Test + public void shouldGetConfigForRestType() { + String wantedResult = "{\n" + + " \"connectionPoolSize\": 5\n" + + "}"; + RestConfigurationInfo restConfiguration = new RestConfigurationInfo(); + restConfiguration.setConnectionPoolSize(5); + Mockito.when(configManager.getRestClientConfiguration()).thenReturn(restConfiguration); + + ConfigMgrServlet configMgrServlet = new ConfigMgrServlet(); + String config = configMgrServlet.getConfig(request, "rest"); + Assert.assertEquals(wantedResult, config); + } + + @Test + public void shouldGetConfigForConfigurationType() { + checkForConfigurationTypeOrEmpty("configuration"); + } + + @Test + public void shouldGetConfigForNullType() { + checkForConfigurationTypeOrEmpty(null); + } + + private void checkForConfigurationTypeOrEmpty(String type) { + String wantedResult = "{\n" + + " \"beProtocol\": \"PROTO\",\n" + + " \"version\": \"VERSION 1\",\n" + + " \"threadpoolSize\": 0,\n" + + " \"requestTimeout\": 0\n" + + "}"; + + Configuration configuration = new Configuration(); + configuration.setVersion(VERSION_1); + configuration.setBeProtocol(PROTOCOL); + Mockito.when(configManager.getConfiguration()).thenReturn(configuration); + + ConfigMgrServlet configMgrServlet = new ConfigMgrServlet(); + String config = configMgrServlet.getConfig(request, type); + Assert.assertEquals(wantedResult, config); + } + +} \ No newline at end of file -- cgit 1.2.3-korg