Skip to content

Commit

Permalink
GRAD2-2648 (#346)
Browse files Browse the repository at this point in the history
* Added update cache for school and district UD events.

* Updated test cases for School and District CD

* Added Create District functionality

* Added Create District to event handler

* Added thread sleep for not fully processed school moved events.

* Changed exception handling

* Reducing code duplication

---------

Co-authored-by: chris.ditcher <[email protected]>
  • Loading branch information
cditcher and chris.ditcher authored Aug 14, 2024
1 parent 426e12f commit ff1215b
Show file tree
Hide file tree
Showing 16 changed files with 289 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public void handleEvent(@NonNull final EventEntity eventEntity) {
val schoolCreated = JsonUtil.getJsonObjectFromString(School.class, eventEntity.getEventPayload());
this.eventServiceMap.get(CREATE_SCHOOL.toString()).processEvent(schoolCreated, eventEntity);
}
case CREATE_DISTRICT -> {
val districtCreated = JsonUtil.getJsonObjectFromString(District.class, eventEntity.getEventPayload());
this.eventServiceMap.get(CREATE_DISTRICT.toString()).processEvent(districtCreated, eventEntity);
}
case MOVE_SCHOOL -> {
val schoolMoved = JsonUtil.getJsonObjectFromString(MoveSchoolData.class, eventEntity.getEventPayload());
this.eventServiceMap.get(MOVE_SCHOOL.toString()).processEvent(schoolMoved, eventEntity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.constant.EventType;
import ca.bc.gov.educ.api.trax.model.dto.DistrictContact;
import ca.bc.gov.educ.api.trax.model.entity.EventEntity;
import ca.bc.gov.educ.api.trax.service.institute.DistrictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DistrictContactCreatedService extends EventBaseService<DistrictContact> {
public class DistrictContactCreatedService extends DistrictContactEventBaseService {

@Override
public void processEvent(final DistrictContact districtContact, EventEntity eventEntity) {
log.debug("Processing District Contact Created");
// process the eventEntity here as per https://eccbc.atlassian.net/browse/GRAD2-2648
this.updateEvent(eventEntity);
@Autowired
public DistrictContactCreatedService(DistrictService districtService) {
super(districtService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.constant.EventType;
import ca.bc.gov.educ.api.trax.model.dto.DistrictContact;
import ca.bc.gov.educ.api.trax.model.entity.EventEntity;
import ca.bc.gov.educ.api.trax.service.institute.DistrictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DistrictContactDeletedService extends EventBaseService<DistrictContact> {
public class DistrictContactDeletedService extends DistrictContactEventBaseService {

@Override
public void processEvent(final DistrictContact districtContact, EventEntity eventEntity) {
log.debug("Processing District Contact Deleted");
// process the eventEntity here as per https://eccbc.atlassian.net/browse/GRAD2-2648
this.updateEvent(eventEntity);
@Autowired
public DistrictContactDeletedService(DistrictService districtService) {
super(districtService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ca.bc.gov.educ.api.trax.service;


import ca.bc.gov.educ.api.trax.exception.ServiceException;
import ca.bc.gov.educ.api.trax.model.dto.DistrictContact;
import ca.bc.gov.educ.api.trax.model.entity.EventEntity;
import ca.bc.gov.educ.api.trax.service.institute.DistrictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public abstract class DistrictContactEventBaseService extends EventBaseService<DistrictContact> {

protected DistrictService districtService;

protected DistrictContactEventBaseService(DistrictService districtService) {
this.districtService = districtService;
}

@Override
public void processEvent(DistrictContact districtContact, EventEntity eventEntity) {log.debug("Processing {}", eventEntity.getEventType());
try {
districtService.updateDistrictCache(districtContact.getDistrictId());
this.updateEvent(eventEntity);
} catch (ServiceException e) {
// do not mark eventEntity as processed
log.error(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.constant.EventType;
import ca.bc.gov.educ.api.trax.exception.ServiceException;
import ca.bc.gov.educ.api.trax.model.dto.institute.District;
import ca.bc.gov.educ.api.trax.model.entity.EventEntity;
import ca.bc.gov.educ.api.trax.service.institute.DistrictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DistrictCreatedService extends EventBaseService<District> {

DistrictService districtService;

@Autowired
public DistrictCreatedService(DistrictService districtService) {
this.districtService = districtService;
}

@Override
public void processEvent(final District district, EventEntity eventEntity) {
log.debug("Processing District Created");
try{
districtService.updateDistrictCache(district.getDistrictId());
this.updateEvent(eventEntity);
} catch (ServiceException e) {
log.error(e.getMessage());
}
}

@Override
public String getEventType() {
return EventType.CREATE_DISTRICT.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.constant.EventType;
import ca.bc.gov.educ.api.trax.model.dto.SchoolContact;
import ca.bc.gov.educ.api.trax.model.entity.EventEntity;
import ca.bc.gov.educ.api.trax.service.institute.SchoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SchoolContactCreatedService extends EventBaseService<SchoolContact> {
public class SchoolContactCreatedService extends SchoolContactEventBaseService {

@Override
public void processEvent(final SchoolContact schoolContact, EventEntity eventEntity) {
log.debug("Processing School Contact Created");
// process the eventEntity here as per https://eccbc.atlassian.net/browse/GRAD2-2648
this.updateEvent(eventEntity);
@Autowired
public SchoolContactCreatedService(SchoolService schoolService) {
super(schoolService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.service.institute.SchoolService;
import ca.bc.gov.educ.api.trax.constant.EventType;
import ca.bc.gov.educ.api.trax.model.dto.SchoolContact;
import ca.bc.gov.educ.api.trax.model.entity.EventEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SchoolContactDeletedService extends EventBaseService<SchoolContact> {
public class SchoolContactDeletedService extends SchoolContactEventBaseService {

@Override
public void processEvent(final SchoolContact districtContact, EventEntity eventEntity) {
log.debug("Processing School Contact Deleted");
// process the eventEntity here as per https://eccbc.atlassian.net/browse/GRAD2-2648
this.updateEvent(eventEntity);

@Autowired
public SchoolContactDeletedService(SchoolService schoolService) {
super(schoolService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ca.bc.gov.educ.api.trax.service;


import ca.bc.gov.educ.api.trax.exception.ServiceException;
import ca.bc.gov.educ.api.trax.model.dto.SchoolContact;
import ca.bc.gov.educ.api.trax.model.entity.EventEntity;
import ca.bc.gov.educ.api.trax.service.institute.SchoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public abstract class SchoolContactEventBaseService extends EventBaseService<SchoolContact> {

protected SchoolService schoolService;

protected SchoolContactEventBaseService(SchoolService schoolService) {
this.schoolService = schoolService;
}

@Override
public void processEvent(SchoolContact schoolContact, EventEntity eventEntity) {
log.debug("Processing {}", eventEntity.getEventType());
try {
schoolService.updateSchoolCache(schoolContact.getSchoolId());
this.updateEvent(eventEntity);
} catch (ServiceException e) {
// do not mark eventEntity as processed
log.error(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ public SchoolMovedService(SchoolService schoolService) {
public void processEvent(final MoveSchoolData moveSchoolData, EventEntity eventEntity) {
log.debug("Processing School Moved");
try{
// school move sometimes not fully processed, sleep 1 second
Thread.sleep(1000);
schoolService.updateSchoolCache(Arrays.asList(moveSchoolData.getFromSchoolId(), moveSchoolData.getToSchool().getSchoolId()));
this.updateEventWithHistory(eventEntity);
} catch (ServiceException e) {
log.error(e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ public void initializeSchoolDetailCache(boolean force) {
*/
public void updateSchoolCache(String schoolId) throws ServiceException {
// get details from institute
log.debug("Updating school %s in cache.", schoolId);
log.debug("Updating school {} in cache.", schoolId);
SchoolDetail schoolDetail = this.restService.get(String.format(constants.getSchoolDetailsByIdFromInstituteApiUrl(), schoolId),
SchoolDetail.class, webClient);
log.debug("Retrieved school: {} from Institute API", schoolDetail.getSchoolId());
schoolDetailRedisRepository.save(schoolDetailTransformer.transformToEntity(schoolDetail));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.exception.ServiceException;
import ca.bc.gov.educ.api.trax.service.institute.DistrictService;
import ca.bc.gov.educ.api.trax.support.TestUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import static org.assertj.core.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;

public class DistrictContactCreatedServiceTest extends BaseReplicationServiceTest {

@Autowired
private DistrictContactCreatedService districtContactCreatedService;

@MockBean
private DistrictService districtServiceMock;

@Test
public void testProcessEvent_givenCREATE_DISTRICT_CONTACT_Event_shouldProcessEvent() throws JsonProcessingException {
final var request = TestUtils.createDistrictContact();
Expand All @@ -25,4 +34,19 @@ public void testProcessEvent_givenCREATE_DISTRICT_CONTACT_Event_shouldProcessEve
}
}

@Test
public void testProcessEvent_givenCREATE_DISTRICT_CONTACT_Event_ServiceUnavailable_triggerError() throws JsonProcessingException {
final String ERROR_MSG = "Test Exception";
doThrow(new ServiceException(ERROR_MSG)).when(districtServiceMock).updateDistrictCache(anyString());
final var request = TestUtils.createDistrictContact();
final var event = TestUtils.createEvent("CREATE_DISTRICT_CONTACT", request, this.replicationTestUtils.getEventRepository());
this.districtContactCreatedService.processEvent(request, event);
var result = this.replicationTestUtils.getEventRepository().findById(event.getReplicationEventId());
if(result.isPresent()){
Assert.assertEquals("DB_COMMITTED", result.get().getEventStatus());
} else {
fail("CREATE_DISTRICT_CONTACT failed to process");
}
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.exception.ServiceException;
import ca.bc.gov.educ.api.trax.service.institute.DistrictService;
import ca.bc.gov.educ.api.trax.support.TestUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import static org.assertj.core.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;

public class DistrictContactDeletedServiceTest extends BaseReplicationServiceTest {

@Autowired
private DistrictContactDeletedService districtContactDeletedService;

@MockBean
private DistrictService districtServiceMock;

@Test
public void testProcessEvent_givenDELETE_DISTRICT_CONTACT_Event_shouldProcessEvent() throws JsonProcessingException {
final var request = TestUtils.createDistrictContact();
Expand All @@ -25,4 +34,19 @@ public void testProcessEvent_givenDELETE_DISTRICT_CONTACT_Event_shouldProcessEve
}
}

@Test
public void testProcessEvent_givenDELETE_DISTRICT_CONTACT_Event_ServiceUnavailable_triggerError() throws JsonProcessingException {
final String ERROR_MSG = "Test Exception";
doThrow(new ServiceException(ERROR_MSG)).when(districtServiceMock).updateDistrictCache(anyString());
final var request = TestUtils.createDistrictContact();
final var event = TestUtils.createEvent("DELETE_DISTRICT_CONTACT", request, this.replicationTestUtils.getEventRepository());
this.districtContactDeletedService.processEvent(request, event);
var result = this.replicationTestUtils.getEventRepository().findById(event.getReplicationEventId());
if(result.isPresent()){
Assert.assertEquals("DB_COMMITTED", result.get().getEventStatus());
} else {
fail("DELETE_DISTRICT_CONTACT failed to process");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public void testProcessEvent_givenUPDATE_DISTRICT_CONTACT_Event_ServiceUnavailab
final String ERROR_MSG = "Test Exception";
doThrow(new ServiceException(ERROR_MSG)).when(districtServiceMock).updateDistrictCache(anyString());
final var request = TestUtils.createDistrictContact();
final var event = TestUtils.createEvent("UPDATE_SCHOOL_CONTACT", request, this.replicationTestUtils.getEventRepository());
final var event = TestUtils.createEvent("UPDATE_DISTRICT_CONTACT", request, this.replicationTestUtils.getEventRepository());
this.districtContactUpdatedService.processEvent(request, event);
var result = this.replicationTestUtils.getEventRepository().findById(event.getReplicationEventId());
if(result.isPresent()){
Assert.assertEquals("DB_COMMITTED", result.get().getEventStatus());
} else {
fail("UPDATE_SCHOOL_CONTACT failed to process");
fail("UPDATE_DISTRICT_CONTACT failed to process");
}
}

Expand Down
Loading

0 comments on commit ff1215b

Please sign in to comment.