Skip to content

Commit

Permalink
Merge pull request #5 from folio-org/MODTLR-7
Browse files Browse the repository at this point in the history
MODTLR-7 Added the client for mod-search
  • Loading branch information
MagzhanArtykov authored Jan 22, 2024
2 parents 42d6008 + 4b1d816 commit d0cb1c0
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/folio/EcsTlrApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/folio/client/feign/SearchClient.java
Original file line number Diff line number Diff line change
@@ -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<Instance> searchInstances(@RequestParam("query") CqlQuery cql, @RequestParam("expandAll") Boolean expandAll);

@AllArgsConstructor
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
class Instance {
private String id;
private String tenantId;
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/folio/model/ResultList.java
Original file line number Diff line number Diff line change
@@ -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<T> {
@JsonAlias("total_records")
private int totalRecords = 0;

private List<T> result = Collections.emptyList();

public static <R> ResultList<R> of(List<R> result) {
return new ResultList<>(result.size(), result);
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/folio/support/CqlQuery.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
33 changes: 33 additions & 0 deletions src/test/java/org/folio/client/SearchClientTest.java
Original file line number Diff line number Diff line change
@@ -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<SearchClient.Instance> 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);
}
}

0 comments on commit d0cb1c0

Please sign in to comment.