@Test public void testAlertMultipleAssociations() { Date d = new Date(); double lat = 10.0; double lon = 10.0; // Make a product with location and time ProductSummary product = new ProductSummary(); product.setEventLatitude(new BigDecimal(lat)); product.setEventLongitude(new BigDecimal(lon)); product.setEventTime(d); product.setId(new ProductId("us", "origin", "c0001234")); product.setEventSource("us"); product.setEventSourceCode("c0001234"); // Make 3 events with similar locations and times Event e1 = createDifferentEvent( new BigDecimal(lat + 0.1), new BigDecimal(lon + 0.1), new Date(d.getTime() + 10), "us", "origin", "c0002222"); Event e2 = createDifferentEvent( new BigDecimal(lat - 0.5), new BigDecimal(lon), new Date(d.getTime() + 50), "nc", "origin", "11112222"); Event e3 = createDifferentEvent( new BigDecimal(lat + 1), new BigDecimal(lon + 1), d, "ci", "origin", "aaaa2222"); Event e4 = createDifferentEvent( new BigDecimal(lat), new BigDecimal(lon), d, "uw", "origin", "00005555"); Event e5 = createDifferentEvent( new BigDecimal(lat), new BigDecimal(lon), d, "us", "origin", "00005555"); // Now add the events to a list, and find the most similar List<Event> events = new ArrayList<Event>(); events.add(e1); events.add(e2); events.add(e3); events.add(e4); events.add(e5); // Calculate which event is most similar to the product DefaultAssociator associator = new DefaultAssociator(); Event mostSimilar = associator.chooseEvent(events, product); Assert.assertEquals(e4, mostSimilar); }
/** * The DefaultAssociator will filter events that are from the same source as a summary if they * have a different code. * * <p>This test exists to prevent a nullpointerexception regression. */ @Test public void testChooseEventWithoutSummarySourceCode() { ProductSummary summary = new ProductSummary(); List<Event> events = new LinkedList<Event>(); DefaultAssociator associator = new DefaultAssociator(); try { Event event = associator.chooseEvent(events, summary); Assert.assertNull("Returned event null", event); } catch (NullPointerException npe) { Assert.fail("NullPointerException thrown"); } }
/** * Test if the associator selects using the most important attribute first. That is, use eventId * before location. * * <p>This test creates 2 events, A and B, and a summary X. A and B both have eventId and location * information. B's location is closest to X's location, but A's eventId matches X's. The * associator should choose A. */ @Test public void testAssociationOrder() { Date eventTime = new Date(); // Create the first event ProductSummary eventSummaryA = new ProductSummary(); eventSummaryA.setId(new ProductId("productsource", "producttype", "productcode")); eventSummaryA.setEventSource("source"); eventSummaryA.setEventSourceCode("code"); eventSummaryA.setEventLatitude(new BigDecimal("10.0")); eventSummaryA.setEventLongitude(new BigDecimal("10.0")); eventSummaryA.setEventTime(eventTime); Event eventA = new Event(); eventA.addProduct(eventSummaryA); // Create the second event ProductSummary eventSummaryB = new ProductSummary(); eventSummaryB.setId(new ProductId("productsource", "producttype", "productcode")); eventSummaryB.setEventSource("source2"); eventSummaryB.setEventSourceCode("code2"); eventSummaryB.setEventLatitude(new BigDecimal("0.0")); eventSummaryB.setEventLongitude(new BigDecimal("0.0")); eventSummaryB.setEventTime(eventTime); Event eventB = new Event(); eventB.addProduct(eventSummaryB); // Create the test summary ProductSummary summaryX = new ProductSummary(); summaryX.setId(new ProductId("productsource", "producttype", "productcode")); summaryX.setEventSource("source"); summaryX.setEventSourceCode("code"); summaryX.setEventLatitude(new BigDecimal("0.0")); summaryX.setEventLongitude(new BigDecimal("0.0")); summaryX.setEventTime(eventTime); // Add the events to a list List<Event> events = new LinkedList<Event>(); events.add(eventA); events.add(eventB); // Test to verify event A is chosen. DefaultAssociator associator = new DefaultAssociator(); Event event = associator.chooseEvent(events, summaryX); Assert.assertEquals("Event from same source with same code chosen", eventA, event); }
/** * Test choosing the "closest" event to associate a product to based on the lat, lon, and * eventTime values. This method creates a product with some lat, lon and time values then it * creates 3 events whose parameters only differ slightly from the product. It then chooses the * event whose Euclidean distance is lowest. * * @see #createEvent(BigDecimal, BigDecimal, Date) * @see gov.usgs.earthquake.indexer.DefaultAssociator#chooseMostSimilar(ProductSummary, List) */ @Test public void testChooseMostSimilar() { Date d = new Date(); double lat = 10.0; double lon = 10.0; // Make a product with location and time ProductSummary product = new ProductSummary(); product.setEventLatitude(new BigDecimal(lat)); product.setEventLongitude(new BigDecimal(lon)); product.setEventTime(d); // Make 3 events with similar locations and times Event e1 = createEvent( "test", "1", new BigDecimal(lat + 0.1), new BigDecimal(lon + 0.1), new Date(d.getTime() + 10)); Event e2 = createEvent( "test", "2", new BigDecimal(lat - 0.5), new BigDecimal(lon), new Date(d.getTime() + 50)); Event e3 = createEvent("test", "3", new BigDecimal(lat + 1), new BigDecimal(lon + 1), d); // Now add the events to a list, and find the most similar List<Event> events = new ArrayList<Event>(); events.add(e1); events.add(e2); events.add(e3); // Calculate which event is most similar to the product DefaultAssociator associator = new DefaultAssociator(); Event mostSimilar = associator.chooseMostSimilar(product, events); Assert.assertEquals(e1, mostSimilar); }
/** * When an event source submits data using the same code, they refer to the same event. * * <p>The associator shouldn't filter an event when it uses the same source and code (although it * won't necessarily choose that event because it has the same source and code). */ @Test public void testSameEventCodeFromSameSource() { ProductSummary summary = new ProductSummary(); summary.setEventSource("source"); summary.setEventSourceCode("code"); // build event from same source, with same code ProductSummary differentSummary = new ProductSummary(); differentSummary.setId(new ProductId("productsource", "producttype", "productcode")); differentSummary.setEventSource("source"); differentSummary.setEventSourceCode("code"); Event sameEvent = new Event(); sameEvent.addProduct(differentSummary); List<Event> events = new LinkedList<Event>(); events.add(sameEvent); // test to verify event is chosen DefaultAssociator associator = new DefaultAssociator(); Event event = associator.chooseEvent(events, summary); Assert.assertEquals("Event from same source with same code chosen", sameEvent, event); }
@Test public void testDateTimeLine() { Date eventTime = new Date(); DefaultAssociator associator = new DefaultAssociator(); // Create the first event ProductSummary eventSummaryA = new ProductSummary(); eventSummaryA.setId(new ProductId("us", "origin", "1234abcd")); eventSummaryA.setEventSource("us"); eventSummaryA.setEventSourceCode("1234abcd"); eventSummaryA.setEventLatitude(new BigDecimal("0.0")); eventSummaryA.setEventLongitude(new BigDecimal("179.80")); eventSummaryA.setEventTime(eventTime); Event eventA = new Event(); eventA.addProduct(eventSummaryA); // Create the second event ProductSummary eventSummaryB = new ProductSummary(); eventSummaryB.setId(new ProductId("ci", "origin", "5678efgh")); eventSummaryB.setEventSource("ci"); eventSummaryB.setEventSourceCode("5678efgh"); eventSummaryB.setEventLatitude(new BigDecimal("0.0")); eventSummaryB.setEventLongitude(new BigDecimal("179.50")); eventSummaryB.setEventTime(eventTime); Event eventB = new Event(); eventB.addProduct(eventSummaryB); /* * Verify that events associate (0.3 degrees apart) when the points are * not crossing date line */ Assert.assertTrue(associator.eventsAssociated(eventA, eventB)); // Create the third event ProductSummary eventSummaryC = new ProductSummary(); eventSummaryC.setId(new ProductId("us", "origin", "1234abcd")); eventSummaryC.setEventSource("us"); eventSummaryC.setEventSourceCode("1234abcd"); eventSummaryC.setEventLatitude(new BigDecimal("0.0")); eventSummaryC.setEventLongitude(new BigDecimal("179.85")); eventSummaryC.setEventTime(eventTime); Event eventC = new Event(); eventC.addProduct(eventSummaryC); // Create the fourth event ProductSummary eventSummaryD = new ProductSummary(); eventSummaryD.setId(new ProductId("ci", "origin", "5678efgh")); eventSummaryD.setEventSource("ci"); eventSummaryD.setEventSourceCode("5678efgh"); eventSummaryD.setEventLatitude(new BigDecimal("0.0")); eventSummaryD.setEventLongitude(new BigDecimal("-179.85")); eventSummaryD.setEventTime(eventTime); Event eventD = new Event(); eventD.addProduct(eventSummaryD); /* * Verify that events associate (0.3 degrees apart) when the points do * cross the date line */ Assert.assertTrue(associator.eventsAssociated(eventC, eventD)); // Create the fifth event ProductSummary eventSummaryE = new ProductSummary(); eventSummaryE.setId(new ProductId("us", "origin", "1234abcd")); eventSummaryE.setEventSource("us"); eventSummaryE.setEventSourceCode("1234abcd"); eventSummaryE.setEventLatitude(new BigDecimal("0.0")); eventSummaryE.setEventLongitude(new BigDecimal("-179.85")); eventSummaryE.setEventTime(eventTime); Event eventE = new Event(); eventE.addProduct(eventSummaryE); // Create the sixth event ProductSummary eventSummaryF = new ProductSummary(); eventSummaryF.setId(new ProductId("ci", "origin", "5678efgh")); eventSummaryF.setEventSource("ci"); eventSummaryF.setEventSourceCode("5678efgh"); eventSummaryF.setEventLatitude(new BigDecimal("0.0")); eventSummaryF.setEventLongitude(new BigDecimal("179.85")); eventSummaryF.setEventTime(eventTime); Event eventF = new Event(); eventF.addProduct(eventSummaryF); /* * Verify that events associate (0.3 degrees apart) when the points do * cross the date line, reversing the order. This checks that * jdbcProductIndex.normalizeLongitude is working in both directions * across the dateline. */ Assert.assertTrue(associator.eventsAssociated(eventE, eventF)); // Create the seventh event ProductSummary eventSummaryG = new ProductSummary(); eventSummaryG.setId(new ProductId("us", "origin", "1234abcd")); eventSummaryG.setEventSource("us"); eventSummaryG.setEventSourceCode("1234abcd"); eventSummaryG.setEventLatitude(new BigDecimal("0.0")); eventSummaryG.setEventLongitude(new BigDecimal("50")); eventSummaryG.setEventTime(eventTime); Event eventG = new Event(); eventG.addProduct(eventSummaryG); // Create the eighth event ProductSummary eventSummaryH = new ProductSummary(); eventSummaryH.setId(new ProductId("ci", "origin", "5678efgh")); eventSummaryH.setEventSource("ci"); eventSummaryH.setEventSourceCode("5678efgh"); eventSummaryH.setEventLatitude(new BigDecimal("0.0")); eventSummaryH.setEventLongitude(new BigDecimal("100")); eventSummaryH.setEventTime(eventTime); Event eventH = new Event(); eventH.addProduct(eventSummaryH); /* * Verify that events do NOT associate, when they are more than 100km * apart */ Assert.assertFalse(associator.eventsAssociated(eventG, eventH)); }