Skip to content

Commit 35dd879

Browse files
Merge pull request #108 from bcgov/feature/enumCheck
Small fix for event catches
2 parents f868515 + e764016 commit 35dd879

6 files changed

Lines changed: 129 additions & 5 deletions

File tree

api/src/main/java/ca/bc/gov/educ/api/pen/replication/constants/EventOutcome.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,17 @@ public enum EventOutcome {
9393

9494
GRAD_STUDENT_CITIZENSHIP_UPDATED,
9595

96-
STUDENT_ADDRESS_UPDATED
96+
STUDENT_ADDRESS_UPDATED;
97+
98+
public static boolean isValid(String value) {
99+
if (value == null) {
100+
return false;
101+
}
102+
try {
103+
EventOutcome.valueOf(value);
104+
return true;
105+
} catch (IllegalArgumentException e) {
106+
return false;
107+
}
108+
}
97109
}

api/src/main/java/ca/bc/gov/educ/api/pen/replication/constants/EventType.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,17 @@ public enum EventType {
8686

8787
ADOPT_GRAD_STUDENT,
8888

89-
UPDATE_GRAD_STUDENT_CITIZENSHIP
89+
UPDATE_GRAD_STUDENT_CITIZENSHIP;
90+
91+
public static boolean isValid(String value) {
92+
if (value == null) {
93+
return false;
94+
}
95+
try {
96+
EventOutcome.valueOf(value);
97+
return true;
98+
} catch (IllegalArgumentException e) {
99+
return false;
100+
}
101+
}
90102
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ca.bc.gov.educ.api.pen.replication.exception;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class IgnoreEventException extends Exception {
7+
8+
private final String eventType;
9+
private final String eventOutcome;
10+
/**
11+
* The constant serialVersionUID.
12+
*/
13+
private static final long serialVersionUID = 5241655513745148898L;
14+
15+
/**
16+
* Instantiates a new Pen reg api runtime exception.
17+
*
18+
* @param message the message
19+
*/
20+
public IgnoreEventException(final String message, String eventType, String eventOutcome) {
21+
super(message);
22+
this.eventType = eventType;
23+
this.eventOutcome = eventOutcome;
24+
}
25+
}

api/src/main/java/ca/bc/gov/educ/api/pen/replication/messaging/jetstream/Subscriber.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package ca.bc.gov.educ.api.pen.replication.messaging.jetstream;
22

3+
import ca.bc.gov.educ.api.pen.replication.exception.IgnoreEventException;
34
import ca.bc.gov.educ.api.pen.replication.helpers.LogHelper;
45
import ca.bc.gov.educ.api.pen.replication.properties.ApplicationProperties;
56
import ca.bc.gov.educ.api.pen.replication.service.EventHandlerDelegatorService;
67
import ca.bc.gov.educ.api.pen.replication.struct.ChoreographedEvent;
7-
import ca.bc.gov.educ.api.pen.replication.util.JsonUtil;
8+
import ca.bc.gov.educ.api.pen.replication.util.EventUtils;
89
import com.google.common.util.concurrent.ThreadFactoryBuilder;
910
import io.nats.client.Connection;
1011
import io.nats.client.JetStreamApiException;
@@ -115,7 +116,7 @@ public void onMessage(final Message message) {
115116
try {
116117
val eventString = new String(message.getData());
117118
LogHelper.logMessagingEventDetails(eventString);
118-
final ChoreographedEvent event = JsonUtil.getJsonObjectFromString(ChoreographedEvent.class, eventString);
119+
final ChoreographedEvent event = EventUtils.getChoreographedEventIfValid(eventString);
119120
if (event.getEventPayload() == null) {
120121
message.ack();
121122
log.warn("payload is null, ignoring event :: {}", event);
@@ -129,10 +130,14 @@ public void onMessage(final Message message) {
129130
}
130131
});
131132
log.info("received event :: {} ", event);
132-
} catch (final Exception ex) {
133+
} catch (final IgnoreEventException ex) {
134+
log.warn("Ignoring event with type :: {} :: and event outcome :: {}", ex.getEventType(), ex.getEventOutcome());
135+
message.ack();
136+
}catch (final Exception ex) {
133137
log.error("Exception ", ex);
134138
}
135139
}
136140
}
141+
137142

138143
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package ca.bc.gov.educ.api.pen.replication.struct;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.util.UUID;
10+
11+
/**
12+
* The type Choreographed event.
13+
*/
14+
@Data
15+
@Builder
16+
@AllArgsConstructor
17+
@NoArgsConstructor
18+
@JsonIgnoreProperties(ignoreUnknown = true)
19+
public class ChoreographedEventValidation {
20+
/**
21+
* The Event id.
22+
*/
23+
UUID eventID;
24+
/**
25+
* The Event type.
26+
*/
27+
String eventType;
28+
/**
29+
* The Event outcome.
30+
*/
31+
String eventOutcome;
32+
/**
33+
* The Event payload.
34+
*/
35+
String eventPayload; // json string
36+
37+
/**
38+
* The Create user.
39+
*/
40+
String createUser;
41+
/**
42+
* The Update user.
43+
*/
44+
String updateUser;
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ca.bc.gov.educ.api.pen.replication.util;
2+
3+
import ca.bc.gov.educ.api.pen.replication.constants.EventOutcome;
4+
import ca.bc.gov.educ.api.pen.replication.constants.EventType;
5+
import ca.bc.gov.educ.api.pen.replication.exception.IgnoreEventException;
6+
import ca.bc.gov.educ.api.pen.replication.struct.ChoreographedEvent;
7+
import ca.bc.gov.educ.api.pen.replication.struct.ChoreographedEventValidation;
8+
import com.fasterxml.jackson.core.JsonProcessingException;
9+
import org.apache.commons.lang3.StringUtils;
10+
11+
12+
public final class EventUtils {
13+
private EventUtils() {
14+
}
15+
16+
public static ChoreographedEvent getChoreographedEventIfValid(String eventString) throws JsonProcessingException, IgnoreEventException {
17+
final ChoreographedEventValidation event = JsonUtil.getJsonObjectFromString(ChoreographedEventValidation.class, eventString);
18+
if(StringUtils.isNotBlank(event.getEventOutcome()) && !EventOutcome.isValid(event.getEventOutcome())) {
19+
throw new IgnoreEventException("Invalid event outcome", event.getEventType(), event.getEventOutcome());
20+
}else if(StringUtils.isNotBlank(event.getEventType()) && !EventType.isValid(event.getEventType())) {
21+
throw new IgnoreEventException("Invalid event type", event.getEventType(), event.getEventOutcome());
22+
}
23+
return JsonUtil.getJsonObjectFromString(ChoreographedEvent.class, eventString);
24+
}
25+
}

0 commit comments

Comments
 (0)