BAEL-5978: Case Insensitive Enum Mapping in Spring Boot (#13061)
* BAEL-5978: Case Insensitive Enum Mapping in Spring Boot * Move code to a new module: spring-boot-request-params
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.enummapping;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.enummapping.controllers.EnumMappingController;
|
||||
import com.baeldung.enummapping.enums.Level;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(EnumMappingController.class)
|
||||
public class EnumMappingIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void whenPassingLowerCaseEnumConstant_thenConvert() throws Exception {
|
||||
mockMvc.perform(get("/enummapping/get?level=medium"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(Level.MEDIUM.name()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassingUnknownEnumConstant_thenReturnUndefined() throws Exception {
|
||||
mockMvc.perform(get("/enummapping/get?level=unknown"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("undefined"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassingEmptyParameter_thenReturnUndefined() throws Exception {
|
||||
mockMvc.perform(get("/enummapping/get?level="))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("undefined"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassingNoParameter_thenReturnUndefined() throws Exception {
|
||||
mockMvc.perform(get("/enummapping/get"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("undefined"));
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.enummapping.converters;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.enummapping.EnumMappingMainApplication;
|
||||
import com.baeldung.enummapping.enums.Level;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = EnumMappingMainApplication.class)
|
||||
public class StringToLevelConverterIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ConversionService conversionService;
|
||||
|
||||
@Test
|
||||
public void whenConvertStringToLevelEnumUsingCustomConverter_thenSuccess() {
|
||||
assertThat(conversionService.convert("low", Level.class)).isEqualTo(Level.LOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringIsEmpty_thenReturnNull() {
|
||||
assertThat(conversionService.convert("", Level.class)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringIsNull_thenReturnNull() {
|
||||
assertThat(conversionService.convert(null, Level.class)).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.enummapping.editors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.enummapping.enums.Level;
|
||||
|
||||
public class LevelEditorIntegrationTest {
|
||||
|
||||
private final LevelEditor levelEditor = new LevelEditor();
|
||||
|
||||
@Test
|
||||
public void whenConvertStringToLevelEnumUsingCustomPropertyEditor_thenSuccess() {
|
||||
levelEditor.setAsText("lOw");
|
||||
|
||||
assertThat(levelEditor.getValue()).isEqualTo(Level.LOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringIsEmpty_thenReturnNull() {
|
||||
levelEditor.setAsText("");
|
||||
|
||||
assertThat(levelEditor.getValue()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringIsNull_thenReturnNull() {
|
||||
levelEditor.setAsText(null);
|
||||
|
||||
assertThat(levelEditor.getValue()).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user