/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.search.test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.perforce.search.test.util.IndexTestBase;
public class RootControllerTest extends IndexTestBase {
@BeforeClass
public void setUp() throws Exception {
this.configFileLocation = "/noscan.config";
super.setUp();
}
// TODO: we don't have a webapp, so test as if authenticated
@Test(groups = "authenticated")
void testLogin() {
// for now, this means nothing
}
@SuppressWarnings("unchecked")
public static Map<String, Object> getMap(final ModelAndView mav,
final String name) {
return (Map<String, Object>) mav.getModel().get(name);
}
@SuppressWarnings("unchecked")
public static List<Object> getList(final ModelAndView mav, final String name) {
return (List<Object>) mav.getModel().get(name);
}
public static String getStatusCode(final ModelAndView mav) {
return (String) getMap(mav, "status").get("code");
}
public static Object getPayload(final ModelAndView mav, final String name) {
return getMap(mav, "payload").get(name);
}
@Test(groups = "authenticated", enabled = false)
void testLoginWithWebapp() {
// TODO: login
Assert.fail("not implemented");
}
@Test(dependsOnGroups = "authenticated")
public void testGetConfigAuth() throws UnsupportedEncodingException {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse resp = new MockHttpServletResponse();
ModelAndView mav = this.rootController.getConfig(resp, req);
Assert.assertEquals(getStatusCode(mav), "200");
Assert.assertEquals(getPayload(mav, "commonsURL"),
config.getCommonsUrl());
Assert.assertEquals(getPayload(mav, "swarmURL"), config.getSwarmUrl());
}
@Test(dependsOnGroups = "authenticated", enabled = false)
public void testGetUIDAuth() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse resp = new MockHttpServletResponse();
this.rootController.getUserId(resp, req);
Assert.assertEquals(resp.getHeader("redirect"), null);
}
@Test(dependsOnGroups = "authenticated")
public void testGetVersion() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse resp = new MockHttpServletResponse();
ModelAndView mav = rootController.getVersion(resp, req);
Assert.assertEquals(getStatusCode(mav), "200");
}
@Test(dependsOnGroups = "authenticated")
public void testGetHome() throws IOException {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse resp = new MockHttpServletResponse();
rootController.home(resp, req);
Assert.assertEquals(resp.getContentAsString().toString(), "");
}
@Test(dependsOnGroups = "authenticated")
public void testGetNotAuthorized() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse resp = new MockHttpServletResponse();
ModelAndView mav = rootController.notAuthorized(resp, req);
Assert.assertEquals(getStatusCode(mav), "401");
}
// TODO: begin not auth
@Test(groups = "notauthenticated", enabled = false)
public void testLogout() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse resp = new MockHttpServletResponse();
ModelAndView mav = this.rootController.getConfig(resp, req);
Assert.assertEquals(mav.getModel().toString(), "this is a failing test");
}
@Test(dependsOnGroups = "notauthenticated", enabled = false)
void testGetUIDNotAuth() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse resp = new MockHttpServletResponse();
ModelAndView mav = this.rootController.getConfig(resp, req);
Assert.assertEquals(mav.getModel().toString(), "this is a failing test");
}
@Test(dependsOnGroups = "notauthenticated", enabled = false)
public void testGetVersionNotAuth() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse resp = new MockHttpServletResponse();
ModelAndView mav = rootController.getVersion(resp, req);
Assert.assertEquals(mav.getModel().toString(), "this is a failing test");
}
@Test(dependsOnGroups = "notauthenticated", enabled = false)
public void testGetHomeNotAuth() throws IOException {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse resp = new MockHttpServletResponse();
rootController.home(resp, req);
Assert.assertEquals(resp.getContentAsString().toString(),
"this is a failing test");
}
@Test(dependsOnGroups = "notauthenticated", enabled = false)
public void testGetNotAuthorizedNotAuth() {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse resp = new MockHttpServletResponse();
ModelAndView mav = rootController.notAuthorized(resp, req);
Assert.assertEquals(mav.getModel().toString(), "this is a failing test");
}
}