Skip to content

Commit

Permalink
Enhance core test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mgoellnitz committed Mar 20, 2016
1 parent 4841788 commit d3bdd6f
Show file tree
Hide file tree
Showing 10 changed files with 637 additions and 74 deletions.
131 changes: 66 additions & 65 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
/**
*
* Copyright 2011-2015 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
apply from: '../webapp.gradle'

configurations {
all*.exclude(group: 'rhino', module: 'js')
}

dependencies {
compile 'javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1'
compile 'org.apache.taglibs:taglibs-standard-impl:1.2.5'
compile "javax.inject:javax.inject:1"

compile "org.slf4j:slf4j-api:$versions.slf4j"
// Nice to have for some pac4j implementations
// runtime "org.slf4j:jcl-over-slf4j:$versions.slf4j"
compile "commons-fileupload:commons-fileupload:1.3"
compile "commons-io:commons-io:2.4"
// Used by velocity:
compile "commons-collections:commons-collections:3.2.2"

compile 'com.github.rjeschke:txtmark:0.13'
// Another option for markdown
// compile 'org.pegdown:pegdown:1.6.0'
compile "org.apache.velocity:velocity:$versions.velocity"
compile "org.codehaus.groovy:groovy:$versions.groovy"
// Latest groovy version doesn't include this dependency which is in fact necessary
compile "org.ow2.asm:asm-analysis:$versions.asm"
compile "org.ow2.asm:asm-commons:$versions.asm"
compile "org.ow2.asm:asm-tree:$versions.asm"
compile "org.ow2.asm:asm-util:$versions.asm"
compile("com.yahoo.platform.yui:yuicompressor:$versions.yui") {
exclude module: 'js'
}

compile "org.pac4j:pac4j-core:$versions.pac4j"
compile "org.pac4j:pac4j-http:$versions.pac4j"
compile "org.pac4j:pac4j-oauth:$versions.pac4j"

testCompile "org.testng:testng:$versions.testng"
// test logging at debug level through special logback.groovy
testCompile "ch.qos.logback:logback-classic:$versions.logback"
testCompile "org.mockito:mockito-all:$versions.mockito"
testCompile "org.springframework:spring-core:$versions.springframework"
testCompile "org.springframework:spring-test:$versions.springframework"

providedCompile "$versions.servlet_api"
providedCompile "$versions.jsp_api"
}
/**
*
* Copyright 2011-2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
apply from: '../webapp.gradle'

configurations {
all*.exclude(group: 'rhino', module: 'js')
}

dependencies {
compile 'javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1'
compile 'org.apache.taglibs:taglibs-standard-impl:1.2.5'
compile "javax.inject:javax.inject:1"

compile "org.slf4j:slf4j-api:$versions.slf4j"
// Nice to have for some pac4j implementations
// runtime "org.slf4j:jcl-over-slf4j:$versions.slf4j"
compile "commons-fileupload:commons-fileupload:1.3"
compile "commons-io:commons-io:2.4"
// Used by velocity:
compile "commons-collections:commons-collections:3.2.2"

compile 'com.github.rjeschke:txtmark:0.13'
// Another option for markdown
// compile 'org.pegdown:pegdown:1.6.0'
compile "org.apache.velocity:velocity:$versions.velocity"
compile "org.codehaus.groovy:groovy:$versions.groovy"
// Latest groovy version doesn't include this dependency which is in fact necessary
compile "org.ow2.asm:asm-analysis:$versions.asm"
compile "org.ow2.asm:asm-commons:$versions.asm"
compile "org.ow2.asm:asm-tree:$versions.asm"
compile "org.ow2.asm:asm-util:$versions.asm"
compile("com.yahoo.platform.yui:yuicompressor:$versions.yui") {
exclude module: 'js'
}

compile "org.pac4j:pac4j-core:$versions.pac4j"
compile "org.pac4j:pac4j-http:$versions.pac4j"
compile "org.pac4j:pac4j-oauth:$versions.pac4j"

testCompile "org.testng:testng:$versions.testng"
// test logging at debug level through special logback.groovy
testCompile "ch.qos.logback:logback-classic:$versions.logback"
testCompile "org.mockito:mockito-all:$versions.mockito"
// testCompile "org.springframework:spring-core:$versions.springframework"
testCompile "org.springframework:spring-test:$versions.springframework"
testCompile "org.springframework:spring-web:$versions.springframework"

providedCompile "$versions.servlet_api"
providedCompile "$versions.jsp_api"
}
2 changes: 2 additions & 0 deletions core/src/org/tangram/components/DefaultHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ public Link createLink(HttpServletRequest request, HttpServletResponse r, Object
} else {
url.append("/id_");
url.append(((Content) bean).getId());
/* how should this be possible in this very branch?
if (view!=null) {
url.append("/view_");
url.append(view);
} // if
*/
} // if
result = new Link(url.toString());
} else {
Expand Down
92 changes: 92 additions & 0 deletions core/test/org/tangram/components/test/DefaultHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
*
* Copyright 2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.tangram.components.test;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.tangram.components.DefaultHandler;
import org.tangram.content.BeanFactory;
import org.tangram.content.Content;
import org.tangram.content.blob.MimedBlob;
import org.tangram.link.Link;
import org.tangram.link.TargetDescriptor;
import org.testng.Assert;
import org.testng.annotations.Test;


/**
* Test default handler behaviour.
*/
public class DefaultHandlerTest {

@Mock
private BeanFactory beanFactory = null;

@InjectMocks
private DefaultHandler defaultHandler = new DefaultHandler();

private interface BlobContent extends MimedBlob, Content {

} // BlobContent

@Test
public void testDefaultHandler() {
MockitoAnnotations.initMocks(this);
defaultHandler.setMaxInlinedBlobSize(2);
HttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = new MockHttpServletResponse();
try {
TargetDescriptor target = defaultHandler.render("Content:42", request, response);
Assert.assertNull(target, "Null target expected for invalid content ID.");
Assert.assertEquals(response.getStatus(), 404, "The given content ID is intentionally wrong.");
} catch (IOException e) {
Assert.fail("Exceptions should not occur during test.", e);
} // try/catch
Content c = Mockito.mock(Content.class);
Mockito.when(c.getId()).thenReturn("RootTopic:1");
Link link = defaultHandler.createLink(request, response, c, null, "inline");
Assert.assertEquals(link.getUrl(), "/id_RootTopic:1/view_inline", "Unexpected URL returned.");

BlobContent blob = Mockito.mock(BlobContent.class);
Mockito.when(blob.getId()).thenReturn("Image:13");
byte[] bytes = new byte[2];
bytes[0] = 65;
bytes[1] = 66;
Mockito.when(blob.getBytes()).thenReturn(bytes);
link = defaultHandler.createLink(request, response, blob, null, null);
Assert.assertEquals(link.getUrl(), "/id_Image:13", "Unexpected large blob URL returned.");

blob = Mockito.mock(BlobContent.class);
Mockito.when(blob.getId()).thenReturn("Image:13");
Mockito.when(blob.getMimeType()).thenReturn("image/png");
bytes = new byte[1];
bytes[0] = 65;
Mockito.when(blob.getBytes()).thenReturn(bytes);
link = defaultHandler.createLink(request, response, blob, null, null);
Assert.assertEquals(link.getUrl(), "data:image/png;base64,QQ==", "Unexpected small blob URL returned.");
} // testDefaultHandler()

} // DefaultHandlerTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
*
* Copyright 2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.tangram.components.test;

import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.tangram.authentication.AuthenticationService;
import org.tangram.components.GenericAuthorizationService;
import org.tangram.content.CodeResourceCache;
import org.testng.Assert;
import org.testng.annotations.Test;


/**
* Test authorization service behaviour.
*/
public class GenericAuthorizationServiceTest {

@Mock
private final AuthenticationService authenticationService = Mockito.mock(AuthenticationService.class);

@Mock
private final CodeResourceCache codeResourceCache = Mockito.mock(CodeResourceCache.class);

@Mock(name = "adminUsers")
private final Set<String> adminUsers = new HashSet<>();

@InjectMocks
private final GenericAuthorizationService authorizationService = new GenericAuthorizationService();


@Test
public void testGenericAuthorizationService() {
MockitoAnnotations.initMocks(this);
authorizationService.afterPropertiesSet();
// TODO:
// authorizationService.reset();

HttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = new MockHttpServletResponse();
Assert.assertFalse(authorizationService.isAdminUser(request, response), "Dummy request don't belong to admin users.");

boolean result = false;
try {
authorizationService.throwIfNotAdmin(request, response, "Sorry");
} catch (Exception e) {
result = true;
} // try/catch
Assert.assertTrue(result, "Since dummy request contains no admin user an exception should be thrown.");
} // testGenericAuthorizationService()

} // GenericAuthorizationServiceTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
*
* Copyright 2016 Martin Goellnitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.tangram.components.test;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.tangram.PersistentRestartCache;
import org.tangram.components.GenericCodeResourceCache;
import org.tangram.content.BeanFactory;
import org.tangram.content.BeanListener;
import org.tangram.util.DummyRestartCache;
import org.testng.Assert;
import org.testng.annotations.Test;


/**
* Test code resource cache behaviour.
*/
public class GenericCodeResourceCacheTest {

@Mock
private BeanFactory factory = null;

@Mock
private PersistentRestartCache restartCache = new DummyRestartCache();

@InjectMocks
private GenericCodeResourceCache codeResourceCache = new GenericCodeResourceCache();


private class TestBeanLister implements BeanListener {

private boolean result = false;


@Override
public void reset() {
result = true;
}


public boolean isResult() {
return result;
}

}


@Test
public void testGenericCodeResourceCache() {
MockitoAnnotations.initMocks(this);
TestBeanLister beanListener = new TestBeanLister();
codeResourceCache.addListener(beanListener);
codeResourceCache.reset();
Assert.assertTrue(beanListener.isResult(), "Bean listener should be called.");
codeResourceCache.afterPropertiesSet();
boolean isInInterval = (System.currentTimeMillis() - codeResourceCache.getLastUpdate()) < 20;
Assert.assertTrue(isInInterval, "Modification time of cache should be initialized.");
} // testGenericCodeResourceCache()

} // GenericCodeResourceCacheTest
Loading

0 comments on commit d3bdd6f

Please sign in to comment.