-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added JUnit tests for rest-api/core module
Added JUnit tests for classes in rest-api/core module: - KapuaSerializableBodyWriter class - ListBodyWriter class - MoxyJsonConfigContextResolver class - filter package: - KapuaSessionCleanupFilterTest class - auth package: - KapuaTokenAuthenticationFilter class - settings package: - KapuaApiSettingKeys class - KapuaApiSetting class Signed-off-by: Sonja <sonja.matic@endava.com>
- Loading branch information
Showing
7 changed files
with
643 additions
and
0 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
...ation/src/test/java/org/eclipse/kapua/integration/misc/KapuaSessionCleanupFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua.integration.misc; | ||
|
||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.config.Ini; | ||
import org.apache.shiro.config.IniSecurityManagerFactory; | ||
import org.apache.shiro.mgt.SecurityManager; | ||
import org.eclipse.kapua.app.api.core.filter.KapuaSessionCleanupFilter; | ||
import org.eclipse.kapua.commons.security.KapuaSecurityUtils; | ||
import org.eclipse.kapua.commons.security.KapuaSession; | ||
import org.eclipse.kapua.qa.markers.junit.JUnitTests; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.mockito.Mockito; | ||
|
||
import javax.servlet.FilterConfig; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.ServletRequest; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
|
||
@Category(JUnitTests.class) | ||
public class KapuaSessionCleanupFilterTest extends Assert { | ||
|
||
KapuaSessionCleanupFilter kapuaSessionCleanupFilter; | ||
URL shiroIniUrl; | ||
Ini shiroIni; | ||
InputStream input; | ||
SecurityManager securityManager; | ||
ServletRequest request; | ||
ServletResponse response; | ||
FilterChain chain; | ||
KapuaSession kapuaSession; | ||
|
||
@Before | ||
public void initialize() throws IOException { | ||
kapuaSessionCleanupFilter = new KapuaSessionCleanupFilter(); | ||
shiroIniUrl = getClass().getResource("/shiro.ini"); | ||
shiroIni = new Ini(); | ||
input = shiroIniUrl.openStream(); | ||
shiroIni.load(input); | ||
securityManager = new IniSecurityManagerFactory(shiroIni).getInstance(); | ||
|
||
request = Mockito.mock(ServletRequest.class); | ||
response = Mockito.mock(ServletResponse.class); | ||
chain = Mockito.mock(FilterChain.class); | ||
kapuaSession = new KapuaSession(); | ||
} | ||
|
||
//COMMENT: Method init(FilterConfig filterConfig) is empty | ||
@Test | ||
public void initTest() { | ||
try { | ||
kapuaSessionCleanupFilter.init(Mockito.mock(FilterConfig.class)); | ||
} catch (Exception e) { | ||
fail("Exception not expected."); | ||
} | ||
} | ||
|
||
@Test | ||
public void initNullTest() { | ||
try { | ||
kapuaSessionCleanupFilter.init(null); | ||
} catch (Exception e) { | ||
fail("Exception not expected."); | ||
} | ||
} | ||
|
||
//COMMENT: Method destroy() is empty | ||
@Test | ||
public void destroyTest() { | ||
try { | ||
kapuaSessionCleanupFilter.destroy(); | ||
} catch (Exception e) { | ||
fail("Exception not expected."); | ||
} | ||
} | ||
|
||
@Test | ||
public void doFilterTest() { | ||
SecurityUtils.setSecurityManager(securityManager); | ||
KapuaSecurityUtils.setSession(kapuaSession); | ||
|
||
try { | ||
kapuaSessionCleanupFilter.doFilter(request, response, chain); | ||
} catch (Exception e) { | ||
fail("Exception not expected."); | ||
} | ||
} | ||
|
||
@Test | ||
public void doFilterNullRequestTest() { | ||
SecurityUtils.setSecurityManager(securityManager); | ||
KapuaSecurityUtils.setSession(kapuaSession); | ||
|
||
try { | ||
kapuaSessionCleanupFilter.doFilter(null, response, chain); | ||
} catch (Exception e) { | ||
fail("Exception not expected."); | ||
} | ||
} | ||
|
||
@Test | ||
public void doFilterNullResponseTest() { | ||
SecurityUtils.setSecurityManager(securityManager); | ||
KapuaSecurityUtils.setSession(kapuaSession); | ||
|
||
try { | ||
kapuaSessionCleanupFilter.doFilter(request, null, chain); | ||
} catch (Exception e) { | ||
fail("Exception not expected."); | ||
} | ||
} | ||
|
||
@Test | ||
public void doFilterNullChainTest() { | ||
SecurityUtils.setSecurityManager(securityManager); | ||
KapuaSecurityUtils.setSession(kapuaSession); | ||
|
||
try { | ||
kapuaSessionCleanupFilter.doFilter(request, response, null); | ||
fail("Exception expected."); | ||
} catch (Exception e) { | ||
assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString()); | ||
} | ||
} | ||
|
||
@Test | ||
public void doFilterNullSessionTest() { | ||
SecurityUtils.setSecurityManager(securityManager); | ||
try { | ||
kapuaSessionCleanupFilter.doFilter(request, response, chain); | ||
} catch (Exception e) { | ||
fail("Exception not expected."); | ||
} | ||
} | ||
} |
163 changes: 163 additions & 0 deletions
163
...pi/core/src/test/java/org/eclipse/kapua/app/api/core/KapuaSerializableBodyWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua.app.api.core; | ||
|
||
import org.eclipse.kapua.KapuaSerializable; | ||
import org.eclipse.kapua.qa.markers.junit.JUnitTests; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.mockito.Mockito; | ||
|
||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.ContextResolver; | ||
import javax.ws.rs.ext.Providers; | ||
import javax.xml.bind.JAXBContext; | ||
import javax.xml.bind.JAXBException; | ||
import javax.xml.bind.Marshaller; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
@Category(JUnitTests.class) | ||
public class KapuaSerializableBodyWriterTest extends Assert { | ||
|
||
KapuaSerializableBodyWriter kapuaSerializableBodyWriter; | ||
Annotation[] annotations; | ||
MediaType mediaType; | ||
KapuaSerializable kapuaSerializable; | ||
Type genericType; | ||
MultivaluedMap<String, Object> httpHeaders; | ||
JAXBContext jaxbContext; | ||
Marshaller marshaller; | ||
OutputStream outputStream; | ||
|
||
@Before | ||
public void initialize() { | ||
kapuaSerializableBodyWriter = new KapuaSerializableBodyWriter(); | ||
annotations = new Annotation[]{Mockito.mock(Annotation.class), Mockito.mock(Annotation.class)}; | ||
mediaType = MediaType.valueOf("some/text"); | ||
kapuaSerializable = Mockito.mock(KapuaSerializable.class); | ||
genericType = Mockito.mock(Type.class); | ||
httpHeaders = Mockito.mock(MultivaluedMap.class); | ||
jaxbContext = Mockito.mock(JAXBContext.class); | ||
marshaller = Mockito.mock(Marshaller.class); | ||
outputStream = new OutputStream() { | ||
@Override | ||
public void write(int b) { | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void isWriteableTest() { | ||
assertTrue("True expected.", kapuaSerializableBodyWriter.isWriteable(String.class, genericType, annotations, mediaType)); | ||
} | ||
|
||
@Test | ||
public void isWriteableNullTypeTest() { | ||
assertTrue("True expected.", kapuaSerializableBodyWriter.isWriteable(null, genericType, annotations, mediaType)); | ||
} | ||
|
||
@Test | ||
public void isWriteableNullGenericTypeTest() { | ||
assertTrue("True expected.", kapuaSerializableBodyWriter.isWriteable(String.class, null, annotations, mediaType)); | ||
} | ||
|
||
@Test | ||
public void isWriteableNullAnnotationsTest() { | ||
assertTrue("True expected.", kapuaSerializableBodyWriter.isWriteable(String.class, genericType, null, mediaType)); | ||
} | ||
|
||
@Test | ||
public void isWriteableNullMediaTypeTest() { | ||
assertTrue("True expected.", kapuaSerializableBodyWriter.isWriteable(String.class, genericType, annotations, null)); | ||
} | ||
|
||
@Test | ||
public void getSizeTest() { | ||
assertEquals("Expected and actual values should be the same.", 0, kapuaSerializableBodyWriter.getSize(kapuaSerializable, String.class, genericType, annotations, mediaType)); | ||
} | ||
|
||
@Test | ||
public void getSizeNullKapuaSerializableTest() { | ||
assertEquals("Expected and actual values should be the same.", 0, kapuaSerializableBodyWriter.getSize(null, String.class, genericType, annotations, mediaType)); | ||
} | ||
|
||
@Test | ||
public void getSizeNullTypeTest() { | ||
assertEquals("Expected and actual values should be the same.", 0, kapuaSerializableBodyWriter.getSize(kapuaSerializable, null, genericType, annotations, mediaType)); | ||
} | ||
|
||
@Test | ||
public void getSizeNullGenericTypeTest() { | ||
assertEquals("Expected and actual values should be the same.", 0, kapuaSerializableBodyWriter.getSize(kapuaSerializable, String.class, null, annotations, mediaType)); | ||
} | ||
|
||
@Test | ||
public void getSizeNullAnnotationsTest() { | ||
assertEquals("Expected and actual values should be the same.", 0, kapuaSerializableBodyWriter.getSize(kapuaSerializable, String.class, genericType, null, mediaType)); | ||
} | ||
|
||
@Test | ||
public void getSizeNullMediaTypeTest() { | ||
assertEquals("Expected and actual values should be the same.", 0, kapuaSerializableBodyWriter.getSize(kapuaSerializable, String.class, genericType, annotations, null)); | ||
} | ||
|
||
@Test(expected = WebApplicationException.class) | ||
public void writeToNullProvidersTest() throws IOException { | ||
kapuaSerializableBodyWriter.providers = null; | ||
|
||
kapuaSerializableBodyWriter.writeTo(kapuaSerializable, String.class, genericType, annotations, mediaType, httpHeaders, outputStream); | ||
} | ||
|
||
@Test(expected = WebApplicationException.class) | ||
public void writeToNullJaxbContextTest() throws IOException { | ||
kapuaSerializableBodyWriter.providers = Mockito.mock(Providers.class); | ||
|
||
Mockito.when(kapuaSerializableBodyWriter.providers.getContextResolver(JAXBContext.class, MediaType.APPLICATION_XML_TYPE)).thenReturn(Mockito.mock(ContextResolver.class)); | ||
Mockito.when(kapuaSerializableBodyWriter.providers.getContextResolver(JAXBContext.class, MediaType.APPLICATION_XML_TYPE).getContext(JAXBContext.class)).thenReturn(null); | ||
|
||
kapuaSerializableBodyWriter.writeTo(kapuaSerializable, String.class, genericType, annotations, mediaType, httpHeaders, outputStream); | ||
} | ||
|
||
@Test | ||
public void writeToTest() throws Exception { | ||
kapuaSerializableBodyWriter.providers = Mockito.mock(Providers.class); | ||
|
||
Mockito.when(kapuaSerializableBodyWriter.providers.getContextResolver(JAXBContext.class, MediaType.APPLICATION_XML_TYPE)).thenReturn(Mockito.mock(ContextResolver.class)); | ||
Mockito.when(kapuaSerializableBodyWriter.providers.getContextResolver(JAXBContext.class, MediaType.APPLICATION_XML_TYPE).getContext(JAXBContext.class)).thenReturn(jaxbContext); | ||
Mockito.when(jaxbContext.createMarshaller()).thenReturn(marshaller); | ||
|
||
try { | ||
kapuaSerializableBodyWriter.writeTo(kapuaSerializable, String.class, genericType, annotations, mediaType, httpHeaders, outputStream); | ||
} catch (Exception e) { | ||
fail("Exception not expected."); | ||
} | ||
} | ||
|
||
@Test(expected = WebApplicationException.class) | ||
public void writeToInternalServerErrorTest() throws Exception { | ||
kapuaSerializableBodyWriter.providers = Mockito.mock(Providers.class); | ||
|
||
Mockito.when(kapuaSerializableBodyWriter.providers.getContextResolver(JAXBContext.class, MediaType.APPLICATION_XML_TYPE)).thenReturn(Mockito.mock(ContextResolver.class)); | ||
Mockito.when(kapuaSerializableBodyWriter.providers.getContextResolver(JAXBContext.class, MediaType.APPLICATION_XML_TYPE).getContext(JAXBContext.class)).thenReturn(jaxbContext); | ||
Mockito.when(jaxbContext.createMarshaller()).thenThrow(new JAXBException("message")); | ||
|
||
kapuaSerializableBodyWriter.writeTo(kapuaSerializable, String.class, genericType, annotations, mediaType, httpHeaders, outputStream); | ||
} | ||
} |
Oops, something went wrong.