Skip to content

Commit

Permalink
make module id or module name required
Browse files Browse the repository at this point in the history
update integration tests
  • Loading branch information
dmtkachenko committed Dec 23, 2024
1 parent 18133cb commit a2876e8
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,13 @@
@Mapper(componentModel = "spring")
public interface TimerDescriptorMapper {

/**
* Converts {@link TimerDescriptorEntity} to {@link TimerDescriptor} object.
*
* @param entity - {@link TimerDescriptorEntity} object
* @return converted {@link TimerDescriptor} object
*/
TimerDescriptor convert(TimerDescriptorEntity entity);

/**
* Converts {@link TimerDescriptor} to {@link TimerDescriptorEntity} object.
*
* @param descriptor - {@link TimerDescriptor} object
* @return converted {@link TimerDescriptorEntity} object
*/
@Mapping(target = "naturalKey", ignore = true)
@Mapping(target = "timerDescriptor", source = "descriptor")
@Mapping(target = "moduleName", expression = "java(fromModuleId(descriptor))")
TimerDescriptorEntity convert(TimerDescriptor descriptor);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.folio.scheduler.service;

import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.folio.common.utils.CollectionUtils.mapItems;

import jakarta.persistence.EntityExistsException;
Expand Down Expand Up @@ -151,6 +152,10 @@ private void validate(TimerDescriptor timerDescriptor) {
&& timerDescriptor.getRoutingEntry().getMethods().size() > 1) {
throw new IllegalArgumentException("Only 1 method is allowed per timer");
}

if (isEmpty(timerDescriptor.getModuleId()) && isEmpty(timerDescriptor.getModuleName())) {
throw new IllegalArgumentException("Module id or module name is required");
}
}

private TimerDescriptor doCreate(TimerDescriptor timerDescriptor) {
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/org/folio/scheduler/it/SchedulerTimerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class SchedulerTimerIT extends BaseIntegrationTest {
private static final String UNKNOWN_ID = "51fd5dff-5d51-4169-a296-d441e1d234c9";
private static final UUID TIMER_ID_TO_UPDATE = UUID.fromString("123e4567-e89b-12d3-a456-426614174001");
private static final String TIMER_ID_TO_DELETE = "123e4567-e89b-12d3-a456-426614174002";
private static final String MODULE_ID = "mod-foo-1.0.0";

@Autowired private Scheduler scheduler;
@Autowired private ObjectMapper objectMapper;
Expand Down Expand Up @@ -89,6 +90,7 @@ void create_positive_simpleTrigger() throws Exception {
var timerDescriptor = new TimerDescriptor()
.id(timerId)
.enabled(true)
.moduleId(MODULE_ID)
.routingEntry(new RoutingEntry()
.methods(List.of("POST"))
.pathPattern("/test")
Expand All @@ -115,6 +117,7 @@ void create_positive_cronTrigger() throws Exception {
var timerDescriptor = new TimerDescriptor()
.id(UUID.fromString(timerId))
.enabled(true)
.moduleId(MODULE_ID)
.routingEntry(new RoutingEntry()
.methods(List.of("POST"))
.pathPattern("/test")
Expand All @@ -134,7 +137,7 @@ void create_positive_cronTrigger() throws Exception {

@Test
void update_positive() throws Exception {
var desc = TestValues.timerDescriptor(TIMER_ID_TO_UPDATE);
var desc = TestValues.timerDescriptor(TIMER_ID_TO_UPDATE).moduleId(MODULE_ID);
doPut("/scheduler/timers/{id}", desc, TIMER_ID_TO_UPDATE)
.andExpect(jsonPath("$.id", notNullValue()))
.andExpect(jsonPath("$.enabled", is(true)));
Expand All @@ -160,6 +163,7 @@ void delete_negative_notFound() throws Exception {
void create_duplicate() throws Exception {
var timerDescriptor = new TimerDescriptor()
.enabled(true)
.moduleId(MODULE_ID)
.routingEntry(new RoutingEntry()
.methods(List.of("POST"))
.pathPattern("/test/sometimer")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
@ExtendWith(MockitoExtension.class)
class SchedulerTimerServiceTest {

private static final String MODULE_ID = "mod-foo-1.0.0";

@InjectMocks SchedulerTimerService schedulerTimerService;
@Mock private SchedulerTimerRepository schedulerTimerRepository;
@Mock private TimerDescriptorMapper timerDescriptorMapper;
Expand Down Expand Up @@ -88,8 +90,8 @@ void getAll_positive() {

@Test
void create_positive() {
var descriptor = timerDescriptor();
var entity = timerDescriptorEntity();
var descriptor = timerDescriptor().moduleId(MODULE_ID);
var entity = timerDescriptorEntity(descriptor);

when(timerDescriptorMapper.convert(descriptor)).thenReturn(entity);
when(schedulerTimerRepository.save(entity)).thenReturn(entity);
Expand All @@ -101,7 +103,7 @@ void create_positive() {

@Test
void create_positive_entityIdIsNull() {
var descriptor = timerDescriptor(null);
var descriptor = timerDescriptor(null).moduleId(MODULE_ID);
when(timerDescriptorMapper.convert(timerDescriptorCaptor.capture()))
.thenAnswer(inv -> timerDescriptorEntity(inv.getArgument(0)));
when(schedulerTimerRepository.save(any(TimerDescriptorEntity.class))).thenAnswer(inv -> inv.getArgument(0));
Expand All @@ -124,9 +126,18 @@ void create_negative_foundEntityById() {
.hasMessage("TimerDescriptor already exist for id " + TIMER_UUID);
}

@Test
void create_negative_moduleIdAndNameIsEmpty() {
var descriptor = timerDescriptor();

assertThatThrownBy(() -> schedulerTimerService.create(descriptor))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Module id or module name is required");
}

@Test
void update_positive() {
var expectedDescriptor = timerDescriptor().modified(true);
var expectedDescriptor = timerDescriptor().moduleId(MODULE_ID).modified(true);
var existingEntity = timerDescriptorEntity();
var entityToUpdate = timerDescriptorEntity(expectedDescriptor);

Expand All @@ -135,7 +146,7 @@ void update_positive() {
when(schedulerTimerRepository.save(entityToUpdate)).thenReturn(entityToUpdate);
doNothing().when(jobSchedulingService).reschedule(existingEntity.getTimerDescriptor(), expectedDescriptor);

var actual = schedulerTimerService.update(TIMER_UUID, timerDescriptor());
var actual = schedulerTimerService.update(TIMER_UUID, timerDescriptor().moduleId(MODULE_ID));

assertThat(actual).isEqualTo(expectedDescriptor);
}
Expand All @@ -158,13 +169,22 @@ void update_negative_differentIdInParameterAndBody() {

@Test
void update_negative_entityNotFound() {
var descriptor = timerDescriptor();
var descriptor = timerDescriptor().moduleId(MODULE_ID);
when(schedulerTimerRepository.findById(TIMER_UUID)).thenReturn(Optional.empty());
assertThatThrownBy(() -> schedulerTimerService.update(TIMER_UUID, descriptor))
.isInstanceOf(EntityNotFoundException.class)
.hasMessage("Unable to find timer descriptor with id " + TIMER_UUID);
}

@Test
void update_negative_moduleIdAndNameIsEmpty() {
var descriptor = timerDescriptor();

assertThatThrownBy(() -> schedulerTimerService.update(TIMER_UUID, descriptor))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Module id or module name is required");
}

@Test
void delete_positive() {
var timerDescriptorEntity = timerDescriptorEntity();
Expand Down Expand Up @@ -199,9 +219,9 @@ void deleteAll_positive() {

@Test
void create_duplicate() {
var descriptor = timerDescriptor();
var descriptor = timerDescriptor().moduleId(MODULE_ID);
descriptor.setId(null);
var entity = timerDescriptorEntity();
var entity = timerDescriptorEntity(descriptor);

when(timerDescriptorMapper.convert(descriptor)).thenReturn(entity);
when(schedulerTimerRepository.save(entity)).thenReturn(entity);
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/folio/scheduler/support/TestValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static TimerDescriptorEntity timerDescriptorEntity(TimerDescriptor descri
var entity = new TimerDescriptorEntity();
entity.setId(TIMER_UUID);
entity.setTimerDescriptor(descriptor);
entity.setModuleId(descriptor.getModuleId());
entity.setModuleName(descriptor.getModuleName());
return entity;
}
}
1 change: 1 addition & 0 deletions src/test/resources/json/user/timer/user-timer-request.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"enabled": true,
"moduleId": "folio-module1-1.0.0",
"routingEntry": {
"methods": [ "GET" ],
"pathPattern": "/folio-module1/v3/scheduled-timer",
Expand Down
11 changes: 7 additions & 4 deletions src/test/resources/sql/timer-descriptor-it.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
insert into test_mod_scheduler.timer(id, timer_descriptor)
insert into test_mod_scheduler.timer(id, module_id, module_name, timer_descriptor)
values
('123e4567-e89b-12d3-a456-426614174000', '{
('123e4567-e89b-12d3-a456-426614174000', 'mod-foo-1.0.0', 'mod-foo', '{
"id":"123e4567-e89b-12d3-a456-426614174000",
"modified": "false",
"enabled": "true",
"moduleId": "mod-foo-1.0.0",
"routingEntry": {
"methods": [
"POST"
Expand All @@ -14,9 +15,10 @@ values
}
}'
),
('123e4567-e89b-12d3-a456-426614174001', '{
('123e4567-e89b-12d3-a456-426614174001', 'mod-foo-1.0.0', 'mod-foo', '{
"id": "123e4567-e89b-12d3-a456-426614174001",
"modified": "false",
"moduleId": "mod-foo-1.0.0",
"routingEntry": {
"methods": [
"POST"
Expand All @@ -28,9 +30,10 @@ values
}
}'
),
('123e4567-e89b-12d3-a456-426614174002', '{
('123e4567-e89b-12d3-a456-426614174002', 'mod-foo-1.0.0', 'mod-foo', '{
"id": "123e4567-e89b-12d3-a456-426614174002",
"modified": "false",
"moduleId": "mod-foo-1.0.0",
"routingEntry": {
"methods": [
"POST"
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/wiremock/stubs/event-timer-endpoint.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
"headers": {
"Content-Type": {
"equalTo": "application/json"
},
"x-okapi-module-hint": {
"matches" : "^folio\\-module1\\-\\d\\.\\d\\.\\d$"
},
"x-okapi-tenant": {
"equalTo": "test"
},
"x-okapi-request-id": {
"matches" : "^\\d{6}$"
},
"x-okapi-url": {
"matches" : "^http://localhost:\\d{1,5}$"
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/wiremock/stubs/timer-endpoint.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
"headers": {
"Content-Type": {
"equalTo": "application/json"
},
"x-okapi-module-hint": {
"equalTo": "mod-foo-1.0.0"
},
"x-okapi-tenant": {
"equalTo": "test"
},
"x-okapi-request-id": {
"matches" : "^\\d{6}$"
},
"x-okapi-url": {
"matches" : "^http://localhost:\\d{1,5}$"
}
}
},
Expand Down

0 comments on commit a2876e8

Please sign in to comment.