/** * Copyright © 2014 Instituto Superior Técnico * * This file is part of Bennu Spring. * * Bennu Spring 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. * * Bennu Spring 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 Bennu Spring. If not, see <http://www.gnu.org/licenses/>. */ package org.fenixedu.bennu.spring; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.fenixedu.commons.i18n.I18N; import org.fenixedu.commons.i18n.LocalizedString; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = BennuSpringConfiguration.class) public class BennuSpringTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void testDomainExceptionHandling() throws Exception { int codes[] = { 403, 404, 412, 500 }; for (int value : codes) { this.mockMvc.perform(get("/test/" + value)).andExpect(status().is(value)); } } @Test public void testLocalizedConverter() throws Exception { String localized = new LocalizedString(I18N.getLocale(), "Hello!").json().toString(); this.mockMvc.perform(post("/test/localized").param("localized", localized)).andExpect(status().isOk()) .andExpect(content().string(localized)); } @Test public void testLocalizedConverterWithBadString() throws Exception { String localized = "xpto"; this.mockMvc.perform(post("/test/localized").param("localized", localized)).andExpect(status().isBadRequest()); } @Test public void testLocalizedConverterWithNoString() throws Exception { this.mockMvc.perform(post("/test/localized")).andExpect(status().isBadRequest()); } }