use of io.divolte.server.ip2geo.LookupService in project divolte-collector by divolte.
the class DslRecordMapperTest method shouldMapAllGeoIpFields.
@Test
public void shouldMapAllGeoIpFields() throws IOException, InterruptedException, ClosedServiceException {
/*
* Have to work around not being able to create a HttpServerExchange a bit.
* We setup a actual server just to do a request and capture the HttpServerExchange
* instance. Then we setup a DslRecordMapper instance with a mock ip2geo lookup service,
* we then use the previously captured exchange object against our locally created mapper
* instance to test the ip2geo mapping (using the a mock lookup service).
*/
setupServer("minimal-mapping.groovy");
final EventPayload payload = request("http://www.example.com");
final File geoMappingFile = File.createTempFile("geo-mapping", ".groovy");
copyResourceToFile("geo-mapping.groovy", geoMappingFile);
final ImmutableMap<String, Object> mappingConfig = ImmutableMap.of("divolte.mappings.test.mapping_script_file", geoMappingFile.getAbsolutePath(), "divolte.mappings.test.schema_file", avroFile.getAbsolutePath());
final Config geoConfig = ConfigFactory.parseMap(mappingConfig).withFallback(ConfigFactory.parseResources("base-test-server.conf")).withFallback(ConfigFactory.parseResources("reference-test.conf"));
final ValidatedConfiguration vc = new ValidatedConfiguration(() -> geoConfig);
final CityResponse mockResponseWithEverything = loadFromClassPath("/city-response-with-everything.json", new TypeReference<CityResponse>() {
});
final Map<String, Object> expectedMapping = loadFromClassPath("/city-response-expected-mapping.json", new TypeReference<Map<String, Object>>() {
});
final LookupService mockLookupService = mock(LookupService.class);
when(mockLookupService.lookup(any())).thenReturn(Optional.of(mockResponseWithEverything));
final DslRecordMapper mapper = new DslRecordMapper(vc, geoMappingFile.getAbsolutePath(), new Schema.Parser().parse(Resources.toString(Resources.getResource("TestRecord.avsc"), StandardCharsets.UTF_8)), Optional.of(mockLookupService));
final GenericRecord record = mapper.newRecordFromExchange(payload.event);
// Validate the results.
verify(mockLookupService).lookup(any());
verifyNoMoreInteractions(mockLookupService);
expectedMapping.forEach((k, v) -> {
final Object recordValue = record.get(k);
assertEquals("Property " + k + " not mapped correctly.", v, recordValue);
});
Files.delete(geoMappingFile.toPath());
}
Aggregations