Search in sources :

Example 36 with EventPayload

use of io.divolte.server.ServerTestUtils.EventPayload 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());
}
Also used : DslRecordMapper(io.divolte.server.recordmapping.DslRecordMapper) Config(com.typesafe.config.Config) ValidatedConfiguration(io.divolte.server.config.ValidatedConfiguration) JsonParser(com.fasterxml.jackson.core.JsonParser) CityResponse(com.maxmind.geoip2.model.CityResponse) LookupService(io.divolte.server.ip2geo.LookupService) GenericRecord(org.apache.avro.generic.GenericRecord) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) EventPayload(io.divolte.server.ServerTestUtils.EventPayload) Test(org.junit.Test)

Example 37 with EventPayload

use of io.divolte.server.ServerTestUtils.EventPayload in project divolte-collector by divolte.

the class DslRecordMapperTest method shouldStopOnNestedStop.

@Test
public void shouldStopOnNestedStop() throws IOException, InterruptedException {
    setupServer("nested-conditional-stop.groovy");
    final EventPayload event = request("http://www.example.com");
    assertEquals("happened", event.record.get("client"));
    assertNull(event.record.get("session"));
}
Also used : EventPayload(io.divolte.server.ServerTestUtils.EventPayload) Test(org.junit.Test)

Example 38 with EventPayload

use of io.divolte.server.ServerTestUtils.EventPayload in project divolte-collector by divolte.

the class DslRecordMapperTest method shouldParseUriComponents.

@Test
public void shouldParseUriComponents() throws IOException, InterruptedException {
    setupServer("uri-mapping.groovy");
    final EventPayload event = request("https://www.example.com:8080/path/to/resource/page.html?q=multiple+words+%24%23%25%26&p=10&p=20", "http://example.com/path/to/resource/page.html?q=divolte&p=42#/client/side/path?x=value&y=42");
    assertEquals("https", event.record.get("uriScheme"));
    assertEquals("/path/to/resource/page.html", event.record.get("uriPath"));
    assertEquals("www.example.com", event.record.get("uriHost"));
    assertEquals(8080, event.record.get("uriPort"));
    assertEquals("/client/side/path?x=value&y=42", event.record.get("uriFragment"));
    assertEquals("q=multiple+words+$#%&&p=10&p=20", event.record.get("uriQueryString"));
    assertEquals("multiple words $#%&", event.record.get("uriQueryStringValue"));
    assertEquals(Arrays.asList("10", "20"), event.record.get("uriQueryStringValues"));
    assertEquals(ImmutableMap.of("p", Arrays.asList("10", "20"), "q", Collections.singletonList("multiple words $#%&")), event.record.get("uriQuery"));
}
Also used : EventPayload(io.divolte.server.ServerTestUtils.EventPayload) Test(org.junit.Test)

Example 39 with EventPayload

use of io.divolte.server.ServerTestUtils.EventPayload in project divolte-collector by divolte.

the class DslRecordMapperTest method shouldExitFromSectionOnConditionClosureSyntax.

@Test
public void shouldExitFromSectionOnConditionClosureSyntax() throws IOException, InterruptedException {
    setupServer("nested-conditional-exit-closure.groovy");
    final EventPayload event = request("http://www.example.com");
    assertEquals("happened", event.record.get("client"));
    assertEquals("happened", event.record.get("pageview"));
    assertEquals("happened", event.record.get("event"));
    assertEquals("happened", event.record.get("customCookie"));
    assertNull(event.record.get("session"));
}
Also used : EventPayload(io.divolte.server.ServerTestUtils.EventPayload) Test(org.junit.Test)

Example 40 with EventPayload

use of io.divolte.server.ServerTestUtils.EventPayload in project divolte-collector by divolte.

the class DslRecordMapperTest method shouldTreatRuntimeEventParameterMappingMismatchAsNonPresent.

@Test
public void shouldTreatRuntimeEventParameterMappingMismatchAsNonPresent() throws IOException, InterruptedException {
    setupServer("event-param-jsonpath-mismatch.groovy");
    final EventPayload event = request("http://example.com/", Collections.singletonList(HETEROGENOUS_EVENT_PARAMS));
    // Nothing should have been mapped here.
    assertNull(event.record.get("paramIntValue"));
    // This is mapped last: it should have completed even though an earlier field mapping failed.
    assertTrue((Boolean) event.record.get("flag1"));
}
Also used : EventPayload(io.divolte.server.ServerTestUtils.EventPayload) Test(org.junit.Test)

Aggregations

EventPayload (io.divolte.server.ServerTestUtils.EventPayload)46 Test (org.junit.Test)46 HttpURLConnection (java.net.HttpURLConnection)5 URL (java.net.URL)5 GenericRecord (org.apache.avro.generic.GenericRecord)3 Utf8 (org.apache.avro.util.Utf8)3 JsonParser (com.fasterxml.jackson.core.JsonParser)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 CityResponse (com.maxmind.geoip2.model.CityResponse)2 Config (com.typesafe.config.Config)2 ValidatedConfiguration (io.divolte.server.config.ValidatedConfiguration)2 LookupService (io.divolte.server.ip2geo.LookupService)2 DslRecordMapper (io.divolte.server.recordmapping.DslRecordMapper)2 Instant (java.time.Instant)2 Optional (java.util.Optional)2 Stream (java.util.stream.Stream)2 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)2 Assert (org.junit.Assert)2 WebElement (org.openqa.selenium.WebElement)2 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)2