PolygonDecoder implements LocationReferenceDecoder { @Override public RawLocationReference decode(String id, LocationReferenceData data) throws OpenLRProtoException { if (!data.hasPolygonLocationReference()) { throw new OpenLRProtoException(OpenLRProtoStatusCode.MISSING_LOCATION_REFERENCE); } PolygonLocationReference polygonLocationReference = data.getPolygonLocationReference(); if (polygonLocationReference.getCoordinatesCount() == 0) { throw new OpenLRProtoException(OpenLRProtoStatusCode.INVALID_LOCATION_REFERENCE); } List<GeoCoordinates> cornerPoints = new ArrayList<>(); for (Coordinates coordinates : polygonLocationReference.getCoordinatesList()) { GeoCoordinatesProtoImpl cornerPoint = new GeoCoordinatesProtoImpl(coordinates.getLongitude(), coordinates.getLatitude()); cornerPoints.add(cornerPoint); } return new RawPolygonLocRef(id, cornerPoints); }  @Override RawLocationReference decode(String id, LocationReferenceData data);  }
@Test public void testValidLocationReference() throws PhysicalFormatException { Coordinates first = Coordinates.newBuilder() .setLongitude(-122.915) .setLatitude(39.045) .build(); Coordinates second = Coordinates.newBuilder() .setLongitude(-122.914) .setLatitude(39.046) .build(); Coordinates third = Coordinates.newBuilder() .setLongitude(-122.913) .setLatitude(39.047) .build(); PolygonLocationReference polygonLocationReference = PolygonLocationReference.newBuilder() .addCoordinates(first) .addCoordinates(second) .addCoordinates(third) .build(); LocationReferenceData locationReferenceData = LocationReferenceData.newBuilder() .setPolygonLocationReference(polygonLocationReference) .build(); RawLocationReference rawLocationReference = polygonDecoder.decode("1", locationReferenceData); assertNotNull(rawLocationReference); assertEquals("1", rawLocationReference.getID()); assertEquals(LocationType.POLYGON, rawLocationReference.getLocationType()); List<GeoCoordinates> cornerPoints = rawLocationReference.getCornerPoints(); assertNotNull(cornerPoints); assertEquals(cornerPoints.size(), 3); GeoCoordinates firstCornerPoint = cornerPoints.get(0); assertNotNull(firstCornerPoint); assertEquals(firstCornerPoint.getLongitudeDeg(), -122.915); assertEquals(firstCornerPoint.getLatitudeDeg(), 39.045); GeoCoordinates secondCornerPoint = cornerPoints.get(1); assertNotNull(secondCornerPoint); assertEquals(secondCornerPoint.getLongitudeDeg(), -122.914); assertEquals(secondCornerPoint.getLatitudeDeg(), 39.046); GeoCoordinates thirdCornerPoint = cornerPoints.get(2); assertNotNull(thirdCornerPoint); assertEquals(thirdCornerPoint.getLongitudeDeg(), -122.913); assertEquals(thirdCornerPoint.getLatitudeDeg(), 39.047); }
@Test public void testMissingLocationReference() { LocationReferenceData locationReferenceData = LocationReferenceData.newBuilder() .build(); try { polygonDecoder.decode("1", locationReferenceData); fail(); } catch (PhysicalFormatException e) { assertEquals(e.getErrorCode(), OpenLRProtoStatusCode.MISSING_LOCATION_REFERENCE); } }
@Test public void testInvalidLocationReference() { PolygonLocationReference polygonLocationReference = PolygonLocationReference.newBuilder() .build(); LocationReferenceData locationReferenceData = LocationReferenceData.newBuilder() .setPolygonLocationReference(polygonLocationReference) .build(); try { polygonDecoder.decode("1", locationReferenceData); fail(); } catch (PhysicalFormatException e) { assertEquals(e.getErrorCode(), OpenLRProtoStatusCode.INVALID_LOCATION_REFERENCE); } }