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

GRAD2-2750 #413

Open
wants to merge 6 commits into
base: grad-release
Choose a base branch
from
Open
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
@@ -0,0 +1,7 @@
package ca.bc.gov.educ.api.trax.constant;

public enum EventHistoryType {
SCHOOL,
DISTRICT,
INDEPENDENT_AUTHORITY
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.educ.api.trax.mapper;

import ca.bc.gov.educ.api.trax.constant.EventHistoryType;
import ca.bc.gov.educ.api.trax.constant.EventType;
import ca.bc.gov.educ.api.trax.model.dto.AuthorityContact;
import ca.bc.gov.educ.api.trax.model.dto.DistrictContact;
Expand All @@ -12,11 +13,14 @@
import ca.bc.gov.educ.api.trax.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.tuple.Pair;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

@Slf4j
@Mapper(componentModel = "spring", uses = {EventMapper.class, UUIDMapper.class})
public abstract class EventHistoryMapper {
Expand All @@ -28,41 +32,69 @@ public void setConstants(EducGradTraxApiConstants constants){
this.constants = constants;
}

@Mapping(source = "event", target = "eventHistoryUrl", qualifiedByName = "getUrlFromEventHistoryEntity")
@Mapping(source = "event", target = "eventHistoryUrl", qualifiedByName = "getUrlFromEventEntity")
@Mapping(source = "event", target = "instituteId", qualifiedByName = "getInstituteIdFromEventEntity")
public abstract EventHistory toStructure(EventHistoryEntity eventHistoryEntity);

@Mapping(target = "event.eventPayloadBytes", ignore = true)
public abstract EventHistoryEntity toEntity(EventHistory eventHistory);

@Named("getUrlFromEventHistoryEntity")
String getUrlFromEventHistoryEntity(EventEntity eventEntity) {
String url = null;
@Named("getInstituteIdFromEventEntity")
UUID getInstituteIdFromEventEntity(EventEntity eventEntity){
return this.getTypeAndIdFromEventEntity(eventEntity).getRight();
}

@Named("getUrlFromEventEntity")
String getUrlFromEventEntity(EventEntity eventEntity) {
Pair<EventHistoryType, UUID> evenHistoryPair = this.getTypeAndIdFromEventEntity(eventEntity);
try {
switch (evenHistoryPair.getLeft()) {
case SCHOOL -> {
return this.getStudentAdminSchoolDetailsUrl(evenHistoryPair.getRight().toString());
}
case DISTRICT -> {
return this.getStudentAdminDistrictDetailsUrl(evenHistoryPair.getRight().toString());
}
case INDEPENDENT_AUTHORITY -> {
return this.getStudentAdminAuthorityDetailsUrl(evenHistoryPair.getRight().toString());
}
default -> {
return null;
}
}
} catch (final Exception exception) {
log.error(exception.getMessage());
}
return null;
}

private Pair<EventHistoryType, UUID> getTypeAndIdFromEventEntity(EventEntity eventEntity) {
if (eventEntity != null) {
try {
switch (EventType.valueOf(eventEntity.getEventType())) {
case CREATE_SCHOOL, UPDATE_SCHOOL -> {
val school = JsonUtil.getJsonObjectFromString(ca.bc.gov.educ.api.trax.model.dto.institute.School.class, eventEntity.getEventPayload());
url = this.getStudentAdminSchoolDetailsUrl(school.getSchoolId());
return Pair.of(EventHistoryType.SCHOOL, UUID.fromString(school.getSchoolId()));
}
case MOVE_SCHOOL -> {
val schoolMoved = JsonUtil.getJsonObjectFromString(MoveSchoolData.class, eventEntity.getEventPayload());
url = this.getStudentAdminSchoolDetailsUrl(schoolMoved.getToSchool().getSchoolId());
return Pair.of(EventHistoryType.SCHOOL, UUID.fromString(schoolMoved.getToSchool().getSchoolId()));
}
case CREATE_SCHOOL_CONTACT, UPDATE_SCHOOL_CONTACT, DELETE_SCHOOL_CONTACT -> {
val schoolContact = JsonUtil.getJsonObjectFromString(SchoolContact.class, eventEntity.getEventPayload());
url = this.getStudentAdminSchoolDetailsUrl(schoolContact.getSchoolId());
return Pair.of(EventHistoryType.SCHOOL, UUID.fromString(schoolContact.getSchoolId()));
}
case CREATE_DISTRICT, UPDATE_DISTRICT -> {
val district = JsonUtil.getJsonObjectFromString(ca.bc.gov.educ.api.trax.model.dto.institute.District.class, eventEntity.getEventPayload());
url = this.getStudentAdminDistrictDetailsUrl(district.getDistrictId());
return Pair.of(EventHistoryType.DISTRICT, UUID.fromString(district.getDistrictId()));
}
case CREATE_AUTHORITY_CONTACT, UPDATE_AUTHORITY_CONTACT, DELETE_AUTHORITY_CONTACT -> {
val authorityContact = JsonUtil.getJsonObjectFromString(AuthorityContact.class, eventEntity.getEventPayload());
url = this.getStudentAdminAuthorityDetailsUrl(authorityContact.getIndependentAuthorityId());
return Pair.of(EventHistoryType.INDEPENDENT_AUTHORITY, UUID.fromString(authorityContact.getIndependentAuthorityId()));
}
case CREATE_DISTRICT_CONTACT, UPDATE_DISTRICT_CONTACT, DELETE_DISTRICT_CONTACT -> {
val districtContact = JsonUtil.getJsonObjectFromString(DistrictContact.class, eventEntity.getEventPayload());
url = this.getStudentAdminDistrictDetailsUrl(districtContact.getDistrictId());
return Pair.of(EventHistoryType.DISTRICT, UUID.fromString(districtContact.getDistrictId()));
}
default -> {
return null;
Expand All @@ -72,7 +104,7 @@ String getUrlFromEventHistoryEntity(EventEntity eventEntity) {
log.error(exception.getMessage());
}
}
return url;
return null;
}

private String getStudentAdminSchoolDetailsUrl(String schoolId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class EventHistory extends BaseModel {
private UUID id;
private Event event;
private String eventHistoryUrl;
private UUID instituteId;
@Pattern(regexp = "^[Yy|Nn]$", message = "acknowledge flag must be either Y or N.")
private String acknowledgeFlag;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
@Builder
@Entity
@Table(name = "REPLICATION_EVENT")
@Data
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@DynamicUpdate
public class EventEntity {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

import java.util.UUID;

@Data
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
Expand Down
29 changes: 23 additions & 6 deletions api/src/test/java/ca/bc/gov/educ/api/trax/mapper/MapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.test.context.ActiveProfiles;

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest(classes = { EducGradTraxApiApplication.class })
@ActiveProfiles("test")
@ExtendWith(OutputCaptureExtension.class)
class MapperTest extends BaseEventHistoryTest {

@Autowired
Expand All @@ -50,7 +56,7 @@ void testUUIDMapper_givenValidUUID_shouldReturnString() {

@Test
void testUUIDMapper_givenBlankString_shouldReturnNull() {
Assertions.assertNull(uuidMapper.map(""));
assertNull(uuidMapper.map(""));
}


Expand All @@ -59,39 +65,50 @@ void testToEventHistory_givenSchoolEvent_shouldReturnCorrectUrl() throws JsonPro
School school = TestUtils.createSchool();
Pair<String, EventHistoryEntity> pair = createUrlAndEntity("CREATE_SCHOOL", school, school.getSchoolId());
EventHistory eventHistory = eventHistoryMapper.toStructure(pair.getRight());
Assertions.assertEquals(true, eventHistory.getEventHistoryUrl().equals(pair.getLeft()));
assertEquals(true, eventHistory.getEventHistoryUrl().equals(pair.getLeft()));
}

@Test
void testToEventHistory_givenMoveSchoolEvent_shouldReturnCorrectUrl() throws JsonProcessingException {
MoveSchoolData school = TestUtils.createMoveSchoolData();
Pair<String, EventHistoryEntity> pair = createUrlAndEntity("MOVE_SCHOOL", school, school.getToSchool().getSchoolId());
EventHistory eventHistory = eventHistoryMapper.toStructure(pair.getRight());
Assertions.assertEquals(true, eventHistory.getEventHistoryUrl().equals(pair.getLeft()));
assertEquals(true, eventHistory.getEventHistoryUrl().equals(pair.getLeft()));
}

@Test
void testToEventHistory_givenCreateDistrictEvent_shouldReturnCorrectUrl() throws JsonProcessingException {
District district = TestUtils.createDistrict();
Pair<String, EventHistoryEntity> pair = createUrlAndEntity("CREATE_DISTRICT", district, district.getDistrictId());
EventHistory eventHistory = eventHistoryMapper.toStructure(pair.getRight());
Assertions.assertEquals(true, eventHistory.getEventHistoryUrl().equals(pair.getLeft()));
assertEquals(true, eventHistory.getEventHistoryUrl().equals(pair.getLeft()));
}

@Test
void testToEventHistory_givenCreateDistrictContact_shouldReturnCorrectUrl() throws JsonProcessingException {
District district = TestUtils.createDistrict();
Pair<String, EventHistoryEntity> pair = createUrlAndEntity("CREATE_DISTRICT_CONTACT", district, district.getDistrictId());
EventHistory eventHistory = eventHistoryMapper.toStructure(pair.getRight());
Assertions.assertEquals(true, eventHistory.getEventHistoryUrl().equals(pair.getLeft()));
assertEquals(true, eventHistory.getEventHistoryUrl().equals(pair.getLeft()));
}

@Test
void testToEventHistory_givenCreateAuthorityContact_shouldReturnCorrectUrl() throws JsonProcessingException {
AuthorityContact authorityContact = TestUtils.createAuthorityContact();
Pair<String, EventHistoryEntity> pair = createUrlAndEntity("CREATE_AUTHORITY_CONTACT", authorityContact, authorityContact.getIndependentAuthorityId());
EventHistory eventHistory = eventHistoryMapper.toStructure(pair.getRight());
Assertions.assertEquals(true, eventHistory.getEventHistoryUrl().equals(pair.getLeft()));
assertEquals(true, eventHistory.getEventHistoryUrl().equals(pair.getLeft()));
}

@Test
void getUrlFromEventEntity_givenNullEventEntity_shouldReturnNull() {
assertNull(eventHistoryMapper.getUrlFromEventEntity(null));
}

@Test
void getUrlFromEventEntity_givenNullEventEntity_shouldLogNullPointerError(CapturedOutput capturedOutput) {
eventHistoryMapper.getUrlFromEventEntity(null);
assertTrue(capturedOutput.getAll().contains("Cannot invoke \"org.apache.commons.lang3.tuple.Pair.getLeft()\" because \"evenHistoryPair\" is null"));
}

private Pair<String, EventHistoryEntity> createUrlAndEntity(String eventType, Object eventPayload, String id) throws JsonProcessingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,22 @@ public abstract class BaseEventHistoryTest {
protected EventEntity createEventData() {
return EventEntity.builder()
.eventId(UUID.randomUUID())
.eventPayload("")
.eventPayload("""
{
"schoolContactId": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
"schoolId": "d3b07384-d9a0-4c1e-8b0e-6e2b6b7b8b8b",
"schoolContactTypeCode": "string",
"phoneNumber": "string",
"jobTitle": "string",
"phoneExtension": "string",
"alternatePhoneNumber": "string",
"alternatePhoneExtension": "string",
"email": "string",
"firstName": "string",
"lastName": "string",
"effectiveDate": "string",
"expiryDate": "string"
}""")
.eventStatus("PROCESSED")
.eventType("CREATE_SCHOOL_CONTACT")
.eventOutcome("SCHOOL_CONTACT_CREATED")
Expand Down