Skip to content

Instantly share code, notes, and snippets.

@bijukunjummen
Created September 5, 2012 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bijukunjummen/3638121 to your computer and use it in GitHub Desktop.
Save bijukunjummen/3638121 to your computer and use it in GitHub Desktop.
UUID binding Test
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