public void createEntity(
      final String serviceRootURL,
      final ODataPubFormat format,
      final ODataEntity original,
      final String entitySetName,
      final String contentType,
      final String prefer) {

    final URIBuilder uriBuilder = client.getURIBuilder(serviceRootURL);
    uriBuilder.appendEntitySetSegment(entitySetName);
    final ODataEntityCreateRequest createReq =
        client.getCUDRequestFactory().getEntityCreateRequest(uriBuilder.build(), original);
    createReq.setFormat(format);
    createReq.setContentType(contentType);
    createReq.setPrefer(prefer);

    final ODataEntityCreateResponse createRes = createReq.execute();

    if (prefer.equals("return-no-content")) {
      assertEquals(204, createRes.getStatusCode());
    } else {
      assertEquals(201, createRes.getStatusCode());
      assertEquals("Created", createRes.getStatusMessage());
      assertTrue(createRes.getHeader("DataServiceVersion").contains("3.0;"));
      final ODataEntity created = createRes.getBody();
      assertNotNull(created);
    }
  }
  public void getOpenTypeEntityWithString(
      final ODataPubFormat format,
      final String contentType,
      final int id,
      final String prefer,
      String uuid) {
    try {
      ODataEntity entity =
          ODataObjectFactory.newEntity("Microsoft.Test.OData.Services.OpenTypesService.Row");
      entity.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "Id",
              client
                  .getPrimitiveValueBuilder()
                  .setType(EdmSimpleType.Guid)
                  .setValue(uuid)
                  .build()));
      entity.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "LongValue",
              client
                  .getPrimitiveValueBuilder()
                  .setType(EdmSimpleType.Int64)
                  .setValue(44L)
                  .build()));
      entity.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "DoubleValue",
              client
                  .getPrimitiveValueBuilder()
                  .setType(EdmSimpleType.Double)
                  .setValue(4.34567D)
                  .build()));

      final ODataEntityCreateRequest createReq =
          client
              .getCUDRequestFactory()
              .getEntityCreateRequest(
                  client
                      .getURIBuilder(testOpenTypeServiceRootURL)
                      .appendEntityTypeSegment("Row")
                      .build(),
                  entity);
      createReq.setFormat(format);
      createReq.setPrefer(prefer);
      createReq.setContentType(contentType);
      final ODataEntityCreateResponse createRes = createReq.execute();
    } catch (Exception e) {
      if (e.getMessage().equals("Provided value is not compatible with Edm.Guid")) {
        assertTrue(true);
      } else {
        fail(e.getMessage());
      }
    }
  }
コード例 #3
0
    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());
        }
    }
  public void getOpenTypeEntity(
      final ODataPubFormat format,
      final String contentType,
      final int id,
      final String prefer,
      String uuid) {
    final UUID guid;
    if (uuid.equals("random")) {
      guid = UUID.randomUUID();
    } else {
      guid = UUID.fromString(uuid);
    }
    ODataEntity entity =
        ODataObjectFactory.newEntity("Microsoft.Test.OData.Services.OpenTypesService.Row");
    entity.addProperty(
        ODataObjectFactory.newPrimitiveProperty(
            "Id",
            client.getPrimitiveValueBuilder().setType(EdmSimpleType.Guid).setValue(guid).build()));
    entity.addProperty(
        ODataObjectFactory.newPrimitiveProperty(
            "LongValue",
            client.getPrimitiveValueBuilder().setType(EdmSimpleType.Int64).setValue(44L).build()));
    entity.addProperty(
        ODataObjectFactory.newPrimitiveProperty(
            "DoubleValue",
            client
                .getPrimitiveValueBuilder()
                .setType(EdmSimpleType.Double)
                .setValue(4.34567D)
                .build()));

    final ODataEntityCreateRequest createReq =
        client
            .getCUDRequestFactory()
            .getEntityCreateRequest(
                client
                    .getURIBuilder(testOpenTypeServiceRootURL)
                    .appendEntityTypeSegment("Row")
                    .build(),
                entity);
    createReq.setFormat(format);
    createReq.setPrefer(prefer);
    createReq.setContentType(contentType);
    final ODataEntityCreateResponse createRes = createReq.execute();
    assertEquals(201, createRes.getStatusCode());
    entity = createRes.getBody();
    assertNotNull(entity);
    if (format.equals(ODataPubFormat.JSON_NO_METADATA)) {
      assertEquals(
          EdmSimpleType.String.toString(),
          entity.getProperty("Id").getPrimitiveValue().getTypeName());
      assertEquals(
          EdmSimpleType.String.toString(),
          entity.getProperty("LongValue").getPrimitiveValue().getTypeName());
      assertEquals(
          EdmSimpleType.Double.toString(),
          entity.getProperty("DoubleValue").getPrimitiveValue().getTypeName());
    } else if (format.equals(ODataPubFormat.JSON)) {
      assertEquals(
          EdmSimpleType.String.toString(),
          entity.getProperty("Id").getPrimitiveValue().getTypeName());
      assertEquals(
          EdmSimpleType.Int64.toString(),
          entity.getProperty("LongValue").getPrimitiveValue().getTypeName());
      assertEquals(
          EdmSimpleType.Double.toString(),
          entity.getProperty("DoubleValue").getPrimitiveValue().getTypeName());
    } else {
      assertEquals(
          EdmSimpleType.Guid.toString(),
          entity.getProperty("Id").getPrimitiveValue().getTypeName());
      assertEquals(
          EdmSimpleType.Int64.toString(),
          entity.getProperty("LongValue").getPrimitiveValue().getTypeName());
      assertEquals(
          EdmSimpleType.Double.toString(),
          entity.getProperty("DoubleValue").getPrimitiveValue().getTypeName());
    }
    ODataDeleteResponse deleteRes;
    try {
      deleteRes =
          client
              .getCUDRequestFactory()
              .getDeleteRequest(new URI(testOpenTypeServiceRootURL + "/Row(guid'" + guid + "')"))
              .execute();
      assertEquals(204, deleteRes.getStatusCode());
    } catch (URISyntaxException e) {
      // TODO Auto-generated catch block
      LOG.error("", e);
    }
  }
  @Test
  public void createNoMultipleKey() {
    final ODataPubFormat format = ODataPubFormat.JSON_FULL_METADATA;
    final String contentType = "application/json;odata=fullmetadata";
    final String prefer = "return-content";
    try {
      final ODataEntity message =
          ODataObjectFactory.newEntity(
              "Microsoft.Test.OData.Services.AstoriaDefaultService.Message");

      message.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "MessageId",
              client
                  .getPrimitiveValueBuilder()
                  .setText(String.valueOf(25))
                  .setType(EdmSimpleType.Int32)
                  .build()));
      message.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "FromUsername",
              client
                  .getPrimitiveValueBuilder()
                  .setText("user")
                  .setType(EdmSimpleType.String)
                  .build()));
      message.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "ToUsername",
              client
                  .getPrimitiveValueBuilder()
                  .setValue("usernameabc")
                  .setType(EdmSimpleType.String)
                  .build()));
      message.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "Subject",
              client
                  .getPrimitiveValueBuilder()
                  .setValue("Subject of message")
                  .setType(EdmSimpleType.String)
                  .build()));
      message.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "Body",
              client
                  .getPrimitiveValueBuilder()
                  .setValue("Body Content")
                  .setType(EdmSimpleType.String)
                  .build()));
      message.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "IsRead",
              client
                  .getPrimitiveValueBuilder()
                  .setValue(false)
                  .setType(EdmSimpleType.Boolean)
                  .build()));

      final URIBuilder builder =
          client.getURIBuilder(testDefaultServiceRootURL).appendEntitySetSegment("Message");
      final ODataEntityCreateRequest req =
          client.getCUDRequestFactory().getEntityCreateRequest(builder.build(), message);
      req.setFormat(format);
      req.setContentType(contentType);
      req.setPrefer(prefer);
      final ODataEntityCreateResponse res = req.execute();
      assertNotNull(res);
      assertEquals(201, res.getStatusCode());
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }