-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Bugfix: Ingest metadata can be null if there is no processor created 2. Refactoring: Moved private method to another class for better testing support 3. Refactoring: Set some private static final variable as public so that unit test can use it 4. Refactoring: Changed string value to static variable Signed-off-by: Heemin Kim <[email protected]>
- Loading branch information
Showing
6 changed files
with
164 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/org/opensearch/geospatial/ip2geo/common/Ip2GeoProcessorFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.ip2geo.common; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.opensearch.common.inject.Inject; | ||
import org.opensearch.geospatial.ip2geo.processor.Ip2GeoProcessor; | ||
import org.opensearch.ingest.IngestMetadata; | ||
import org.opensearch.ingest.IngestService; | ||
|
||
public class Ip2GeoProcessorFacade { | ||
private final IngestService ingestService; | ||
|
||
@Inject | ||
public Ip2GeoProcessorFacade(final IngestService ingestService) { | ||
this.ingestService = ingestService; | ||
} | ||
|
||
public List<Ip2GeoProcessor> getProcessors(final String datasourceName) { | ||
IngestMetadata ingestMetadata = ingestService.getClusterService().state().getMetadata().custom(IngestMetadata.TYPE); | ||
if (ingestMetadata == null) { | ||
return Collections.emptyList(); | ||
} | ||
return ingestMetadata.getPipelines() | ||
.keySet() | ||
.stream() | ||
.flatMap(pipelineId -> ingestService.getProcessorsInPipeline(pipelineId, Ip2GeoProcessor.class).stream()) | ||
.filter(ip2GeoProcessor -> ip2GeoProcessor.getDatasourceName().equals(datasourceName)) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/test/java/org/opensearch/geospatial/ip2geo/common/Ip2GeoProcessorFacadeTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.ip2geo.common; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.Before; | ||
import org.opensearch.common.bytes.BytesReference; | ||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.geospatial.GeospatialTestHelper; | ||
import org.opensearch.geospatial.ip2geo.Ip2GeoTestCase; | ||
import org.opensearch.geospatial.ip2geo.processor.Ip2GeoProcessor; | ||
import org.opensearch.ingest.IngestMetadata; | ||
import org.opensearch.ingest.PipelineConfiguration; | ||
|
||
public class Ip2GeoProcessorFacadeTests extends Ip2GeoTestCase { | ||
private Ip2GeoProcessorFacade ip2GeoProcessorFacade; | ||
|
||
@Before | ||
public void init() { | ||
ip2GeoProcessorFacade = new Ip2GeoProcessorFacade(ingestService); | ||
} | ||
|
||
public void testGetProcessors_whenNullMetadata_thenReturnEmpty() { | ||
String datasourceName = GeospatialTestHelper.randomLowerCaseString(); | ||
when(metadata.custom(IngestMetadata.TYPE)).thenReturn(null); | ||
|
||
List<Ip2GeoProcessor> ip2GeoProcessorList = ip2GeoProcessorFacade.getProcessors(datasourceName); | ||
assertTrue(ip2GeoProcessorList.isEmpty()); | ||
} | ||
|
||
public void testGetProcessors_whenNoProcessorForGivenDatasource_thenReturnEmpty() { | ||
String datasourceBeingUsed = GeospatialTestHelper.randomLowerCaseString(); | ||
String datasourceNotBeingUsed = GeospatialTestHelper.randomLowerCaseString(); | ||
String pipelineId = GeospatialTestHelper.randomLowerCaseString(); | ||
Map<String, PipelineConfiguration> pipelines = new HashMap<>(); | ||
pipelines.put(pipelineId, createPipelineConfiguration()); | ||
IngestMetadata ingestMetadata = new IngestMetadata(pipelines); | ||
when(metadata.custom(IngestMetadata.TYPE)).thenReturn(ingestMetadata); | ||
Ip2GeoProcessor ip2GeoProcessor = randomIp2GeoProcessor(datasourceBeingUsed); | ||
when(ingestService.getProcessorsInPipeline(pipelineId, Ip2GeoProcessor.class)).thenReturn(Arrays.asList(ip2GeoProcessor)); | ||
|
||
List<Ip2GeoProcessor> ip2GeoProcessorList = ip2GeoProcessorFacade.getProcessors(datasourceNotBeingUsed); | ||
assertTrue(ip2GeoProcessorList.isEmpty()); | ||
} | ||
|
||
public void testGetProcessors_whenProcessorsForGivenDatasource_thenReturnProcessors() { | ||
String datasourceName = GeospatialTestHelper.randomLowerCaseString(); | ||
String pipelineId = GeospatialTestHelper.randomLowerCaseString(); | ||
Map<String, PipelineConfiguration> pipelines = new HashMap<>(); | ||
pipelines.put(pipelineId, createPipelineConfiguration()); | ||
IngestMetadata ingestMetadata = new IngestMetadata(pipelines); | ||
when(metadata.custom(IngestMetadata.TYPE)).thenReturn(ingestMetadata); | ||
Ip2GeoProcessor ip2GeoProcessor = randomIp2GeoProcessor(datasourceName); | ||
when(ingestService.getProcessorsInPipeline(pipelineId, Ip2GeoProcessor.class)).thenReturn(Arrays.asList(ip2GeoProcessor)); | ||
|
||
List<Ip2GeoProcessor> ip2GeoProcessorList = ip2GeoProcessorFacade.getProcessors(datasourceName); | ||
assertEquals(1, ip2GeoProcessorList.size()); | ||
assertEquals(ip2GeoProcessor.getDatasourceName(), ip2GeoProcessorList.get(0).getDatasourceName()); | ||
} | ||
|
||
private PipelineConfiguration createPipelineConfiguration() { | ||
String id = GeospatialTestHelper.randomLowerCaseString(); | ||
ByteBuffer byteBuffer = ByteBuffer.wrap(GeospatialTestHelper.randomLowerCaseString().getBytes(StandardCharsets.US_ASCII)); | ||
BytesReference config = BytesReference.fromByteBuffer(byteBuffer); | ||
return new PipelineConfiguration(id, config, XContentType.JSON); | ||
} | ||
} |