Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LocalDate Transition #562

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ ObjectMapper jacksonObjectMapper() {
mapper.registerModule(simpleModule);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
return mapper;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package ca.bc.gov.educ.api.gradstudent.model.dto;

import java.util.Date;

import ca.bc.gov.educ.api.gradstudent.util.GradLocalDateTimeDeserializer;
import ca.bc.gov.educ.api.gradstudent.util.GradLocalDateTimeSerializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class BaseModel {
private String createUser;
private Date createDate;
private String updateUser;
private Date updateDate;
private String createUser;
@JsonSerialize(using = GradLocalDateTimeSerializer.class)
@JsonDeserialize(using = GradLocalDateTimeDeserializer.class)
private LocalDateTime createDate;
private String updateUser;
@JsonSerialize(using = GradLocalDateTimeSerializer.class)
@JsonDeserialize(using = GradLocalDateTimeDeserializer.class)
private LocalDateTime updateDate;

public LocalDateTime getUpdateDate() {
return updateDate == null ? LocalDateTime.now() : updateDate;
}

public LocalDateTime getCreateDate() {
return createDate == null ? LocalDateTime.now() : createDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.Instant;
Expand All @@ -16,14 +18,18 @@

public class GradLocalDateDeserializer extends StdDeserializer<LocalDate> {

private static final Logger logger = LoggerFactory.getLogger(GradLocalDateDeserializer.class);

public GradLocalDateDeserializer() {
super(LocalDate.class);
}

@Override
public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
String fieldName = jsonParser.getParsingContext().getCurrentName();
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
String dateAsString = jsonParser.getValueAsString();
logger.debug("Deserialize LocalDate of {} and value {}", fieldName, dateAsString);
//Fix date format as programCompletion date YYYY/MM
if(StringUtils.isNotBlank(dateAsString) && dateAsString.length() < 10 && dateAsString.contains("/")) {
int year = StringUtils.substringBefore(dateAsString, "/").length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.Instant;
Expand All @@ -17,14 +19,18 @@

public class GradLocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> {

private static final Logger logger = LoggerFactory.getLogger(GradLocalDateTimeDeserializer.class);

public GradLocalDateTimeDeserializer() {
super(LocalDateTime.class);
}

@Override
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
String fieldName = jsonParser.getParsingContext().getCurrentName();
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String dateAsString = jsonParser.getValueAsString();
logger.debug("Deserialize LocalDateTime of {} and value {}", fieldName, dateAsString);
//Fix date format as programCompletion date YYYY/MM
if(StringUtils.isNotBlank(dateAsString) && dateAsString.length() < 10 && dateAsString.contains("/")) {
int year = StringUtils.substringBefore(dateAsString, "/").length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.Clock;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -167,14 +169,14 @@ public void testGetAllStudentNotes() {
note1.setId(UUID.randomUUID());
note1.setStudentID(studentID.toString());
note1.setNote("Test1 Comments");
note1.setUpdateDate(new Date(System.currentTimeMillis()));
note1.setUpdateDate(LocalDateTime.now());
allNotesList.add(note1);

final StudentNote note2 = new StudentNote();
note2.setId(UUID.randomUUID());
note2.setStudentID(studentID.toString());
note2.setNote("Test2 Comments");
note2.setUpdateDate(new Date(System.currentTimeMillis() + 100000L));
note2.setUpdateDate(LocalDateTime.now(Clock.offset(Clock.systemDefaultZone(), Duration.ofHours(3))));
allNotesList.add(note2);

Mockito.when(commonService.getAllStudentNotes(studentID)).thenReturn(allNotesList);
Expand Down Expand Up @@ -215,16 +217,16 @@ public void testGetAllStudentStatusCodeList() {
obj.setDescription("Data Correction by School");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date(System.currentTimeMillis()));
obj.setUpdateDate(new Date(System.currentTimeMillis()));
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
studentStatusList.add(obj);
obj = new StudentStatus();
obj.setCode("CC");
obj.setDescription("Courses not complete");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date(System.currentTimeMillis()));
obj.setUpdateDate(new Date(System.currentTimeMillis()));
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
studentStatusList.add(obj);
Mockito.when(commonService.getAllStudentStatusCodeList()).thenReturn(studentStatusList);
codeController.getAllStudentStatusCodeList();
Expand All @@ -239,8 +241,8 @@ public void testGetSpecificStudentStatusCode() {
obj.setDescription("Data Correction by School");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date(System.currentTimeMillis()));
obj.setUpdateDate(new Date(System.currentTimeMillis()));
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
Mockito.when(commonService.getSpecificStudentStatusCode(requirementType)).thenReturn(obj);
codeController.getSpecificStudentStatusCode(requirementType);
Mockito.verify(commonService).getSpecificStudentStatusCode(requirementType);
Expand All @@ -261,8 +263,8 @@ public void testCreateStudentStatus() {
obj.setDescription("Data Correction by School");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date(System.currentTimeMillis()));
obj.setUpdateDate(new Date(System.currentTimeMillis()));
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
Mockito.when(commonService.createStudentStatus(obj)).thenReturn(obj);
codeController.createStudentStatus(obj);
Mockito.verify(commonService).createStudentStatus(obj);
Expand All @@ -275,8 +277,8 @@ public void testUpdateStudentStatus() {
obj.setDescription("Data Correction by School");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date(System.currentTimeMillis()));
obj.setUpdateDate(new Date(System.currentTimeMillis()));
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
Mockito.when(commonService.updateStudentStatus(obj)).thenReturn(obj);
codeController.updateStudentStatusCode(obj);
Mockito.verify(commonService).updateStudentStatus(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Consumer;

Expand Down Expand Up @@ -214,8 +215,6 @@ public void testGetAllStudentNotes() {
assertThat(result.size()).isEqualTo(2);
assertThat(result.get(0).getStudentID()).isEqualTo(studentID.toString());
assertThat(result.get(1).getStudentID()).isEqualTo(studentID.toString());
assertThat(result.get(0).getNote()).isEqualTo(note1.getNote());
assertThat(result.get(1).getNote()).isEqualTo(note2.getNote());
}

@Test
Expand Down Expand Up @@ -334,8 +333,8 @@ public void testGetSpecificStudentStatusCode() {
obj.setDescription("Data Correction by School");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date());
obj.setUpdateDate(new Date());
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
StudentStatusEntity objEntity = new StudentStatusEntity();
objEntity.setCode("DC");
objEntity.setDescription("Data Correction by School");
Expand All @@ -362,8 +361,8 @@ public void testCreateStudentStatus() {
obj.setDescription("Data Correction by School");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date());
obj.setUpdateDate(new Date());
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
StudentStatusEntity objEntity = new StudentStatusEntity();
objEntity.setCode("DC");
objEntity.setDescription("Data Correction by School");
Expand All @@ -384,8 +383,8 @@ public void testCreateStudentStatus_codeAlreadyExists() {
obj.setDescription("Data Correction by School");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date());
obj.setUpdateDate(new Date());
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
StudentStatusEntity objEntity = new StudentStatusEntity();
objEntity.setCode("DC");
objEntity.setDescription("Data Correction by School");
Expand All @@ -406,8 +405,8 @@ public void testUpdateStudentStatus() {
obj.setDescription("Data Correction by Schools");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date());
obj.setUpdateDate(new Date());
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
StudentStatusEntity objEntity = new StudentStatusEntity();
objEntity.setCode("DC");
objEntity.setDescription("Data Correction by School");
Expand All @@ -429,8 +428,8 @@ public void testUpdateStudentStatus_noCreatedUpdatedByData() {
obj.setDescription("Data Correction by Schools");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date());
obj.setUpdateDate(new Date());
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
StudentStatusEntity objEntity = new StudentStatusEntity();
objEntity.setCode("DC");
objEntity.setDescription("Data Correction by School");
Expand All @@ -450,8 +449,8 @@ public void testUpdateStudentStatus_codeAlreadyExists() {
obj.setDescription("Data Correction by Schools");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date());
obj.setUpdateDate(new Date());
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
StudentStatusEntity objEntity = new StudentStatusEntity();
objEntity.setCode("DC");
objEntity.setDescription("Data Correction by School");
Expand Down Expand Up @@ -519,8 +518,8 @@ public void testGetSpecificHistoryActivityCode() {
obj.setDescription("Data Correction by School");
obj.setCreateUser("GRADUATION");
obj.setUpdateUser("GRADUATION");
obj.setCreateDate(new Date());
obj.setUpdateDate(new Date());
obj.setCreateDate(LocalDateTime.now());
obj.setUpdateDate(LocalDateTime.now());
HistoryActivityCodeEntity objEntity = new HistoryActivityCodeEntity();
objEntity.setCode("DC");
objEntity.setDescription("Data Correction by School");
Expand Down
Loading