Skip to content

Commit

Permalink
Correctly serialize and deserialize the statusChanges field
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed Sep 20, 2023
1 parent 6b965aa commit 3fb0b63
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -79,8 +80,8 @@ public synchronized void unregister() {
}

@Rpc(name = "jobStatus")
public Map<String, String> getJobStatus(@RpcParam(name = "job_id") String jobId) {
Map<String, String> resultMap = new HashMap<>();
public Map<String, Object> getJobStatus(@RpcParam(name = "job_id") String jobId) {
Map<String, Object> resultMap = new HashMap<>();
Job jobWithId = service.getJobWithId(jobId);
if (jobWithId == null) {
return resultMap;
Expand All @@ -93,6 +94,15 @@ public Map<String, String> getJobStatus(@RpcParam(name = "job_id") String jobId)
if (jobWithId.getStatus() == Job.JobStatus.ERROR) {
resultMap.put("error", jobWithId.getError().getLocalizedMessage());
}
List<List<String>> statusChanges = new ArrayList<>();
for (Job.StatusChange statusChange : jobWithId.getStatusChanges()) {
statusChanges.add(
Lists.newArrayList(
statusChange.getStatus().name(),
Long.valueOf(statusChange.getChangeTime()).toString(),
statusChange.getMessage()));
}
resultMap.put("status_changes", statusChanges);

return resultMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package com.datastax.mgmtapi.resources.models;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.io.Serializable;
import java.util.List;

Expand Down Expand Up @@ -35,7 +37,9 @@ public enum JobStatus {
@JsonProperty(value = "error")
private String error;

public class StatusChange {
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
@JsonPropertyOrder({"status", "change_time", "message"})
public static class StatusChange {
@JsonProperty(value = "status")
String status;

Expand All @@ -45,8 +49,12 @@ public class StatusChange {
@JsonProperty(value = "message")
String message;

public StatusChange(String type, String message) {
changeTime = System.currentTimeMillis();
@JsonCreator
public StatusChange(
@JsonProperty(value = "status") String type,
@JsonProperty(value = "change_time") String time,
@JsonProperty(value = "message") String message) {
changeTime = Long.parseLong(time);
status = type;
this.message = message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,17 @@ public void testJobStatus() throws Exception {

when(mockResultSet.one()).thenReturn(mockRow);

Map<String, String> jobDetailsRow = new HashMap<>();
Map<String, Object> jobDetailsRow = new HashMap<>();
jobDetailsRow.put("id", "0fe65b47-98c2-47d8-9c3c-5810c9988e10");
jobDetailsRow.put("type", "CLEANUP");
jobDetailsRow.put("status", "COMPLETED");
jobDetailsRow.put("submit_time", String.valueOf(System.currentTimeMillis()));
jobDetailsRow.put("end_time", String.valueOf(System.currentTimeMillis()));

List<List<String>> statusChanges = new ArrayList<>();
statusChanges.add(Lists.newArrayList("SUCCESS", "1695183696663", "No message"));
jobDetailsRow.put("status_changes", statusChanges);

when(mockRow.getObject(0)).thenReturn(jobDetailsRow);

MockHttpResponse response =
Expand All @@ -609,6 +613,8 @@ public void testJobStatus() throws Exception {
assertEquals("0fe65b47-98c2-47d8-9c3c-5810c9988e10", jobDetails.getJobId());
assertEquals("COMPLETED", jobDetails.getStatus().toString());
assertEquals("CLEANUP", jobDetails.getJobType());
assertEquals(1, jobDetails.getStatusChanges().size());
assertEquals("SUCCESS", jobDetails.getStatusChanges().get(0).getStatus());
}

@Test
Expand Down

0 comments on commit 3fb0b63

Please sign in to comment.