FINERACT-2028: Add audit logging for manual scheduler job execution
This commit is contained in:
+8
@@ -2287,6 +2287,14 @@ public class CommandWrapperBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CommandWrapperBuilder executeSchedulerJob(final Long jobId) {
|
||||||
|
this.actionName = "EXECUTEJOB";
|
||||||
|
this.entityName = "SCHEDULER";
|
||||||
|
this.entityId = jobId;
|
||||||
|
this.href = "/jobs/" + jobId + "?command=executeJob";
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public CommandWrapperBuilder createMeeting(final CommandWrapper resourceDetails, final String supportedEntityType,
|
public CommandWrapperBuilder createMeeting(final CommandWrapper resourceDetails, final String supportedEntityType,
|
||||||
final Long supportedEntityId) {
|
final Long supportedEntityId) {
|
||||||
this.actionName = "CREATE";
|
this.actionName = "CREATE";
|
||||||
|
|||||||
+5
-1
@@ -227,7 +227,11 @@ public class SchedulerJobApiResource {
|
|||||||
response = Response.status(400).build();
|
response = Response.status(400).build();
|
||||||
if (is(commandParam, SchedulerJobApiConstants.COMMAND_EXECUTE_JOB)) {
|
if (is(commandParam, SchedulerJobApiConstants.COMMAND_EXECUTE_JOB)) {
|
||||||
Long jobId = schedulerJobRunnerReadService.retrieveId(idType, identifier);
|
Long jobId = schedulerJobRunnerReadService.retrieveId(idType, identifier);
|
||||||
jobRegisterService.executeJobWithParameters(jobId, jsonRequestBody);
|
final CommandWrapper commandRequest = new CommandWrapperBuilder() //
|
||||||
|
.executeSchedulerJob(jobId) //
|
||||||
|
.withJson(jsonRequestBody) //
|
||||||
|
.build();
|
||||||
|
commandsSourceWritePlatformService.logCommandSource(commandRequest);
|
||||||
response = Response.status(202).build();
|
response = Response.status(202).build();
|
||||||
} else {
|
} else {
|
||||||
throw new UnrecognizedQueryParamException(SchedulerJobApiConstants.COMMAND, commandParam);
|
throw new UnrecognizedQueryParamException(SchedulerJobApiConstants.COMMAND, commandParam);
|
||||||
|
|||||||
+46
@@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.fineract.infrastructure.jobs.handler;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.apache.fineract.commands.annotation.CommandType;
|
||||||
|
import org.apache.fineract.commands.handler.NewCommandSourceHandler;
|
||||||
|
import org.apache.fineract.infrastructure.core.api.JsonCommand;
|
||||||
|
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
|
||||||
|
import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder;
|
||||||
|
import org.apache.fineract.infrastructure.jobs.service.JobRegisterService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@CommandType(entity = "SCHEDULER", action = "EXECUTEJOB")
|
||||||
|
public class ExecuteJobCommandHandler implements NewCommandSourceHandler {
|
||||||
|
|
||||||
|
private final JobRegisterService jobRegisterService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandProcessingResult processCommand(final JsonCommand command) {
|
||||||
|
final Long jobId = command.entityId();
|
||||||
|
jobRegisterService.executeJobWithParameters(jobId, command.json());
|
||||||
|
return new CommandProcessingResultBuilder() //
|
||||||
|
.withCommandId(command.commandId()) //
|
||||||
|
.withEntityId(jobId) //
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
/**
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.fineract.infrastructure.jobs.handler;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import org.apache.fineract.infrastructure.core.api.JsonCommand;
|
||||||
|
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
|
||||||
|
import org.apache.fineract.infrastructure.jobs.service.JobRegisterService;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class ExecuteJobCommandHandlerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private JobRegisterService jobRegisterService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private JsonCommand command;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private ExecuteJobCommandHandler underTest;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldExecuteJobAndReturnCommandResult() {
|
||||||
|
// given
|
||||||
|
Long jobId = 123L;
|
||||||
|
Long commandId = 456L;
|
||||||
|
String json = "{\"includeTasks\":true}";
|
||||||
|
when(command.entityId()).thenReturn(jobId);
|
||||||
|
when(command.commandId()).thenReturn(commandId);
|
||||||
|
when(command.json()).thenReturn(json);
|
||||||
|
|
||||||
|
// when
|
||||||
|
CommandProcessingResult result = underTest.processCommand(command);
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify(jobRegisterService).executeJobWithParameters(jobId, json);
|
||||||
|
assertEquals(commandId, result.getCommandId());
|
||||||
|
assertEquals(jobId, result.getResourceId());
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
@@ -36,6 +36,7 @@ import java.util.List;
|
|||||||
import org.apache.fineract.integrationtests.common.AuditHelper;
|
import org.apache.fineract.integrationtests.common.AuditHelper;
|
||||||
import org.apache.fineract.integrationtests.common.ClientHelper;
|
import org.apache.fineract.integrationtests.common.ClientHelper;
|
||||||
import org.apache.fineract.integrationtests.common.OfficeHelper;
|
import org.apache.fineract.integrationtests.common.OfficeHelper;
|
||||||
|
import org.apache.fineract.integrationtests.common.SchedulerJobHelper;
|
||||||
import org.apache.fineract.integrationtests.common.Utils;
|
import org.apache.fineract.integrationtests.common.Utils;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -51,6 +52,7 @@ public class AuditIntegrationTest {
|
|||||||
private RequestSpecification requestSpec;
|
private RequestSpecification requestSpec;
|
||||||
private ClientHelper clientHelper;
|
private ClientHelper clientHelper;
|
||||||
private AuditHelper auditHelper;
|
private AuditHelper auditHelper;
|
||||||
|
private SchedulerJobHelper schedulerJobHelper;
|
||||||
private static final SecureRandom rand = new SecureRandom();
|
private static final SecureRandom rand = new SecureRandom();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,6 +67,7 @@ public class AuditIntegrationTest {
|
|||||||
this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build();
|
this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build();
|
||||||
this.auditHelper = new AuditHelper(this.requestSpec, this.responseSpec);
|
this.auditHelper = new AuditHelper(this.requestSpec, this.responseSpec);
|
||||||
this.clientHelper = new ClientHelper(this.requestSpec, this.responseSpec);
|
this.clientHelper = new ClientHelper(this.requestSpec, this.responseSpec);
|
||||||
|
this.schedulerJobHelper = new SchedulerJobHelper(this.requestSpec);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -157,4 +160,19 @@ public class AuditIntegrationTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Test
|
||||||
|
public void executeSchedulerJobShouldCreateAuditEntry() {
|
||||||
|
// given
|
||||||
|
int jobId = schedulerJobHelper.getSchedulerJobIdByShortName("SA_AANF");
|
||||||
|
List<HashMap<String, Object>> auditsRecievedInitial = auditHelper.getAuditDetails(jobId, "EXECUTEJOB", "SCHEDULER");
|
||||||
|
|
||||||
|
// when
|
||||||
|
schedulerJobHelper.runSchedulerJob(jobId);
|
||||||
|
|
||||||
|
// then
|
||||||
|
List<HashMap<String, Object>> auditsRecieved = auditHelper.getAuditDetails(jobId, "EXECUTEJOB", "SCHEDULER");
|
||||||
|
auditHelper.verifyMultipleAuditsOnserver(auditsRecievedInitial, auditsRecieved, jobId, "EXECUTEJOB", "SCHEDULER");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user