Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vietnguyengit committed Aug 24, 2024
1 parent bd8f670 commit 1454d66
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package au.org.aodn.ogcapi.server.core.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ArdcVocabModel {
// properties are extendable (e.g platformVocabs, organisationVocabs etc.), currently just parameterVocabs.
@JsonProperty("parameter_vocab")
ParameterVocabModel parameterVocabModel;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package au.org.aodn.ogcapi.server.core.model;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.*;

import java.util.List;

Expand All @@ -12,6 +10,8 @@
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ParameterVocabModel {

protected String label;
Expand Down
77 changes: 52 additions & 25 deletions server/src/test/java/au/org/aodn/ogcapi/server/BaseTestClass.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package au.org.aodn.ogcapi.server;

import au.org.aodn.ogcapi.server.core.model.ArdcVocabModel;
import au.org.aodn.ogcapi.server.core.model.ParameterVocabModel;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.client.Request;
Expand All @@ -27,7 +28,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -122,36 +122,63 @@ protected void createElasticIndex() {
});
}

protected void insertTestAodnDiscoveryParameterVocabs() {
BulkRequest.Builder bulkRequest = new BulkRequest.Builder();
protected void insertTestArdcVocabs() throws IOException {

ObjectMapper objectMapper = new ObjectMapper();
List<ArdcVocabModel> vocabs = new ArrayList<>();

// Read the JSON files
try {
// Read the JSON file
File file = ResourceUtils.getFile("classpath:databag/aodn_discovery_parameter_vocabs.json");
// Parse the JSON content
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(file);
// Get the array from the JSON content
JsonNode parameter_vocabs = jsonNode.get("parameter_vocabs");
// Iterate over the JSON array
for (JsonNode vocab : parameter_vocabs) {
// convert parameterVocabModel values to binary data
logger.debug("Ingested json is {}", vocab);
// send bulk request to Elasticsearch
bulkRequest.operations(op -> op
.index(idx -> idx
.index(vocabs_index_name)
.document(vocab)
)
);

// parameter vocabs
JsonNode parameterVocabNodes = jsonNode.get("parameter_vocabs");

// populate data for parameter vocabs list
for (JsonNode parameterVocabNode : parameterVocabNodes) {
ParameterVocabModel parameterVocabModel = objectMapper.readValue(parameterVocabNode.toString(), ParameterVocabModel.class);
ArdcVocabModel vocab = ArdcVocabModel.builder().parameterVocabModel(parameterVocabModel).build();
vocabs.add(vocab);
}
BulkResponse result = client.bulk(bulkRequest.build());
assertEquals(parameter_vocabs.size(), result.items().size(), "Number of docs stored is correct");
} catch (JsonProcessingException e) {
logger.error("Failed to ingest test parameter vocabs to {}", vocabs_index_name);
throw new RuntimeException(e);
} catch (IOException e) {
logger.error("Failed to ingest test parameter vocabs to {}", vocabs_index_name);
throw new RuntimeException(e);
}

logger.info("Indexing all vocabs to {}", vocabs_index_name);
bulkIndexVocabs(vocabs);
}

protected void bulkIndexVocabs(List<ArdcVocabModel> vocabs) throws IOException {
// count portal index documents, or create index if not found from defined mapping JSON file
BulkRequest.Builder bulkRequest = new BulkRequest.Builder();

for (ArdcVocabModel vocab : vocabs) {
// send bulk request to Elasticsearch
bulkRequest.operations(op -> op
.index(idx -> idx
.index(vocabs_index_name)
.document(vocab)
)
);
}

BulkResponse result = client.bulk(bulkRequest.build());

// Flush after insert, otherwise you need to wait for next auto-refresh. It is
// especially a problem with autotest, where assert happens very fast.
client.indices().refresh();

// Log errors, if any
if (result.errors()) {
logger.error("Bulk had errors");
for (BulkResponseItem item: result.items()) {
if (item.error() != null) {
logger.error("{} {}", item.error().reason(), item.error().causedBy());
}
}
}
}

protected void insertJsonToElasticIndex(String... filenames) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import au.org.aodn.ogcapi.server.BaseTestClass;

import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
Expand Down Expand Up @@ -157,7 +156,7 @@ public void verifyApiResponseOnTypoInputMultipleParameterVocabsFilterNoResults()

@Test
public void verifyApiResponseOnParameterVocabSuggestions() throws IOException {
super.insertTestAodnDiscoveryParameterVocabs();
super.insertTestArdcVocabs();

super.insertJsonToElasticIndex(
"19da2ce7-138f-4427-89de-a50c724f5f54.json"
Expand Down
2 changes: 1 addition & 1 deletion server/src/test/resources/vocabs_index_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"mappings": {
"dynamic": true,
"properties": {
"parameter_vocabs": {
"parameter_vocab": {
"properties": {
"label": {
"type": "search_as_you_type",
Expand Down

0 comments on commit 1454d66

Please sign in to comment.