Skip to content

Commit

Permalink
#29498 Rename FailJob to FailSuccessJob and improve job handling
Browse files Browse the repository at this point in the history
Renamed the FailJob class to FailSuccessJob and updated its process method to conditionally throw an exception based on job parameters. Refactored JobParams to simplify parameter parsing. Updated Postman tests to reflect these changes and added new tests for active jobs and job states.
  • Loading branch information
jgambarios committed Oct 26, 2024
1 parent 834d256 commit 9e43009
Show file tree
Hide file tree
Showing 4 changed files with 760 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import com.dotmarketing.exception.DotRuntimeException;
import java.util.Map;

@Queue("fail")
public class FailJob implements JobProcessor {
@Queue("failSuccess")
public class FailSuccessJob implements JobProcessor {

@Override
public void process(Job job) {

throw new DotRuntimeException( "Failed job !");
if (job.parameters().containsKey("fail")) {
throw new DotRuntimeException("Failed job !");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ public void setJsonParams(String jsonParams) {
}

public Map<String, Object> getParams() throws JsonProcessingException {
if (null == params) {
if (null != jsonParams && !jsonParams.isEmpty()) {
params = new ObjectMapper().readValue(jsonParams, Map.class);
}
if (null == params && (null != jsonParams && !jsonParams.isEmpty())) {
params = new ObjectMapper().readValue(jsonParams, Map.class);
}
return params;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,57 @@ public class JobQueueHelperIntegrationTest extends TestBaseJunit5WeldInitiator {
@Inject
JobQueueHelper jobQueueHelper;

/**
* Test with no parameters in the JobParams creating a job
* Given scenario: create a job with no parameters and valid queue name
* Expected result: the job is created
*
* @throws DotDataException if there's an error creating the job
*/
@Test
void testEmptyParams(){
void testEmptyParams() throws DotDataException, JsonProcessingException {

jobQueueHelper.registerProcessor("demoQueue", DemoJobProcessor.class);

final var jobParams = new JobParams();
final var user = mock(User.class);
final var request = mock(HttpServletRequest.class);

assertThrows(IllegalArgumentException.class, () -> {
jobQueueHelper.createJob(
"any", jobParams, user, request
);
});
when(user.getUserId()).thenReturn("dotcms.org.1");

final String jobId = jobQueueHelper.createJob(
"demoQueue", jobParams, user, request
);

Assertions.assertNotNull(jobId);
final Job job = jobQueueHelper.getJob(jobId);
Assertions.assertNotNull(job);
Assertions.assertEquals(jobId, job.id());
}

/**
* Test with null parameters creating a job
* Given scenario: create a job with null parameters and valid queue name
* Expected result: the job is created
*
* @throws DotDataException if there's an error creating the job
*/
@Test
void testCreateJobWithNoParameters() throws DotDataException {

jobQueueHelper.registerProcessor("demoQueue", DemoJobProcessor.class);

final var user = mock(User.class);
when(user.getUserId()).thenReturn("dotcms.org.1");

final String jobId = jobQueueHelper.createJob(
"demoQueue", (Map<String, Object>) null, user, mock(HttpServletRequest.class)
);

Assertions.assertNotNull(jobId);
final Job job = jobQueueHelper.getJob(jobId);
Assertions.assertNotNull(job);
Assertions.assertEquals(jobId, job.id());
}

@Test
Expand Down Expand Up @@ -197,31 +236,6 @@ FormDataContentDisposition mockFormDataContentDisposition(){
return formDataContentDisposition;
}

/**
* Test with null parameters creating a job
* Given scenario: create a job with null parameters and valida queue name
* Expected result: the job is created
*
* @throws DotDataException if there's an error creating the job
*/
@Test
void testCreateJobWithNoParameters() throws DotDataException {

jobQueueHelper.registerProcessor("demoQueue", DemoJobProcessor.class);

final var user = mock(User.class);
when(user.getUserId()).thenReturn("dotcms.org.1");

final String jobId = jobQueueHelper.createJob(
"demoQueue", (Map<String, Object>) null, user, mock(HttpServletRequest.class)
);

Assertions.assertNotNull(jobId);
final Job job = jobQueueHelper.getJob(jobId);
Assertions.assertNotNull(job);
Assertions.assertEquals(jobId, job.id());
}

/**
* Test file upload
* Given scenario: upload a file
Expand Down
Loading

0 comments on commit 9e43009

Please sign in to comment.