Skip to content

Commit

Permalink
feat(cbir): search across project (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
bathienle authored Aug 30, 2024
1 parent 622845f commit 3429ea0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/main/java/be/cytomine/dto/search/SearchResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
@AllArgsConstructor
@NoArgsConstructor
public class SearchResponse {

private String query;
private String storage;
private List<String> storage;
private String index;
private List<List<Object>> similarities;
}
11 changes: 10 additions & 1 deletion src/main/java/be/cytomine/service/search/RetrievalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
import be.cytomine.config.properties.ApplicationProperties;
import be.cytomine.domain.CytomineDomain;
import be.cytomine.domain.ontology.AnnotationDomain;
import be.cytomine.domain.project.Project;
import be.cytomine.domain.search.RetrievalServer;
import be.cytomine.dto.PimsResponse;
import be.cytomine.dto.search.SearchResponse;
import be.cytomine.service.ModelService;
import be.cytomine.service.dto.CropParameter;
import be.cytomine.service.middleware.ImageServerService;
import be.cytomine.service.project.ProjectService;
import be.cytomine.utils.JsonObject;

@Slf4j
Expand All @@ -47,6 +49,8 @@ public class RetrievalService extends ModelService {

private final ImageServerService imageServerService;

private final ProjectService projectService;

private final RestTemplate restTemplate;

private final String baseUrl;
Expand All @@ -56,9 +60,11 @@ public class RetrievalService extends ModelService {
public RetrievalService(
ApplicationProperties applicationProperties,
ImageServerService imageServerService,
ProjectService projectService,
RestTemplate restTemplate
) {
this.imageServerService = imageServerService;
this.projectService = projectService;
this.restTemplate = restTemplate;
this.baseUrl = applicationProperties.getRetrievalServerURL();
}
Expand Down Expand Up @@ -152,9 +158,12 @@ public ResponseEntity<SearchResponse> retrieveSimilarImages(
String etag,
Long nrt_neigh
) throws ParseException, UnsupportedEncodingException {
List<Project> projects = projectService.listForCurrentUser();
List<Long> ids = projects.stream().map(CytomineDomain::getId).toList();

String url = UriComponentsBuilder
.fromHttpUrl(this.baseUrl + "/api/search")
.queryParam("storage", annotation.getProject().getId())
.queryParam("storage", ids)
.queryParam("index", this.indexName)
.queryParam("nrt_neigh", nrt_neigh + 1)
.toUriString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -18,6 +19,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.test.context.support.WithMockUser;

import be.cytomine.BasicInstanceBuilder;
import be.cytomine.CytomineCoreApplication;
Expand Down Expand Up @@ -132,20 +134,18 @@ public void delete_index_with_success() {
}

@Test
@WithMockUser(authorities = "ROLE_SUPER_ADMIN", username = "superadmin")
public void search_similar_images_with_success() throws JsonProcessingException, ParseException,
UnsupportedEncodingException {
UserAnnotation annotation =
UserAnnotationResourceTests.given_a_user_annotation_with_valid_image_server(builder);
UserAnnotation annotation = UserAnnotationResourceTests.given_a_user_annotation_with_valid_image_server(builder);

/* Simulate call to PIMS */
String id = URLEncoder.encode(annotation.getSlice().getBaseSlice().getPath(), StandardCharsets.UTF_8);
String url = "/image/" + id + "/annotation/crop";
String body = "{\"annotations\":{},\"level\":0,\"background_transparency\":0,\"z_slices\":0,\"timepoints\":0}";

wireMockServer.stubFor(WireMock.post(urlEqualTo(url))
.withRequestBody(
WireMock.equalToJson(body)
)
.withRequestBody(WireMock.equalToJson(body))
.willReturn(aResponse()
.withStatus(HttpStatus.OK.value())
.withBody(UUID.randomUUID().toString().getBytes())
Expand All @@ -154,10 +154,9 @@ public void search_similar_images_with_success() throws JsonProcessingException,

/* Simulate call to CBIR */
ObjectMapper objectMapper = new ObjectMapper();
String expectedUrlPath = "/api/search";
SearchResponse expectedResponse = new SearchResponse(
annotation.getId().toString(),
annotation.getProject().getId().toString(),
List.of(annotation.getProject().getId().toString()),
"annotation",
Arrays.asList(
Arrays.asList(annotation.getId().toString(), 0.0),
Expand All @@ -166,9 +165,9 @@ public void search_similar_images_with_success() throws JsonProcessingException,
)
);

wireMockServer.stubFor(WireMock.post(urlPathEqualTo(expectedUrlPath))
.withQueryParam("storage", WireMock.equalTo(annotation.getProject().getId().toString()))
.withQueryParam("index", WireMock.equalTo("annotation"))
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/search"))
.withQueryParam("storage", WireMock.matching(".*"))
.withQueryParam("index", WireMock.matching(".*"))
.willReturn(aResponse()
.withStatus(HttpStatus.OK.value())
.withHeader("Content-Type", "application/json")
Expand Down

0 comments on commit 3429ea0

Please sign in to comment.