Skip to content

Commit

Permalink
Fixes #301 - Add ability to list jobs using REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem committed Jul 14, 2024
1 parent b0aac14 commit 8d9bf45
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
27 changes: 26 additions & 1 deletion rest/src/main/java/com/manorrock/sphynx/rest/JobResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.enterprise.context.RequestScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
import static jakarta.ws.rs.core.Response.Status.CONFLICT;
Expand All @@ -42,6 +44,8 @@
import static java.lang.System.Logger.Level.INFO;
import static java.lang.System.Logger.Level.TRACE;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
Expand Down Expand Up @@ -72,7 +76,7 @@ public class JobResource {
@PUT
@Consumes(APPLICATION_JSON)
public Job create(Job job) {

/*
* Step 1 - determine name.
*/
Expand Down Expand Up @@ -161,4 +165,25 @@ public Job create(Job job) {

return job;
}

/**
* List jobs.
*
* @return the list of jobs.
*/
@GET
@Produces(APPLICATION_JSON)
public List<String> list() {
File jobsDirectory = new File(baseDirectory, "jobs");
if (jobsDirectory.exists()) {
ArrayList<String> result = new ArrayList<>();
String[] names = jobsDirectory.list();
for (String name : names) {
result.add(name);
}
return result;
} else {
throw new WebApplicationException(INTERNAL_SERVER_ERROR);
}
}
}
11 changes: 11 additions & 0 deletions rest/src/test/java/it/JobIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,15 @@ void testCreate() throws Exception {
System.out.println(response.body());
assertTrue(response.body().trim().length() > 0);
}

@Test
void testList() throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest
.newBuilder(new URI("http://localhost:8080/rest/job"))
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println(response.body());
assertTrue(response.body().trim().length() > 0);
}
}

0 comments on commit 8d9bf45

Please sign in to comment.