Example #1
0
  private void batchCreate(
      final EntityTypeInvocationHandler handler,
      final ODataEntity entity,
      final ODataChangeset changeset) {

    LOG.debug("Create '{}'", handler);

    final ODataURIBuilder uriBuilder =
        new ODataURIBuilder(factory.getServiceRoot())
            .appendEntitySetSegment(handler.getEntitySetName());
    changeset.addRequest(ODataCUDRequestFactory.getEntityCreateRequest(uriBuilder.build(), entity));
  }
    public void geoSpacialTest(final ODataPubFormat format, final String contentType, final String prefer, final int id) {
        try {
            final ODataEntity entity =
                    ODataFactory.newEntity("Microsoft.Test.OData.Services.AstoriaDefaultService.AllSpatialTypes");
            entity.addProperty(ODataFactory.newPrimitiveProperty("Id",
                    new ODataPrimitiveValue.Builder().setText(String.valueOf(id)).setType(EdmSimpleType.Int32).build()));

            final Point point1 = new Point(Geospatial.Dimension.GEOGRAPHY);
            point1.setX(6.2);
            point1.setY(1.1);
            final Point point2 = new Point(Geospatial.Dimension.GEOGRAPHY);
            point2.setX(33.33);
            point2.setY(-2.5);

            // create a point
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeogPoint",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyPoint).
                    setValue(point1).build()));

            // create  multiple point
            final List<Point> points = new ArrayList<Point>();
            points.add(point1);
            points.add(point2);
            final MultiPoint multipoint = new MultiPoint(Geospatial.Dimension.GEOGRAPHY, points);
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeogMultiPoint",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyMultiPoint).
                    setValue(multipoint).build()));

            // create a line
            final List<Point> linePoints = new ArrayList<Point>();
            linePoints.add(point1);
            linePoints.add(point2);
            final LineString lineString = new LineString(Geospatial.Dimension.GEOGRAPHY, linePoints);

            entity.addProperty(ODataFactory.newPrimitiveProperty("GeogLine",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyLineString).
                    setValue(lineString).build()));

            // create a polygon
            linePoints.set(1, point2);
            linePoints.add(point2);
            linePoints.add(point1);
            final Polygon polygon = new Polygon(Geospatial.Dimension.GEOGRAPHY, linePoints, linePoints);
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeogPolygon",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyPolygon).
                    setValue(polygon).build()));

            // create a multi line string
            final List<LineString> multipleLines = new ArrayList<LineString>();
            multipleLines.add(lineString);
            multipleLines.add(lineString);
            final MultiLineString multiLine = new MultiLineString(Geospatial.Dimension.GEOGRAPHY, multipleLines);
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeogMultiLine",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyMultiLineString).
                    setValue(multiLine).build()));

            // create a multi polygon        
            final List<Polygon> polygons = new ArrayList<Polygon>();
            polygons.add(polygon);
            polygons.add(polygon);
            final MultiPolygon multiPolygon = new MultiPolygon(Geospatial.Dimension.GEOGRAPHY, polygons);
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeogMultiPolygon",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyMultiPolygon).
                    setValue(multiPolygon).build()));

            // create  acolletion of various shapes
            final List<Geospatial> geospatialCollection = new ArrayList<Geospatial>();
            geospatialCollection.add(point1);
            geospatialCollection.add(lineString);
            geospatialCollection.add(polygon);
            final GeospatialCollection collection = new GeospatialCollection(Geospatial.Dimension.GEOGRAPHY,
                    geospatialCollection);
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeogCollection",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyCollection).
                    setValue(collection).build()));

            // with geometry test

            final Point goemPoint1 = new Point(Geospatial.Dimension.GEOMETRY);
            goemPoint1.setX(6.2);
            goemPoint1.setY(1.1);
            final Point goemPoint2 = new Point(Geospatial.Dimension.GEOMETRY);
            goemPoint2.setX(33.33);
            goemPoint2.setY(-2.5);

            // create a point
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeomPoint",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryPoint).
                    setValue(goemPoint2).build()));

            // create  multiple point
            final List<Point> geomPoints = new ArrayList<Point>();
            geomPoints.add(point1);
            geomPoints.add(point2);
            final MultiPoint geomMultipoint = new MultiPoint(Geospatial.Dimension.GEOMETRY, geomPoints);
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeomMultiPoint",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryMultiPoint).
                    setValue(geomMultipoint).build()));

            // create a line
            final List<Point> geomLinePoints = new ArrayList<Point>();
            geomLinePoints.add(goemPoint1);
            geomLinePoints.add(goemPoint2);
            final LineString geomLineString = new LineString(Geospatial.Dimension.GEOMETRY, geomLinePoints);

            entity.addProperty(ODataFactory.newPrimitiveProperty("GeomLine",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryLineString).
                    setValue(geomLineString).build()));

            // create a polygon
            geomLinePoints.set(1, goemPoint2);
            geomLinePoints.add(goemPoint2);
            geomLinePoints.add(goemPoint1);
            final Polygon geomPolygon = new Polygon(Geospatial.Dimension.GEOMETRY, geomLinePoints, geomLinePoints);
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeomPolygon",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryPolygon).
                    setValue(geomPolygon).build()));

            // create a multi line string
            final List<LineString> geomMultipleLines = new ArrayList<LineString>();
            geomMultipleLines.add(geomLineString);
            geomMultipleLines.add(geomLineString);
            final MultiLineString geomMultiLine = new MultiLineString(Geospatial.Dimension.GEOMETRY, geomMultipleLines);
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeomMultiLine",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryMultiLineString).
                    setValue(geomMultiLine).build()));

            // create a multi polygon        
            final List<Polygon> geomPolygons = new ArrayList<Polygon>();
            geomPolygons.add(geomPolygon);
            geomPolygons.add(geomPolygon);
            final MultiPolygon geomMultiPolygon = new MultiPolygon(Geospatial.Dimension.GEOMETRY, geomPolygons);
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeomMultiPolygon",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyMultiPolygon).
                    setValue(geomMultiPolygon).build()));

            // create  a collection of various shapes
            final List<Geospatial> geomspatialCollection = new ArrayList<Geospatial>();
            geomspatialCollection.add(goemPoint1);
            geomspatialCollection.add(geomLineString);
            final GeospatialCollection geomCollection = new GeospatialCollection(Geospatial.Dimension.GEOMETRY,
                    geomspatialCollection);
            entity.addProperty(ODataFactory.newPrimitiveProperty("GeomCollection",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryCollection).
                    setValue(geomCollection).build()));

            // create request
            final ODataEntityCreateRequest createReq = ODataCUDRequestFactory.
                    getEntityCreateRequest(new ODataURIBuilder(testDefaultServiceRootURL).
                    appendEntityTypeSegment("AllGeoTypesSet").build(), entity);
            createReq.setFormat(format);
            createReq.setContentType(contentType);
            createReq.setPrefer(prefer);
            final ODataEntityCreateResponse createRes = createReq.execute();
            final ODataEntity entityAfterCreate = createRes.getBody();
            final ODataProperty geogCollection = entityAfterCreate.getProperty("GeogCollection");
            if (format.equals(ODataPubFormat.JSON) || format.equals(ODataPubFormat.JSON_NO_METADATA)) {
                assertTrue(geogCollection.hasComplexValue());
            } else {
                assertEquals(EdmSimpleType.GeographyCollection.toString(), geogCollection.getPrimitiveValue().
                        getTypeName());

                final ODataProperty geometryCollection = entityAfterCreate.getProperty("GeomCollection");
                assertEquals(EdmSimpleType.GeographyCollection.toString(),
                        geogCollection.getPrimitiveValue().getTypeName());

                int count = 0;
                for (Geospatial g : geogCollection.getPrimitiveValue().<GeospatialCollection>toCastValue()) {
                    assertNotNull(g);
                    count++;
                }
                assertEquals(3, count);
                count = 0;
                for (Geospatial g : geometryCollection.getPrimitiveValue().<GeospatialCollection>toCastValue()) {
                    assertNotNull(g);
                    count++;
                }
                assertEquals(2, count);
            }
            // update geog points 
            final Point updatePoint1 = new Point(Geospatial.Dimension.GEOGRAPHY);
            updatePoint1.setX(21.2);
            updatePoint1.setY(31.1);
            final Point updatePoint2 = new Point(Geospatial.Dimension.GEOGRAPHY);
            updatePoint2.setX(99.99);
            updatePoint2.setY(-3.24);
            ODataProperty property = entityAfterCreate.getProperty("GeogPoint");
            entityAfterCreate.removeProperty(property);
            entityAfterCreate.addProperty(ODataFactory.newPrimitiveProperty("GeogPoint",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyPoint).
                    setValue(updatePoint1).build()));
            updateGeog(format, contentType, prefer, entityAfterCreate, UpdateType.REPLACE, entityAfterCreate.getETag());

            // update geography line  
            final List<Point> updateLinePoints = new ArrayList<Point>();
            updateLinePoints.add(updatePoint1);
            updateLinePoints.add(updatePoint2);
            final LineString updateLineString = new LineString(Geospatial.Dimension.GEOGRAPHY, updateLinePoints);
            ODataProperty lineProperty = entityAfterCreate.getProperty("GeogLine");
            entityAfterCreate.removeProperty(lineProperty);
            entityAfterCreate.addProperty(ODataFactory.newPrimitiveProperty("GeogLine",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyLineString).
                    setValue(updateLineString).build()));
            //updateGeog(format,contentType, prefer, entityAfterCreate, UpdateType.REPLACE,entityAfterCreate.getETag());

            // update a geography polygon
            updateLinePoints.set(1, updatePoint2);
            updateLinePoints.add(updatePoint2);
            updateLinePoints.add(updatePoint1);
            final Polygon updatePolygon =
                    new Polygon(Geospatial.Dimension.GEOGRAPHY, updateLinePoints, updateLinePoints);
            ODataProperty polygonProperty = entityAfterCreate.getProperty("GeogPolygon");
            entityAfterCreate.removeProperty(polygonProperty);
            entityAfterCreate.addProperty(ODataFactory.newPrimitiveProperty("GeogPolygon",
                    new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyPolygon).
                    setValue(updatePolygon).build()));
            //updateGeog(format,contentType, prefer, entityAfterCreate, UpdateType.REPLACE,entityAfterCreate.getETag());

            // delete the entity
            ODataURIBuilder deleteUriBuilder = new ODataURIBuilder(testDefaultServiceRootURL).
                    appendEntityTypeSegment("AllGeoTypesSet(" + id + ")");
            ODataDeleteRequest deleteReq = ODataCUDRequestFactory.getDeleteRequest(deleteUriBuilder.build());
            deleteReq.setFormat(format);
            deleteReq.setContentType(contentType);
            assertEquals(204, deleteReq.execute().getStatusCode());
        } catch (Exception e) {
            LOG.error("", e);
            if (format.equals(ODataPubFormat.JSON) || format.equals(ODataPubFormat.JSON_NO_METADATA)) {
                assertTrue(true);
            } else {
                fail(e.getMessage());
            }
        } catch (AssertionError e) {
            LOG.error("", e);
            fail(e.getMessage());
        }
    }