Skip to content

Commit

Permalink
Merge pull request #75 from aodn/features/5551-add-missing-field
Browse files Browse the repository at this point in the history
Fix bug on contact
  • Loading branch information
vietnguyengit authored May 30, 2024
2 parents 7c62fc4 + 0e59ecc commit 571453e
Show file tree
Hide file tree
Showing 14 changed files with 4,960 additions and 647 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,40 @@
@Service
public class IndexerServiceImpl implements IndexerService {

@Autowired
GeoNetworkService geoNetworkResourceService;

@Autowired
ElasticsearchClient portalElasticsearchClient;

@Autowired
ElasticSearchIndexService elasticSearchIndexService;

@Value("${elasticsearch.index.name}")
private String indexName;

@Autowired
ObjectMapper indexerObjectMapper;

@Autowired
protected String indexName;
protected GeoNetworkService geoNetworkResourceService;
protected ElasticsearchClient portalElasticsearchClient;
protected ElasticSearchIndexService elasticSearchIndexService;
protected ObjectMapper indexerObjectMapper;
protected StacCollectionMapperService stacCollectionMapperService;
protected JaxbUtils<MDMetadataType> jaxbUtils;
protected RankingService rankingService;
protected AodnDiscoveryParameterVocabService aodnDiscoveryParameterVocabService;

@Autowired
JaxbUtils<MDMetadataType> jaxbUtils;

@Autowired
RankingService rankingService;
private static final Logger logger = LogManager.getLogger(IndexerServiceImpl.class);

@Autowired
AodnDiscoveryParameterVocabService aodnDiscoveryParameterVocabService;

private static final Logger logger = LogManager.getLogger(IndexerServiceImpl.class);
public IndexerServiceImpl(
@Value("${elasticsearch.index.name}") String indexName,
ObjectMapper indexerObjectMapper,
JaxbUtils<MDMetadataType> jaxbUtils,
RankingService rankingService,
GeoNetworkService geoNetworkResourceService,
ElasticsearchClient portalElasticsearchClient,
ElasticSearchIndexService elasticSearchIndexService,
StacCollectionMapperService stacCollectionMapperService,
AodnDiscoveryParameterVocabService aodnDiscoveryParameterVocabService
) {
this.indexName = indexName;
this.indexerObjectMapper = indexerObjectMapper;
this.jaxbUtils = jaxbUtils;
this.rankingService = rankingService;
this.geoNetworkResourceService = geoNetworkResourceService;
this.portalElasticsearchClient = portalElasticsearchClient;
this.elasticSearchIndexService = elasticSearchIndexService;
this.stacCollectionMapperService = stacCollectionMapperService;
this.aodnDiscoveryParameterVocabService = aodnDiscoveryParameterVocabService;
}

public Hit<ObjectNode> getDocumentByUUID(String uuid) throws IOException {
try {
Expand Down Expand Up @@ -175,7 +181,7 @@ public ResponseEntity<String> indexMetadata(String metadataValues) {
if (this.isMetadataPublished(uuid)) {
try(InputStream is = new ByteArrayInputStream(indexerObjectMapper.writeValueAsBytes(mappedMetadataValues))) {
logger.info("Ingesting a new metadata with UUID: " + uuid + " to index: " + indexName);
logger.info("{}", mappedMetadataValues);
logger.debug("{}", mappedMetadataValues);
req = IndexRequest.of(b -> b
.index(indexName)
.withJson(is));
Expand Down

Large diffs are not rendered by default.

407 changes: 407 additions & 0 deletions indexer/src/main/java/au/org/aodn/esindexer/utils/MapperUtils.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import au.org.aodn.esindexer.BaseTestClass;
import au.org.aodn.esindexer.configuration.GeoNetworkSearchTestConfig;
import au.org.aodn.stac.model.StacCollectionModel;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.testcontainers.shaded.org.checkerframework.checker.units.qual.A;

Expand Down Expand Up @@ -140,15 +142,17 @@ public void verifyGetDocumentByUUID() throws IOException {
Hit<ObjectNode> objectNodeHit = indexerService.getDocumentByUUID(uuid);

String test = objectNodeHit.source().toPrettyString();

assertEquals("Stac equals " + uuid, indexerObjectMapper.readTree(expected), indexerObjectMapper.readTree(test));
}
finally {
deleteRecord(uuid);
}
}
/**
* Some dataset can provide links to logos, this test is use to verify the logo links added correctly to the
* Some dataset can provide links to logos, this test is use to verify the logo links added correctly to the STAC,
* this function is better test with docker image as it need to invoke some additional function where we need to
* verify it works too.
*
* @throws IOException - If file not found
*/
@Test
Expand Down Expand Up @@ -185,6 +189,7 @@ public void verifyThumbnailLinkAddedOnIndex() throws IOException {
Hit<ObjectNode> objectNodeHit = indexerService.getDocumentByUUID(uuid);

String test = objectNodeHit.source().toPrettyString();
logger.info(test);
assertEquals("Stac equals " + uuid, indexerObjectMapper.readTree(expected), indexerObjectMapper.readTree(test));
}
finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package au.org.aodn.esindexer.service;

import au.org.aodn.esindexer.utils.JaxbUtils;
import au.org.aodn.metadata.iso19115_3_2018.MDMetadataType;
import au.org.aodn.stac.model.StacCollectionModel;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.IndexRequest;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.elasticsearch.core.SearchResponse;

import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import co.elastic.clients.elasticsearch.core.search.TotalHits;
import co.elastic.clients.json.JsonData;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.xml.bind.JAXBException;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;

import static au.org.aodn.esindexer.BaseTestClass.readResourceFile;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

/**
* This test case different from the others as it didn't create docker image to test the whole flow. It only mock the
* component for speed testing.
*/
@Slf4j
@SpringBootTest(classes = {StacCollectionMapperServiceImpl.class})
public class StacCollectionMapperServiceTests {

protected ObjectMapper objectMapper = new ObjectMapper();
protected JaxbUtils<MDMetadataType> jaxbUtils = new JaxbUtils<>(MDMetadataType.class);

@MockBean
protected GeoNetworkServiceImpl geoNetworkResourceService;

@MockBean
protected ElasticsearchClient portalElasticsearchClient;

@MockBean
protected ElasticSearchIndexService elasticSearchIndexService;

@MockBean
protected AodnDiscoveryParameterVocabService aodnDiscoveryParameterVocabService;

@MockBean
protected RankingService rankingService;

@Autowired
protected StacCollectionMapperService service;

protected AtomicReference<IndexRequest<JsonData>> lastRequest = new AtomicReference<>();

protected IndexerServiceImpl indexerService;

public StacCollectionMapperServiceTests() throws JAXBException {
}

@AfterEach
public void cleanUp() {
Mockito.reset(
geoNetworkResourceService,
portalElasticsearchClient,
elasticSearchIndexService,
aodnDiscoveryParameterVocabService
);
}

@BeforeEach
public void createIndexerService() throws IOException {
indexerService = new IndexerServiceImpl(
"any-works",
objectMapper,
jaxbUtils,
rankingService,
geoNetworkResourceService,
portalElasticsearchClient,
elasticSearchIndexService,
service,
aodnDiscoveryParameterVocabService
);

// Number is 0, pretend fresh elastic instance
when(elasticSearchIndexService.getDocumentsCount(anyString()))
.thenReturn(0L);

doNothing()
.when(elasticSearchIndexService)
.createIndexFromMappingJSONFile(anyString(), anyString());

doAnswer(ans -> {
lastRequest.set(ans.getArgument(0));
IndexResponse response = Mockito.mock(IndexResponse.class);

when(response.version()).thenReturn(1L);
when(response.toString()).thenReturn("");

return response;
})
.when(portalElasticsearchClient)
.index(any(IndexRequest.class));

doAnswer(ans -> {
SearchResponse<ObjectNode> response = Mockito.mock();
HitsMetadata<ObjectNode> metadata = Mockito.mock();
TotalHits totalHits = Mockito.mock();

when(totalHits.value()).thenReturn(1L);
when(metadata.total()).thenReturn(totalHits);
when(response.hits()).thenReturn(metadata);

return response;
})
.when(portalElasticsearchClient)
.search(any(Function.class), eq(ObjectNode.class));

// Pretend empty elastic
when(geoNetworkResourceService.isMetadataRecordsCountLessThan(anyInt()))
.thenReturn(Boolean.TRUE);

when(geoNetworkResourceService.searchRecordBy(anyString()))
.thenReturn("");

when(rankingService.evaluateCompleteness(any(StacCollectionModel.class)))
.thenReturn(1);

}

@Test
public void verifyPointOfContactCorrect() throws IOException, JAXBException {
// We only index one record, the
String xml = readResourceFile("classpath:canned/sample8.xml");
String expected = readResourceFile("classpath:canned/sample8_stac.json");
indexerService.indexMetadata(xml);

// We use a mock to pretend insert value into Elastic, there we store what is being send to elastic
// and now we can use it to compare expected result.
Map<?,?> content = objectMapper.readValue(lastRequest.get().document().toString(), Map.class);
String out = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(content);
assertEquals("Stac equals", objectMapper.readTree(expected), objectMapper.readTree(out.strip()));
}
}
Loading

0 comments on commit 571453e

Please sign in to comment.