diff --git a/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/json/model/distributionset/MgmtDistributionSetStatistics.java b/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/json/model/distributionset/MgmtDistributionSetStatistics.java index 75ba4b208..652761674 100644 --- a/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/json/model/distributionset/MgmtDistributionSetStatistics.java +++ b/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/json/model/distributionset/MgmtDistributionSetStatistics.java @@ -16,20 +16,19 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.util.HashMap; import java.util.Map; -@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonIgnoreProperties(ignoreUnknown = true) public class MgmtDistributionSetStatistics { private static final String TOTAL = "total"; - @JsonProperty + @JsonProperty("actions") private Map totalActionsPerStatus; - @JsonProperty + @JsonProperty("rollouts") private Map totalRolloutsPerStatus; @JsonProperty private Long totalAutoAssignments; - private MgmtDistributionSetStatistics() { // Private constructor to enforce the use of the builder pattern } @@ -47,14 +46,15 @@ public class MgmtDistributionSetStatistics { } public static class Builder { - private Map totalActionsPerStatus; - private Map totalRolloutsPerStatus; - + private final Map totalActionsPerStatus; + private final Map totalRolloutsPerStatus; private Long totalAutoAssignments; + private final boolean fullRepresentation; - public Builder() { + public Builder(boolean fullRepresentation) { totalActionsPerStatus = new HashMap<>(); totalRolloutsPerStatus = new HashMap<>(); + this.fullRepresentation = fullRepresentation; } public Builder addTotalActionPerStatus(String status, Long count) { @@ -76,13 +76,13 @@ public class MgmtDistributionSetStatistics { MgmtDistributionSetStatistics statistics = new MgmtDistributionSetStatistics(); statistics.totalActionsPerStatus = calculateTotalWithStatus(totalActionsPerStatus); statistics.totalRolloutsPerStatus = calculateTotalWithStatus(totalRolloutsPerStatus); - statistics.totalAutoAssignments = totalAutoAssignments; + statistics.totalAutoAssignments = fullRepresentation ? (totalAutoAssignments == null ? Long.valueOf(0) : totalAutoAssignments) : totalAutoAssignments; return statistics; } private Map calculateTotalWithStatus(Map statusMap) { - if (statusMap.isEmpty()) { - return null; + if (!fullRepresentation && statusMap.isEmpty()) { + return statusMap; } long total = statusMap.values().stream().mapToLong(Long::longValue).sum(); diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResource.java b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResource.java index e3e58452d..9746a06ba 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResource.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResource.java @@ -417,7 +417,7 @@ public class MgmtDistributionSetResource implements MgmtDistributionSetRestApi { @Override public ResponseEntity getRolloutsCountByStatusForDistributionSet(Long distributionSetId) { - MgmtDistributionSetStatistics.Builder statistics = new MgmtDistributionSetStatistics.Builder(); + MgmtDistributionSetStatistics.Builder statistics = new MgmtDistributionSetStatistics.Builder(false); distributionSetManagement.countRolloutsByStatusForDistributionSet(distributionSetId).forEach(statistic -> statistics.addTotalRolloutPerStatus(String.valueOf(statistic.getName()), Long.parseLong(statistic.getData().toString()))); return ResponseEntity.ok(statistics.build()); @@ -425,7 +425,7 @@ public class MgmtDistributionSetResource implements MgmtDistributionSetRestApi { @Override public ResponseEntity getActionsCountByStatusForDistributionSet(Long distributionSetId) { - MgmtDistributionSetStatistics.Builder statistics = new MgmtDistributionSetStatistics.Builder(); + MgmtDistributionSetStatistics.Builder statistics = new MgmtDistributionSetStatistics.Builder(false); distributionSetManagement.countActionsByStatusForDistributionSet(distributionSetId).forEach(statistic -> statistics.addTotalActionPerStatus(String.valueOf(statistic.getName()), Long.parseLong(statistic.getData().toString()))); return ResponseEntity.ok(statistics.build()); @@ -433,14 +433,14 @@ public class MgmtDistributionSetResource implements MgmtDistributionSetRestApi { @Override public ResponseEntity getAutoAssignmentsCountForDistributionSet(Long distributionSetId) { - MgmtDistributionSetStatistics.Builder statistics = new MgmtDistributionSetStatistics.Builder(); + MgmtDistributionSetStatistics.Builder statistics = new MgmtDistributionSetStatistics.Builder(false); statistics.addTotalAutoAssignments(distributionSetManagement.countAutoAssignmentsForDistributionSet(distributionSetId)); return ResponseEntity.ok(statistics.build()); } @Override public ResponseEntity getStatisticsForDistributionSet(Long distributionSetId) { - MgmtDistributionSetStatistics.Builder statistics = new MgmtDistributionSetStatistics.Builder(); + MgmtDistributionSetStatistics.Builder statistics = new MgmtDistributionSetStatistics.Builder(true); distributionSetManagement.countRolloutsByStatusForDistributionSet(distributionSetId).forEach(statistic -> statistics.addTotalRolloutPerStatus(String.valueOf(statistic.getName()), Long.parseLong(statistic.getData().toString()))); distributionSetManagement.countActionsByStatusForDistributionSet(distributionSetId).forEach(statistic -> diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResourceTest.java b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResourceTest.java index 8bdeeead3..eb1fbcca2 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResourceTest.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResourceTest.java @@ -1458,17 +1458,17 @@ public class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegr mvc.perform(get(MgmtRestConstants.DISTRIBUTIONSET_V1_REQUEST_MAPPING + "/{ds}/statistics/rollouts", ds1.getId()).contentType(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()) .andExpect(status().isOk()) - .andExpect(jsonPath("totalRolloutsPerStatus.READY", equalTo(1))) - .andExpect(jsonPath("totalRolloutsPerStatus.RUNNING", equalTo(1))) - .andExpect(jsonPath("totalRolloutsPerStatus.total", equalTo(2))) - .andExpect(jsonPath("totalActionsPerStatus").doesNotExist()) + .andExpect(jsonPath("rollouts.READY", equalTo(1))) + .andExpect(jsonPath("rollouts.RUNNING", equalTo(1))) + .andExpect(jsonPath("rollouts.total", equalTo(2))) + .andExpect(jsonPath("actions").doesNotExist()) .andExpect(jsonPath("totalAutoAssignments").doesNotExist()); mvc.perform(get(MgmtRestConstants.DISTRIBUTIONSET_V1_REQUEST_MAPPING + "/{ds}/statistics/rollouts", ds2.getId()).contentType(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()) .andExpect(status().isOk()) - .andExpect(jsonPath("totalRolloutsPerStatus").doesNotExist()) - .andExpect(jsonPath("totalActionsPerStatus").doesNotExist()) + .andExpect(jsonPath("rollouts").doesNotExist()) + .andExpect(jsonPath("actions").doesNotExist()) .andExpect(jsonPath("totalAutoAssignments").doesNotExist()); } @@ -1488,16 +1488,16 @@ public class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegr mvc.perform(get(MgmtRestConstants.DISTRIBUTIONSET_V1_REQUEST_MAPPING + "/{ds}/statistics/actions", ds1.getId()).contentType(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()) .andExpect(status().isOk()) - .andExpect(jsonPath("totalActionsPerStatus.RUNNING", equalTo(4))) - .andExpect(jsonPath("totalActionsPerStatus.total", equalTo(4))) - .andExpect(jsonPath("totalRolloutsPerStatus").doesNotExist()) + .andExpect(jsonPath("actions.RUNNING", equalTo(4))) + .andExpect(jsonPath("actions.total", equalTo(4))) + .andExpect(jsonPath("rollouts").doesNotExist()) .andExpect(jsonPath("totalAutoAssignments").doesNotExist()); mvc.perform(get(MgmtRestConstants.DISTRIBUTIONSET_V1_REQUEST_MAPPING + "/{ds}/statistics/actions", ds2.getId()).contentType(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()) .andExpect(status().isOk()) - .andExpect(jsonPath("totalRolloutsPerStatus").doesNotExist()) - .andExpect(jsonPath("totalActionsPerStatus").doesNotExist()) + .andExpect(jsonPath("rollouts").doesNotExist()) + .andExpect(jsonPath("actions").doesNotExist()) .andExpect(jsonPath("totalAutoAssignments").doesNotExist()); } @@ -1518,14 +1518,14 @@ public class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegr .andDo(MockMvcResultPrinter.print()) .andExpect(status().isOk()) .andExpect(jsonPath("totalAutoAssignments", equalTo(2))) - .andExpect(jsonPath("totalRolloutsPerStatus").doesNotExist()) - .andExpect(jsonPath("totalActionsPerStatus").doesNotExist()); + .andExpect(jsonPath("rollouts").doesNotExist()) + .andExpect(jsonPath("actions").doesNotExist()); mvc.perform(get(MgmtRestConstants.DISTRIBUTIONSET_V1_REQUEST_MAPPING + "/{ds}/statistics/autoassignments", ds2.getId()).contentType(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()) .andExpect(status().isOk()) - .andExpect(jsonPath("totalRolloutsPerStatus").doesNotExist()) - .andExpect(jsonPath("totalActionsPerStatus").doesNotExist()) + .andExpect(jsonPath("rollouts").doesNotExist()) + .andExpect(jsonPath("actions").doesNotExist()) .andExpect(jsonPath("totalAutoAssignments").doesNotExist()); } @@ -1550,17 +1550,17 @@ public class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegr .andDo(MockMvcResultPrinter.print()) .andExpect(status().isOk()) .andExpect(jsonPath("totalAutoAssignments", equalTo(1))) - .andExpect(jsonPath("totalActionsPerStatus.RUNNING", equalTo(4))) - .andExpect(jsonPath("totalActionsPerStatus.total", equalTo(4))) - .andExpect(jsonPath("totalRolloutsPerStatus.RUNNING", equalTo(1))) - .andExpect(jsonPath("totalRolloutsPerStatus.total", equalTo(1))); + .andExpect(jsonPath("actions.RUNNING", equalTo(4))) + .andExpect(jsonPath("actions.total", equalTo(4))) + .andExpect(jsonPath("rollouts.RUNNING", equalTo(1))) + .andExpect(jsonPath("rollouts.total", equalTo(1))); mvc.perform(get(MgmtRestConstants.DISTRIBUTIONSET_V1_REQUEST_MAPPING + "/{ds}/statistics/autoassignments", ds2.getId()).contentType(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultPrinter.print()) .andExpect(status().isOk()) - .andExpect(jsonPath("totalRolloutsPerStatus").doesNotExist()) - .andExpect(jsonPath("totalActionsPerStatus").doesNotExist()) + .andExpect(jsonPath("rollouts").doesNotExist()) + .andExpect(jsonPath("actions").doesNotExist()) .andExpect(jsonPath("totalAutoAssignments").doesNotExist()); }