|
| 1 | +package nu.ndw.nls.routingmapmatcher.graphhopper; |
| 2 | + |
| 3 | +import static nu.ndw.nls.routingmapmatcher.graphhopper.ev.EncodedTag.WAY_ID; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | +import static org.hamcrest.Matchers.is; |
| 6 | +import static org.junit.jupiter.api.Assertions.*; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import com.graphhopper.coll.GHLongLongBTree; |
| 11 | +import com.graphhopper.coll.LongLongMap; |
| 12 | +import com.graphhopper.routing.ev.EdgeIntAccess; |
| 13 | +import com.graphhopper.routing.ev.IntEncodedValue; |
| 14 | +import com.graphhopper.routing.util.EncodingManager; |
| 15 | +import com.graphhopper.routing.util.parsers.TagParser; |
| 16 | +import com.graphhopper.storage.BaseGraph; |
| 17 | +import com.graphhopper.storage.IntsRef; |
| 18 | +import com.graphhopper.storage.NodeAccess; |
| 19 | +import com.graphhopper.util.EdgeIteratorState; |
| 20 | +import com.graphhopper.util.PointList; |
| 21 | +import com.graphhopper.util.shapes.BBox; |
| 22 | +import java.util.List; |
| 23 | +import nu.ndw.nls.routingmapmatcher.domain.model.Link; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 27 | +import org.locationtech.jts.geom.Coordinate; |
| 28 | +import org.locationtech.jts.geom.LineString; |
| 29 | +import org.mockito.ArgumentCaptor; |
| 30 | +import org.mockito.Captor; |
| 31 | +import org.mockito.Mock; |
| 32 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 33 | + |
| 34 | +@ExtendWith(MockitoExtension.class) |
| 35 | +class ExpandedBoundsNetworkReaderTest extends NetworkReaderTest { |
| 36 | + |
| 37 | + private static final double LONG_1 = 5.358247; |
| 38 | + private static final double LAT_1 = 52.161257; |
| 39 | + private static final double LONG_2 = 5.379687; |
| 40 | + private static final double LAT_2 = 52.158304; |
| 41 | + private static final double LONG_3 = 5.379667; |
| 42 | + private static final double LAT_3 = 52.158280; |
| 43 | + |
| 44 | + // Link fields |
| 45 | + private static final long FROM_NODE_ID = 101; |
| 46 | + private static final long TO_NODE_ID = 102; |
| 47 | + private static final double DISTANCE = 12.15; |
| 48 | + private static final double SPEED = 50D; |
| 49 | + private static final long LINK_ID = 100; |
| 50 | + |
| 51 | + // Internal fields |
| 52 | + private static final int FROM_NODE_ID_INTERNAL = 0; |
| 53 | + private static final int TO_NODE_ID_INTERNAL = 1; |
| 54 | + private static final int EDGE_ID = 2; |
| 55 | + |
| 56 | + private static final Coordinate coordinateA1 = new Coordinate(LONG_1, LAT_1); |
| 57 | + private static final Coordinate coordinateA2 = new Coordinate(LONG_2, LAT_2); |
| 58 | + private static final Coordinate coordinateA3 = new Coordinate(LONG_3, LAT_3); |
| 59 | + |
| 60 | + @Captor |
| 61 | + private ArgumentCaptor<PointList> pointListArgumentCaptor; |
| 62 | + |
| 63 | + @Mock |
| 64 | + private BaseGraph baseGraph; |
| 65 | + @Mock |
| 66 | + private BBox bBox; |
| 67 | + @Mock |
| 68 | + private EncodingManager encodingManager; |
| 69 | + @Mock |
| 70 | + private Link link; |
| 71 | + @Mock |
| 72 | + private TagParser tagParser; |
| 73 | + |
| 74 | + private LongLongMap nodeIdToInternalNodeIdMap; |
| 75 | + |
| 76 | + @Mock |
| 77 | + private EdgeIntAccess edgeIntAccess; |
| 78 | + @Mock |
| 79 | + private IntEncodedValue idEncoder; |
| 80 | + @Mock |
| 81 | + private LineString lineString; |
| 82 | + @Mock |
| 83 | + private NodeAccess nodeAccess; |
| 84 | + @Mock |
| 85 | + private EdgeIteratorState edge; |
| 86 | + |
| 87 | + private ExpandedBoundsNetworkReader expandedBoundsNetworkReader; |
| 88 | + |
| 89 | + @BeforeEach |
| 90 | + void setUp() { |
| 91 | + // Use parameters from NetworkGraphHopper class |
| 92 | + nodeIdToInternalNodeIdMap = new GHLongLongBTree(200, 4, -1); |
| 93 | + when(baseGraph.createEdgeIntAccess()).thenReturn(edgeIntAccess); |
| 94 | + when(encodingManager.getIntEncodedValue(WAY_ID.getKey())).thenReturn(idEncoder); |
| 95 | + expandedBoundsNetworkReader = new ExpandedBoundsNetworkReader(baseGraph, encodingManager, |
| 96 | + List.of(link)::iterator, List.of(tagParser), nodeIdToInternalNodeIdMap); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void readGraph_ok() { |
| 101 | + when(link.getGeometry()).thenReturn(lineString); |
| 102 | + Coordinate[] coordinates = {coordinateA1, coordinateA2, coordinateA3}; |
| 103 | + when(lineString.getCoordinates()).thenReturn(coordinates); |
| 104 | + when(link.getFromNodeId()).thenReturn(FROM_NODE_ID); |
| 105 | + when(link.getToNodeId()).thenReturn(TO_NODE_ID); |
| 106 | + when(baseGraph.getNodeAccess()).thenReturn(nodeAccess); |
| 107 | + when(baseGraph.getBounds()).thenReturn(bBox); |
| 108 | + when(baseGraph.edge(FROM_NODE_ID_INTERNAL, TO_NODE_ID_INTERNAL)).thenReturn(edge); |
| 109 | + when(link.getDistanceInMeters()).thenReturn(DISTANCE); |
| 110 | + when(edge.setDistance(DISTANCE)).thenReturn(edge); |
| 111 | + when(edge.getEdge()).thenReturn(EDGE_ID); |
| 112 | + when(link.getSpeedInKilometersPerHour()).thenReturn(SPEED); |
| 113 | + when(link.getId()).thenReturn(LINK_ID); |
| 114 | + |
| 115 | + expandedBoundsNetworkReader.readGraph(); |
| 116 | + |
| 117 | + assertEquals(FROM_NODE_ID_INTERNAL, nodeIdToInternalNodeIdMap.get(FROM_NODE_ID)); |
| 118 | + assertEquals(TO_NODE_ID_INTERNAL, nodeIdToInternalNodeIdMap.get(TO_NODE_ID)); |
| 119 | + verify(nodeAccess).setNode(FROM_NODE_ID_INTERNAL, LAT_1, LONG_1); |
| 120 | + verify(nodeAccess).setNode(TO_NODE_ID_INTERNAL, LAT_3, LONG_3); |
| 121 | + verify(tagParser).handleWayTags(EDGE_ID, edgeIntAccess, link, IntsRef.EMPTY); |
| 122 | + verify(idEncoder).setInt(false, EDGE_ID, edgeIntAccess, (int) LINK_ID); |
| 123 | + verify(edge).setWayGeometry(pointListArgumentCaptor.capture()); |
| 124 | + verify(bBox).update(LAT_1 + .000001, LONG_1 + .000001); |
| 125 | + verify(bBox).update(LAT_1 - .000001, LONG_1 - .000001); |
| 126 | + verify(bBox).update(LAT_2 + .000001, LONG_2 + .000001); |
| 127 | + verify(bBox).update(LAT_2 - .000001, LONG_2 - .000001); |
| 128 | + verify(bBox).update(LAT_3 + .000001, LONG_3 + .000001); |
| 129 | + verify(bBox).update(LAT_3 - .000001, LONG_3 - .000001); |
| 130 | + PointList pointList = pointListArgumentCaptor.getValue(); |
| 131 | + assertThat(pointList.size(), is(1)); |
| 132 | + assertThat(pointList.getLon(0), is(LONG_2)); |
| 133 | + assertThat(pointList.getLat(0), is(LAT_2)); |
| 134 | + } |
| 135 | +} |
0 commit comments