Commit de61e702 authored by Christos Tranoris's avatar Christos Tranoris
Browse files

findResourcesByFilter: add category filter and health in result

parent d8bceb1c
Loading
Loading
Loading
Loading
+6 −1
Changes for src/main/java/org/etsi/osl/tmf/ri639/reposervices/ResourceFilter.java: 6 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * Filter DTO for querying resources via the CATALOG_GET_RESOURCES_BY_FILTER JMS route.
 * Example: {"specId":null,"namePattern":null,"vendor":null,"location":null,"status":"AVAILABLE","page":0,"pageSize":20}
 * Example: {"specId":null,"namePattern":null,"vendor":null,"location":null,"category":null,"status":"AVAILABLE","page":0,"pageSize":20}
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResourceFilter {
@@ -17,9 +17,14 @@ public class ResourceFilter {
    private String location;
    /** Matched against resourceStatus enum value (e.g. "AVAILABLE") */
    private String status;
    /** Matched exactly against the resource category name */
    private String category;
    private int page = 0;
    private int pageSize = 20;

    public String getCategory() { return category; }
    public void setCategory(String category) { this.category = category; }

    public String getSpecId() { return specId; }
    public void setSpecId(String specId) { this.specId = specId; }

+7 −1
Changes for src/main/java/org/etsi/osl/tmf/ri639/reposervices/ResourceRepoService.java: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -567,7 +567,8 @@ public class ResourceRepoService {
  /**
   * Find resources matching the given filter.
   * Returns JSON: {"resources":[...],"totalCount":N,"continuationToken":"<nextPage>" or null}
   * Filter fields: specId, namePattern, vendor (characteristic), location (place.name), status, page, pageSize.
   * Filter fields: specId, namePattern, vendor (characteristic), location (place.name), category (exact name), status, page, pageSize.
   * Each resource entry includes "health" (ResourceHealth derived from the X.731 states).
   */
  @Transactional
  public String findResourcesByFilter(String filterJson) throws JsonProcessingException {
@@ -603,6 +604,10 @@ public class ResourceRepoService {
        conditions.add("srv.place.name LIKE :location");
        params.put("location", "%" + filter.getLocation() + "%");
      }
      if (filter.getCategory() != null) {
        conditions.add("srv.category = :category");
        params.put("category", filter.getCategory());
      }
      if (filter.getStatus() != null) {
        ResourceStatusType statusEnum = ResourceStatusType.fromValue(filter.getStatus().toLowerCase());
        if (statusEnum != null) {
@@ -645,6 +650,7 @@ public class ResourceRepoService {
        entry.put("category", r.getCategory());
        entry.put("description", r.getDescription());
        entry.put("resourceStatus", r.getResourceStatus() != null ? r.getResourceStatus().toString() : null);
        entry.put("health", r.deriveHealth().name());
        if (r.getResourceSpecification() != null) {
          entry.put("resourceSpecificationId", r.getResourceSpecification().getUuid());
          entry.put("resourceSpecificationName", r.getResourceSpecification().getName());
+92
Changes for src/test/java/org/etsi/osl/services/api/ri639/ResourceFindByFilterTest.java: 92 added lines, 0 removed lines.
Original line number Diff line number Diff line
package org.etsi.osl.services.api.ri639;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import jakarta.persistence.EntityManager;
import org.etsi.osl.services.api.BaseIT;
import org.etsi.osl.tmf.ri639.model.LogicalResource;
import org.etsi.osl.tmf.ri639.model.ResourceHealth;
import org.etsi.osl.tmf.ri639.repo.ResourceRepository;
import org.etsi.osl.tmf.ri639.reposervices.ResourceRepoService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class ResourceFindByFilterTest extends BaseIT {

  private static final String CAT_A = "FindByFilterCatA";
  private static final String CAT_B = "FindByFilterCatB";

  @Autowired
  private ResourceRepoService resourceRepoService;
  @Autowired
  private ResourceRepository resourceRepository;
  @Autowired
  private EntityManager entityManager;

  private final ObjectMapper mapper = new ObjectMapper();
  private final List<LogicalResource> created = new ArrayList<>();

  private void save(String name, String category, ResourceHealth health) {
    LogicalResource r = new LogicalResource();
    r.setName(name);
    r.setCategory(category);
    r.applyHealth(health);
    created.add(resourceRepository.saveAndFlush(r));
  }

  @BeforeEach
  public void setupBefore() {
    save("fbf-up", CAT_A, ResourceHealth.UP);
    save("fbf-held", CAT_A, ResourceHealth.HELD);
    save("fbf-gone", CAT_B, ResourceHealth.GONE);
    entityManager.clear();
  }

  @AfterEach
  public void cleanup() {
    created.forEach(resourceRepository::delete);
    resourceRepository.flush();
    created.clear();
    entityManager.clear();
  }

  @Test
  public void testFilterByCategoryReturnsOnlyThatCategoryWithHealth() throws Exception {
    JsonNode result = mapper.readTree(
        resourceRepoService.findResourcesByFilter("{\"category\":\"" + CAT_A + "\"}"));

    assertThat(result.get("totalCount").asLong()).isEqualTo(2);
    JsonNode resources = result.get("resources");
    assertThat(resources).hasSize(2);
    for (JsonNode r : resources) {
      assertThat(r.get("category").asText()).isEqualTo(CAT_A);
    }
    // ordered by name: fbf-held, fbf-up
    assertThat(resources.get(0).get("health").asText()).isEqualTo("HELD");
    assertThat(resources.get(1).get("health").asText()).isEqualTo("UP");
  }

  @Test
  public void testCategoryCombinedWithNamePattern() throws Exception {
    JsonNode result = mapper.readTree(resourceRepoService.findResourcesByFilter(
        "{\"category\":\"" + CAT_A + "\",\"namePattern\":\"fbf-up\"}"));

    assertThat(result.get("totalCount").asLong()).isEqualTo(1);
    assertThat(result.get("resources").get(0).get("name").asText()).isEqualTo("fbf-up");
  }

  @Test
  public void testHealthReportedWithoutCategoryFilter() throws Exception {
    JsonNode result = mapper.readTree(
        resourceRepoService.findResourcesByFilter("{\"namePattern\":\"fbf-gone\"}"));

    assertThat(result.get("totalCount").asLong()).isEqualTo(1);
    assertThat(result.get("resources").get(0).get("health").asText()).isEqualTo("GONE");
  }
}