Created
September 5, 2012 15:12
-
-
Save bijukunjummen/3638121 to your computer and use it in GitHub Desktop.
UUID binding Test
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
package org.bk.webtestuuid; | |
import static org.hamcrest.Matchers.containsString; | |
import static org.springframework.test.web.server.request.MockMvcRequestBuilders.get; | |
import static org.springframework.test.web.server.result.MockMvcResultMatchers.content; | |
import static org.springframework.test.web.server.result.MockMvcResultMatchers.status; | |
import static org.springframework.test.web.server.setup.MockMvcBuilders.annotationConfigSetup; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.UUID; | |
import org.junit.Test; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.convert.converter.Converter; | |
import org.springframework.format.FormatterRegistry; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.test.web.server.MockMvc; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
public class UUIDConfigurationTest { | |
@Test | |
public void testAWebFlow() throws Exception { | |
MockMvc mockMvc = annotationConfigSetup(UUIDConfigurationTest.TestConfiguration.class) | |
.build(); | |
mockMvc.perform(get("/strings/1,2,3")) | |
.andExpect(status().isOk()) | |
.andExpect(content().string(containsString("\"1\""))); | |
mockMvc.perform(get("/uuids/48e977df-3c30-4988-af5d-2e2275945424,48e977df-3c30-4988-af5d-2e2275945424")) | |
.andExpect(status().isOk()) | |
.andExpect(content().string(containsString("\"48e977df-3c30-4988-af5d-2e2275945424\""))); | |
} | |
@Configuration | |
@EnableWebMvc | |
@ComponentScan(basePackages="org.bk.webtestuuid") | |
public static class TestConfiguration extends WebMvcConfigurerAdapter{ | |
public void addFormatters(FormatterRegistry registry) { | |
registry.addConverter(new UUIDConverter()); | |
} | |
} | |
} | |
@Controller | |
class UUIDController{ | |
@RequestMapping(value = "/strings/{strings}", method = RequestMethod.GET) | |
@ResponseBody | |
public List<String> strings(@PathVariable("strings") List<String> strings) { | |
return strings; | |
} | |
@RequestMapping(value = "/uuids/{uuids}", method = RequestMethod.GET) | |
@ResponseBody | |
public List<UUID> list(@PathVariable("uuids") UUID... uuids) { | |
return Arrays.asList(uuids); | |
} | |
} | |
class UUIDConverter implements Converter<String, UUID> { | |
@Override | |
public UUID convert(String source) { | |
return UUID.fromString(source); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment