Skip to content

Commit

Permalink
Merge pull request #1171 from OSGP/feature/scheduled-task-has-no-devi…
Browse files Browse the repository at this point in the history
…cemodelcode

Feature/scheduled task has no devicemodelcode
  • Loading branch information
harrymiddelburgetall authored Feb 19, 2024
2 parents 4e93866 + 8ba2984 commit 5bdaefb
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ private static MessageMetadata messageMetadataFromScheduledTaskForDevice(
.withNetworkAddress(getIpAddress(device))
.withNetworkSegmentIds(device.getBtsId(), device.getCellId())
.withMessagePriority(scheduledTask.getMessagePriority())
.withDeviceModelCode(scheduledTask.getDeviceModelCode())
.withScheduled(true)
.withMaxScheduleTime(
scheduledTask.getMaxScheduleTime() == null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DO
$$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE TABLE_SCHEMA = current_schema AND TABLE_NAME = 'scheduled_task' AND COLUMN_NAME = 'device_model_code')
THEN
ALTER TABLE scheduled_task ADD COLUMN device_model_code VARCHAR(1279);
END IF;

COMMENT ON COLUMN scheduled_task.device_model_code IS 'comma separated list of devicemodel codes. First modelcode is the gateway device and subsequently the mbus devicess on channel 1 to 4 (size:(5*255)+4)';
END;
$$
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -59,6 +60,8 @@ public class ScheduledTaskExecutorServiceTest {

@Captor private ArgumentCaptor<List<ScheduledTask>> scheduledTaskCaptor;

@Captor private ArgumentCaptor<ProtocolRequestMessage> protocolRequestMessageCaptor;

/**
* Test the scheduled task runner for the case when the deviceRequestMessageService gives a
* functional exception
Expand Down Expand Up @@ -125,6 +128,41 @@ void testRetryStrandedPendingTask() {
assertThat(deleteScheduledTasks).hasSize(3);
}

@Test
void testMetadataOfScheduledTaskToRetryRequest() throws FunctionalException {
final String deviceIdentification = "device-1";
final String deviceModelCode = "E,M1,M2,M3,M4";
final MessageMetadata messageMetadata =
this.createMessageMetadata(deviceIdentification, deviceModelCode);
final ScheduledTask scheduledTask =
new ScheduledTask(messageMetadata, DOMAIN, DOMAIN, DATA_OBJECT, INITIAL_SCHEDULED_TIME);
final Device device = new Device();

when(this.scheduledTaskExecutorJobConfig.scheduledTaskPendingDurationMaxSeconds())
.thenReturn(-1L);
when(this.scheduledTaskExecutorJobConfig.scheduledTaskPageSize()).thenReturn(30);
when(this.scheduledTaskRepository.save(scheduledTask)).thenReturn(scheduledTask);
when(this.deviceRepository.findByDeviceIdentification(deviceIdentification)).thenReturn(device);
when(this.scheduledTaskRepository.findByStatusAndScheduledTimeLessThan(
eq(ScheduledTaskStatusType.PENDING), any(Timestamp.class), any(Pageable.class)))
.thenReturn(new ArrayList<>());
when(this.scheduledTaskRepository.findByStatusAndScheduledTimeLessThan(
eq(ScheduledTaskStatusType.NEW), any(Timestamp.class), any(Pageable.class)))
.thenReturn(List.of(scheduledTask), Collections.emptyList());
when(this.scheduledTaskRepository.findByStatusAndScheduledTimeLessThan(
eq(ScheduledTaskStatusType.RETRY), any(Timestamp.class), any(Pageable.class)))
.thenReturn(new ArrayList<>());
this.scheduledTaskExecutorService.processScheduledTasks();

verify(this.deviceRequestMessageService)
.processMessage(this.protocolRequestMessageCaptor.capture());
final ProtocolRequestMessage protocolRequestMessage =
this.protocolRequestMessageCaptor.getValue();
assertThat(protocolRequestMessage).isNotNull();
assertThat(protocolRequestMessage.getDeviceIdentification()).isEqualTo(deviceIdentification);
assertThat(protocolRequestMessage.getDeviceModelCode()).isEqualTo(deviceModelCode);
}

private void whenFindByStatusAndScheduledTime(
final List<ScheduledTask> pendingTasks,
final List<ScheduledTask> newTasks,
Expand Down Expand Up @@ -179,7 +217,15 @@ private MessageMetadata createExpiredMessageMetadata() {
}

private MessageMetadata createMessageMetadata() {
return this.createMessageMetadataBuilder().withDeviceIdentification("retryable").build();
return this.createMessageMetadata("retryable", null);
}

private MessageMetadata createMessageMetadata(
final String deviceIdentification, final String deviceModelCode) {
return this.createMessageMetadataBuilder()
.withDeviceIdentification(deviceIdentification)
.withDeviceModelCode(deviceModelCode)
.build();
}

private MessageMetadata.Builder createMessageMetadataBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public abstract class AbstractScheduledTask extends AbstractEntity {
@Column(name = "retry")
protected int retry;

@Column(name = "device_model_code", length = 1279)
protected String deviceModelCode;

AbstractScheduledTask() {
// Default empty constructor for Hibernate.
}
Expand All @@ -73,6 +76,7 @@ protected AbstractScheduledTask(
this.deviceIdentification = messageMetadata.getDeviceIdentification();
this.messageType = messageMetadata.getMessageType();
this.messagePriority = messageMetadata.getMessagePriority();
this.deviceModelCode = messageMetadata.getDeviceModelCode();
this.domain = domain;
this.domainVersion = domainVersion;
this.scheduledTime = (Timestamp) scheduledTime.clone();
Expand Down Expand Up @@ -105,6 +109,10 @@ public String getDeviceIdentification() {
return this.deviceIdentification;
}

public String getDeviceModelCode() {
return this.deviceModelCode;
}

public String getErrorLog() {
return this.errorLog;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public Builder maxScheduleTime(final Long maxScheduleTime) {
return this;
}

public Builder deviceModelCode(final String deviceModelCode) {
this.superBuilder.withDeviceModelCode(deviceModelCode);
return this;
}

public Builder retryCount(final int retryCount) {
this.retryCount = retryCount;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ResponseMessage implements Serializable {
private final int messagePriority;
private final boolean scheduled;
private final Long maxScheduleTime;
private final String deviceModelCode;
private final boolean bypassRetry;

private final RetryHeader retryHeader;
Expand All @@ -42,6 +43,7 @@ protected ResponseMessage(final Builder builder) {
this.messagePriority = builder.messagePriority;
this.scheduled = builder.scheduled;
this.maxScheduleTime = builder.maxScheduleTime;
this.deviceModelCode = builder.deviceModelCode;
this.bypassRetry = builder.bypassRetry;
this.retryHeader = builder.retryHeader;
this.result = builder.result;
Expand Down Expand Up @@ -85,6 +87,7 @@ public MessageMetadata messageMetadata() {
.withMessagePriority(this.messagePriority)
.withScheduled(this.scheduled)
.withMaxScheduleTime(this.maxScheduleTime)
.withDeviceModelCode(this.deviceModelCode)
.withBypassRetry(this.bypassRetry)
.withTopic(this.topic)
.build();
Expand All @@ -106,6 +109,7 @@ public static class Builder {
private int messagePriority = MessagePriorityEnum.DEFAULT.getPriority();
private boolean scheduled = false;
private Long maxScheduleTime = null;
private String deviceModelCode = null;
private boolean bypassRetry = DEFAULT_BYPASS_RETRY;
private RetryHeader retryHeader;
private String topic = null;
Expand Down Expand Up @@ -172,6 +176,11 @@ public Builder withMaxScheduleTime(final Long maxScheduleTime) {
return this;
}

public Builder withDeviceModelCode(final String deviceModelCode) {
this.deviceModelCode = deviceModelCode;
return this;
}

public Builder withRetryHeader(final RetryHeader retryHeader) {
this.retryHeader = retryHeader;
return this;
Expand All @@ -190,6 +199,7 @@ public Builder withMessageMetadata(final MessageMetadata messageMetadata) {
this.messagePriority = messageMetadata.getMessagePriority();
this.scheduled = messageMetadata.isScheduled();
this.maxScheduleTime = messageMetadata.getMaxScheduleTime();
this.deviceModelCode = messageMetadata.getDeviceModelCode();
this.bypassRetry = messageMetadata.isBypassRetry();
this.retryHeader = new RetryHeader();
this.topic = messageMetadata.getTopic();
Expand Down

0 comments on commit 5bdaefb

Please sign in to comment.