-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from peter-szrnka/352-gms-173-manual-job-exec…
…ution-feature GMS-173 manual job execution feature
- Loading branch information
Showing
31 changed files
with
536 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
code/gms-backend/src/main/java/io/github/gms/job/ManualJobExecutionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.github.gms.job; | ||
|
||
import io.github.gms.common.abstraction.AbstractJob; | ||
import io.github.gms.common.abstraction.GmsController; | ||
import io.github.gms.common.enums.EventTarget; | ||
import io.github.gms.common.enums.MdcParameter; | ||
import io.github.gms.common.types.AuditTarget; | ||
import io.github.gms.common.util.MdcUtils; | ||
import io.github.gms.job.model.UrlConfiguration; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.NoSuchBeanDefinitionException; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static io.github.gms.common.util.Constants.ROLE_ADMIN; | ||
import static io.github.gms.common.util.Constants.TRUE; | ||
|
||
/** | ||
* @author Peter Szrnka | ||
* @since 1.0 | ||
*/ | ||
@RestController | ||
@RequiredArgsConstructor | ||
@PreAuthorize(ROLE_ADMIN) | ||
@AuditTarget(EventTarget.ANNOUNCEMENT) | ||
@RequestMapping("/secure/job_execution") | ||
public class ManualJobExecutionController implements GmsController { | ||
|
||
private final ApplicationContext applicationContext; | ||
|
||
@GetMapping("/{jobName}") | ||
public ResponseEntity<Void> runJobByName(@PathVariable("jobName") String jobName) { | ||
UrlConfiguration urlConfiguration = UrlConfiguration.fromUrl(jobName); | ||
|
||
if (urlConfiguration == null) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
|
||
return runJob(urlConfiguration.getClazz()); | ||
} | ||
|
||
private <T extends AbstractJob> ResponseEntity<Void> runJob(@NonNull Class<T> clazz) { | ||
MdcUtils.put(MdcParameter.MANUAL_JOB_EXECUTION, TRUE); | ||
|
||
try { | ||
T job = applicationContext.getBean(clazz); | ||
job.run(); | ||
|
||
MdcUtils.remove(MdcParameter.MANUAL_JOB_EXECUTION); | ||
return ResponseEntity.ok().build(); | ||
} catch (NoSuchBeanDefinitionException e) { | ||
MdcUtils.remove(MdcParameter.MANUAL_JOB_EXECUTION); | ||
return ResponseEntity.notFound().build(); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
code/gms-backend/src/main/java/io/github/gms/job/model/UrlConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.github.gms.job.model; | ||
|
||
import io.github.gms.common.abstraction.AbstractJob; | ||
import io.github.gms.job.*; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* @author Peter Szrnka | ||
* @since 1.0 | ||
*/ | ||
@Getter | ||
@RequiredArgsConstructor | ||
public enum UrlConfiguration { | ||
EVENT_MAINTENANCE(UrlConstants.EVENT_MAINTENANCE, EventMaintenanceJob.class), | ||
GENERATED_KEYSTORE_CLEANUP(UrlConstants.GENERATED_KEYSTORE_CLEANUP, GeneratedKeystoreCleanupJob.class), | ||
JOB_MAINTENANCE(UrlConstants.JOB_MAINTENANCE, JobMaintenanceJob.class), | ||
MESSAGE_CLEANUP(UrlConstants.MESSAGE_CLEANUP, MessageCleanupJob.class), | ||
SECRET_ROTATION(UrlConstants.SECRET_ROTATION, SecretRotationJob.class), | ||
USER_ANONYMIZATION(UrlConstants.USER_ANONYMIZATION, UserAnonymizationJob.class), | ||
USER_DELETION(UrlConstants.USER_DELETION, UserDeletionJob.class), | ||
LDAP_USER_SYNC(UrlConstants.LDAP_USER_SYNC, LdapUserSyncJob.class); | ||
|
||
private final String url; | ||
private final Class<? extends AbstractJob> clazz; | ||
|
||
public static UrlConfiguration fromUrl(String url) { | ||
return Arrays.stream(UrlConfiguration.values()) | ||
.filter(urlConfiguration -> urlConfiguration.url.equals(url)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
code/gms-backend/src/main/java/io/github/gms/job/model/UrlConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.github.gms.job.model; | ||
|
||
/** | ||
* @author Peter Szrnka | ||
* @since 1.0 | ||
*/ | ||
public interface UrlConstants { | ||
String EVENT_MAINTENANCE = "event_maintenance"; | ||
String GENERATED_KEYSTORE_CLEANUP = "generated_keystore_cleanup"; | ||
String JOB_MAINTENANCE = "job_maintenance"; | ||
String MESSAGE_CLEANUP = "message_cleanup"; | ||
String SECRET_ROTATION = "secret_rotation"; | ||
String USER_ANONYMIZATION = "user_anonymization"; | ||
String USER_DELETION = "user_deletion"; | ||
String LDAP_USER_SYNC = "ldap_user_sync"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...-backend/src/test/java/io/github/gms/job/ManualJobExecutionControllerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.github.gms.job; | ||
|
||
import io.github.gms.abstraction.AbstractIntegrationTest; | ||
import io.github.gms.abstraction.GmsControllerIntegrationTest; | ||
import io.github.gms.common.TestedClass; | ||
import io.github.gms.common.TestedMethod; | ||
import io.github.gms.job.model.UrlConstants; | ||
import io.github.gms.util.TestUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import static io.github.gms.util.TestConstants.TAG_INTEGRATION_TEST; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
/** | ||
* @author Peter Szrnka | ||
* @since 1.0 | ||
*/ | ||
@Tag(TAG_INTEGRATION_TEST) | ||
@TestedClass(ManualJobExecutionController.class) | ||
class ManualJobExecutionControllerIntegrationTest extends AbstractIntegrationTest implements GmsControllerIntegrationTest { | ||
|
||
@Override | ||
@BeforeEach | ||
public void setup() { | ||
gmsUser = TestUtils.createGmsAdminUser(); | ||
jwt = jwtService.generateJwt(TestUtils.createJwtUserRequest(gmsUser)); | ||
} | ||
|
||
@ParameterizedTest | ||
@TestedMethod("runJobByName") | ||
@MethodSource("jobNameTestData") | ||
void runJobByName_whenInputIsProvided_thenReturnExpectedStatus(String urlPath, HttpStatus expectedStatus) { | ||
assertByUrl("/" + urlPath, expectedStatus); | ||
} | ||
|
||
private static Object[][] jobNameTestData() { | ||
return new Object[][] { | ||
{UrlConstants.JOB_MAINTENANCE, HttpStatus.OK}, | ||
{UrlConstants.EVENT_MAINTENANCE, HttpStatus.OK}, | ||
{UrlConstants.GENERATED_KEYSTORE_CLEANUP, HttpStatus.OK}, | ||
{UrlConstants.MESSAGE_CLEANUP, HttpStatus.OK}, | ||
{UrlConstants.SECRET_ROTATION, HttpStatus.OK}, | ||
{UrlConstants.USER_ANONYMIZATION, HttpStatus.OK}, | ||
{UrlConstants.USER_DELETION, HttpStatus.OK}, | ||
{UrlConstants.LDAP_USER_SYNC, HttpStatus.NOT_FOUND}, | ||
{"/invalid_job", HttpStatus.NOT_FOUND} | ||
}; | ||
} | ||
|
||
private void assertByUrl(String url, HttpStatus expectedStatus) { | ||
// arrange | ||
HttpEntity<Void> requestEntity = new HttpEntity<>(TestUtils.getHttpHeaders(jwt)); | ||
|
||
// act | ||
String path = "/secure/job_execution"; | ||
ResponseEntity<Void> response = executeHttpGet(path + url, requestEntity, Void.class); | ||
|
||
// assert | ||
assertNotNull(response); | ||
assertEquals(expectedStatus, response.getStatusCode()); | ||
} | ||
} |
Oops, something went wrong.