diff --git a/src/main/java/org/folio/EcsTlrApplication.java b/src/main/java/org/folio/EcsTlrApplication.java index 21feef82..666e4ad5 100644 --- a/src/main/java/org/folio/EcsTlrApplication.java +++ b/src/main/java/org/folio/EcsTlrApplication.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication +@EnableFeignClients public class EcsTlrApplication { public static void main(String[] args) { diff --git a/src/main/java/org/folio/client/feign/SearchClient.java b/src/main/java/org/folio/client/feign/SearchClient.java new file mode 100644 index 00000000..5944b4cc --- /dev/null +++ b/src/main/java/org/folio/client/feign/SearchClient.java @@ -0,0 +1,26 @@ +package org.folio.client.feign; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Data; +import org.folio.model.ResultList; +import org.folio.spring.config.FeignClientConfiguration; +import org.folio.support.CqlQuery; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient(name = "search", configuration = FeignClientConfiguration.class) +public interface SearchClient { + + @GetMapping("/instances") + ResultList searchInstances(@RequestParam("query") CqlQuery cql, @RequestParam("expandAll") Boolean expandAll); + + @AllArgsConstructor + @Data + @JsonIgnoreProperties(ignoreUnknown = true) + class Instance { + private String id; + private String tenantId; + } +} diff --git a/src/main/java/org/folio/model/ResultList.java b/src/main/java/org/folio/model/ResultList.java new file mode 100644 index 00000000..1fdcde01 --- /dev/null +++ b/src/main/java/org/folio/model/ResultList.java @@ -0,0 +1,22 @@ +package org.folio.model; + +import com.fasterxml.jackson.annotation.JsonAlias; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import java.util.Collections; +import java.util.List; + +@Data +@NoArgsConstructor +@AllArgsConstructor(staticName = "of") +public class ResultList { + @JsonAlias("total_records") + private int totalRecords = 0; + + private List result = Collections.emptyList(); + + public static ResultList of(List result) { + return new ResultList<>(result.size(), result); + } +} diff --git a/src/main/java/org/folio/support/CqlQuery.java b/src/main/java/org/folio/support/CqlQuery.java new file mode 100644 index 00000000..398a16b7 --- /dev/null +++ b/src/main/java/org/folio/support/CqlQuery.java @@ -0,0 +1,9 @@ +package org.folio.support; + +import static java.lang.String.format; + +public record CqlQuery(String query) { + public static CqlQuery exactMatch(String index, String value) { + return new CqlQuery(format("%s==\"%s\"", index, value)); + } +} diff --git a/src/test/java/org/folio/client/SearchClientTest.java b/src/test/java/org/folio/client/SearchClientTest.java new file mode 100644 index 00000000..a8a98be0 --- /dev/null +++ b/src/test/java/org/folio/client/SearchClientTest.java @@ -0,0 +1,33 @@ +package org.folio.client; + +import org.folio.support.CqlQuery; +import org.folio.client.feign.SearchClient; +import org.folio.model.ResultList; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class SearchClientTest { + @Mock + private SearchClient searchClient; + + @Test + void canGetInstances() { + SearchClient.Instance instance = new SearchClient.Instance(UUID.randomUUID().toString(), "tenant1"); + ResultList mockResult = ResultList.of(List.of(instance)); + when(searchClient.searchInstances(any(CqlQuery.class), anyBoolean())).thenReturn(mockResult); + var response = searchClient.searchInstances(CqlQuery.exactMatch("id", UUID.randomUUID().toString()), true); + assertNotNull(response); + assertTrue(response.getTotalRecords() > 0); + } +}