Created
November 27, 2018 06:21
-
-
Save kwon37xi/aa359e364b7e81f79085fb04cc710037 to your computer and use it in GitHub Desktop.
SpringBoot MVC foramtter, Jackson java 8 LocalDateTime/OffSetDateTime/ZonedDateTime serializer/deserializer 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
import org.slf4j.Logger | |
import org.slf4j.LoggerFactory | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest | |
import org.springframework.http.MediaType | |
import org.springframework.test.web.servlet.MockMvc | |
import org.springframework.test.web.servlet.MvcResult | |
import org.springframework.web.bind.annotation.* | |
import spock.lang.Specification | |
import java.time.LocalDate | |
import java.time.LocalDateTime | |
import java.time.LocalTime | |
import java.time.OffsetDateTime | |
import java.time.ZonedDateTime | |
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 | |
/** | |
for Spring Boot 2.x or later. | |
Formatter/Jackson java 8 datetime serializers/deserializers test. | |
*/ | |
@WebMvcTest(controllers = FormatterSerializerTestControllerTest.FormatterSerializerTestController) | |
class FormatterSerializerTestControllerTest extends Specification { | |
@Autowired | |
MockMvc mockMvc | |
def "localDateTimeParameter - LocalDateTime parameter ISO_LOCAL_DATE_TIME format"() { | |
when: | |
MvcResult mvcResult = mockMvc.perform(get("/configtest/localDateTimeParameter?localDateTime=2018-12-17T14:21:47.123456789")) | |
.andExpect(status().is2xxSuccessful()) | |
.andReturn() | |
then: | |
mvcResult.response.contentAsString == '"2018-12-17T14:21:47.123456789"' | |
} | |
def "localDate- LocalDate parameter ISO_LOCAL_DATE format"() { | |
when: | |
MvcResult mvcResult = mockMvc.perform(get("/configtest/localDateParameter?localDate=1997-02-06")) | |
.andExpect(status().is2xxSuccessful()) | |
.andReturn() | |
then: | |
mvcResult.response.contentAsString == '"1997-02-06"' | |
} | |
def "jackson localdatetime serializer/deserialize"() { | |
expect: | |
mockMvc.perform( | |
post("/configtest/jackson/datetimes") | |
.contentType(MediaType.APPLICATION_JSON) | |
.content('''{ | |
"localDateTime": "2016-09-03T22:04:08.123123123", | |
"localDate": "1088-01-31", | |
"localTime": "15:11:49.693", | |
"date": "2000-11-22T10:15:21.123+09:00", | |
"offsetDateTime": "2000-11-22T10:15:21.123123123+09:00", | |
"zonedDateTime": "2018-07-18T15:11:49.123123123+09:00" | |
}''')) | |
.andExpect(status().is2xxSuccessful()) | |
.andExpect(content().json('''{ | |
"localDateTime": "2016-09-03T22:04:08.123123123", | |
"localDate": "1088-01-31", | |
"localTime": "15:11:49.693", | |
"date": "2000-11-22T10:15:21.123+09:00", | |
"offsetDateTime": "2000-11-22T10:15:21.123123123+09:00", | |
"zonedDateTime": "2018-07-18T15:11:49.123123123+09:00" | |
}''')) | |
} | |
@RestController | |
@RequestMapping("/configtest") | |
static class FormatterSerializerTestController { | |
static Logger log = LoggerFactory.getLogger(this) | |
@GetMapping("/localDateTimeParameter") | |
LocalDateTime localDateTimeParameter(@RequestParam("localDateTime") LocalDateTime localDateTime) { | |
log.info("localDateTime: {}", localDateTime) | |
return localDateTime | |
} | |
@GetMapping("/localDateParameter") | |
LocalDate localDateParameter(@RequestParam("localDate") LocalDate localDate) { | |
log.info("localDate: {}", localDate) | |
return localDate | |
} | |
@PostMapping("/jackson/datetimes") | |
Map<String, Object> jacksonDateTimes(@RequestBody DateTimeRequest request) { | |
return [ | |
"localDateTime" : request.localDateTime, | |
"localDate" : request.localDate, | |
"localTime" : request.localTime, | |
"date" : request.date, | |
"offsetDateTime": request.offsetDateTime, | |
"zonedDateTime" : request.zonedDateTime | |
] | |
} | |
} | |
static class DateTimeRequest { | |
LocalDateTime localDateTime | |
LocalDate localDate | |
LocalTime localTime | |
Date date | |
OffsetDateTime offsetDateTime | |
ZonedDateTime zonedDateTime | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment