Skip to content

Commit

Permalink
fix Elasticsearch backend
Browse files Browse the repository at this point in the history
  • Loading branch information
PacoVK committed Oct 26, 2023
1 parent a4dccb4 commit 1bcfbcd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.HttpMethod;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -211,8 +212,22 @@ public void bootstrap() throws IOException {
createIndexIfNotExists("deploykeys");
}

Boolean indexHasDocuments(String indexName) throws IOException {
Request countRequest = new Request(
HttpMethod.GET,
String.format("/%s/_count", indexName)
);
HttpEntity countEntity = restClient.performRequest(countRequest).getEntity();
String countResponseBody = EntityUtils.toString(countEntity);
Integer count = (Integer) new JsonObject(countResponseBody).getValue("count");
return count > 0;
}

PaginationDto getEntities(String indexName, String query, Class<? extends CoreEntity> type)
throws IOException {
if (!indexHasDocuments(indexName)) {
return new PaginationDto(Collections.EMPTY_LIST);
}
Request request = new Request(
HttpMethod.GET,
String.format("/%s/_search", indexName)
Expand Down Expand Up @@ -267,7 +282,8 @@ public PaginationDto findProviders(String identifier, Integer limit, String term
}

@Override
public PaginationDto findDeployKeys(String identifier, Integer limit, String term) throws Exception {
public PaginationDto findDeployKeys(String identifier, Integer limit, String term)
throws Exception {
String fields = "\"id\", \"key\"";
String query = buildPaginationSearchQuery(identifier, limit, term, fields);
return getEntities("deploykeys", query, DeployKey.class);
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/core/backend/AbstractDeployKeysBackendTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ void findDeployKeys() throws Exception {
assertEquals(withIdentifier.getEntities().size(), 1);
assertEquals(all.getEntities().size(), 2);
}

@Test
void findDeployKeysIfNoDeployKeysAreStored() throws Exception {
PaginationDto result = repository.findDeployKeys("", 5, "fred");
assertEquals(result.getEntities().size(), 0);
}
}

0 comments on commit 1bcfbcd

Please sign in to comment.