Skip to content

Commit

Permalink
Fix bug after PR
Browse files Browse the repository at this point in the history
  • Loading branch information
utas-raymondng committed Jul 8, 2024
1 parent 47eada7 commit 2534c5e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,13 @@ public RestTemplate createRestTemplate() {
}

@Bean
public GenericEntityListener createGenericEntityListener() {
return new GenericEntityListener();
public GenericEntityListener createGenericEntityListener(
@Value("${aodn.geonetwork4.esIndexer.apikey}") String apiKey,
@Value("${aodn.geonetwork4.esIndexer.host}") String host,
@Value("${aodn.geonetwork4.esIndexer.urlIndex}") String indexUrl,
RestTemplate restTemplate) {

return new GenericEntityListener(apiKey, host, indexUrl, restTemplate);
}
/**
* Must use prototype scope as there is a XSRF-TOKEN header for each api, that cannot share
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.fao.geonet.entitylistener.GeonetworkEntityListener;
import org.fao.geonet.entitylistener.PersistentEventType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
Expand All @@ -33,13 +32,10 @@ public class GenericEntityListener implements GeonetworkEntityListener<Metadata>

protected static final String UUID = "uuid";

@Value("${aodn.geonetwork4.esIndexer.urlIndex}")
protected String indexUrl;

@Value("${aodn.geonetwork4.esIndexer.apikey}")
protected String apiKey;

@Autowired
protected RestTemplate restTemplate;

@Override
Expand All @@ -54,6 +50,14 @@ public void cleanUp() {

protected int delayStart = 5;

@Autowired
public GenericEntityListener(String apiKey, String host, String indexUrl, RestTemplate restTemplate) {

this.apiKey = apiKey;
this.indexUrl = host != null && !host.isEmpty() ? indexUrl : null;
this.restTemplate = restTemplate;
}

@PostConstruct
public void init() {
if(indexUrl == null) {
Expand Down
2 changes: 1 addition & 1 deletion geonetwork-core/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
aodn.geonetwork4.esIndexer.host=${INDEXER_HOST:http://localhost}
aodn.geonetwork4.esIndexer.host=${INDEXER_HOST:}
aodn.geonetwork4.esIndexer.port=${INDEXER_PORT:80}
aodn.geonetwork4.esIndexer.apikey=${INDEXER_APIKEY}
aodn.geonetwork4.esIndexer.urlIndex=${aodn.geonetwork4.esIndexer.host}:${aodn.geonetwork4.esIndexer.port}/api/v1/indexer/index/{uuid}
Expand Down
5 changes: 5 additions & 0 deletions geonetwork-core/src/main/resources/log4j-imos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@
<AppenderRef ref="File"/>
</Logger>

<Logger name="au.org.aodn.geonetwork4.handler" level="debug" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Logger>

<Logger name="au.org.aodn.geonetwork_api" level="debug" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class GenericEntityListenerTest {
@Test
public void verifyUpdateDeleteBehavior() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
GenericEntityListener listener = new GenericEntityListener();
RestTemplate template = Mockito.mock(RestTemplate.class);

// Set of test only, a mock to count what have been called
listener.indexUrl = "http://localhost/api/v1/indexer/index/{uuid}";
listener.apiKey = "test-key";
listener.restTemplate = template;

GenericEntityListener listener = new GenericEntityListener(
"test-key",
"localhost",
"http://localhost/api/v1/indexer/index/{uuid}",
template);
listener.init();

// Test data
Expand Down Expand Up @@ -80,7 +80,6 @@ public void verifyUpdateDeleteBehavior() throws InterruptedException {
@Test
public void verifyRetryBehavior() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
GenericEntityListener listener = new GenericEntityListener();
RestTemplate template = Mockito.mock(RestTemplate.class);

// Throw exception on first call
Expand All @@ -105,9 +104,11 @@ public void verifyRetryBehavior() throws InterruptedException {
.thenReturn(ResponseEntity.ok(null));

// Set of test only, a mock to count what have been called
listener.indexUrl = "http://localhost/api/v1/indexer/index/{uuid}";
listener.apiKey = "test-key";
listener.restTemplate = template;
GenericEntityListener listener = new GenericEntityListener(
"test-key",
"localhost",
"http://localhost/api/v1/indexer/index/{uuid}",
template);

listener.init();

Expand All @@ -132,4 +133,23 @@ public void verifyRetryBehavior() throws InterruptedException {
assertEquals("Map not contains uuid", 0, listener.updateMap.size());
assertEquals("Delete not contains uuid", 0, listener.deleteMap.size());
}
/**
* If host null, then we disable api call to indexer
*/
@Test
public void verifyIndexerCanBeDisabled() {
RestTemplate template = Mockito.mock(RestTemplate.class);
GenericEntityListener listener = new GenericEntityListener(
"test-key",
null,
"http://localhost/api/v1/indexer/index/{uuid}",
template);

listener.init();
listener.handleEvent(PersistentEventType.PostUpdate, new Metadata());
listener.handleEvent(PersistentEventType.PostRemove, new Metadata());

assertTrue("Internal update map empty", listener.updateMap.isEmpty());
assertTrue("Internal delete map empty", listener.deleteMap.isEmpty());
}
}

0 comments on commit 2534c5e

Please sign in to comment.