Ejemplo n.º 1
0
  public void attachFile(
      String space, String page, String name, InputStream is, boolean failIfExists)
      throws Exception {
    // make sure xwiki.Import exists
    if (!pageExists(space, page)) {
      createPage(space, page, null, null);
    }

    StringBuilder url = new StringBuilder(BASE_REST_URL);

    url.append("wikis/xwiki/spaces/");
    url.append(escapeURL(space));
    url.append("/pages/");
    url.append(escapeURL(page));
    url.append("/attachments/");
    url.append(escapeURL(name));

    if (failIfExists) {
      executePut(
          url.toString(), is, MediaType.APPLICATION_OCTET_STREAM, Status.CREATED.getStatusCode());
    } else {
      executePut(
          url.toString(),
          is,
          MediaType.APPLICATION_OCTET_STREAM,
          Status.CREATED.getStatusCode(),
          Status.ACCEPTED.getStatusCode());
    }
  }
Ejemplo n.º 2
0
  @Test
  public void shouldBeAbleToIndexValuesContainingSpaces() throws Exception {
    final long nodeId = helper.createNode();
    final String key = "key";
    final String value = "value with   spaces  in it";

    String indexName = "spacey-values";
    helper.createNodeIndex(indexName);
    final RestRequest request = RestRequest.req();
    JaxRsResponse response =
        request.post(
            functionalTestHelper.indexNodeUri(indexName), createJsonStringFor(nodeId, key, value));

    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
    URI location = response.getLocation();
    response.close();
    response =
        request.get(functionalTestHelper.indexNodeUri(indexName, key, URIHelper.encode(value)));
    assertEquals(Status.OK.getStatusCode(), response.getStatus());
    Collection<?> hits = (Collection<?>) JsonHelper.jsonToSingleValue(response.getEntity());
    assertEquals(1, hits.size());
    response.close();

    CLIENT.resource(location).delete();
    response =
        request.get(functionalTestHelper.indexNodeUri(indexName, key, URIHelper.encode(value)));
    hits = (Collection<?>) JsonHelper.jsonToSingleValue(response.getEntity());
    assertEquals(0, hits.size());
  }
Ejemplo n.º 3
0
  @Test
  public void shouldGetNodeRepresentationFromIndexUri() throws JsonParseException {
    long nodeId = helper.createNode();
    String key = "key2";
    String value = "value";

    String indexName = "mindex";
    helper.createNodeIndex(indexName);
    JaxRsResponse response =
        RestRequest.req()
            .post(
                functionalTestHelper.indexNodeUri(indexName),
                createJsonStringFor(nodeId, key, value));

    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
    String indexUri = response.getHeaders().getFirst("Location");

    response = RestRequest.req().get(indexUri);
    assertEquals(200, response.getStatus());

    String entity = response.getEntity();

    Map<String, Object> map = JsonHelper.jsonToMap(entity);
    assertNotNull(map.get("self"));
  }
Ejemplo n.º 4
0
 @Test
 public void testResponseHasBeenReceivedWhenAddingNewBook() {
   Response r =
       createWebClient("/rest/api/bookstore/books")
           .post(new Form().param("id", "1234").param("name", "Book 1234"));
   assertEquals(Status.CREATED.getStatusCode(), r.getStatus());
 }
  /**
   * Creates a new data set.
   *
   * @param providerId provider identifier (required)
   * @param dataSetId data set identifier (required)
   * @param description data set description (not required)
   * @return URI to created data set
   * @throws DataSetAlreadyExistsException when data set with given id (for given provider) already
   *     exists
   * @throws ProviderNotExistsException when provider with given id does not exist
   * @throws MCSException on unexpected situations
   */
  public URI createDataSet(String providerId, String dataSetId, String description)
      throws ProviderNotExistsException, DataSetAlreadyExistsException, MCSException {

    WebTarget target =
        client
            .target(this.baseUrl)
            .path(dataSetsPath)
            .resolveTemplate(ParamConstants.P_PROVIDER, providerId);

    Form form = new Form();
    form.param(ParamConstants.F_DATASET, dataSetId);
    form.param(ParamConstants.F_DESCRIPTION, description);

    Response response =
        target.request().post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));

    if (response.getStatus() == Status.CREATED.getStatusCode()) {
      return response.getLocation();
    }

    // TODO this does not function correctly,
    // details are filled with "MessageBodyReader not found for media type=text/html;
    // charset=utf-8, type=class eu.europeana.cloud.common.response.ErrorInfo,
    // genericType=class eu.europeana.cloud.common.response.ErrorInfo."
    // simple strings like 'adsfd' get entitised correctly
    ErrorInfo errorInfo = response.readEntity(ErrorInfo.class);
    throw MCSExceptionProvider.generateException(errorInfo);
  }
  @Test
  public void createInstance() {
    CassandraInstance instance = createMock(CassandraInstance.class);
    service.save(RING_NAME, instance);
    expect(instance.getId()).andReturn(CASSANDRA_ID);
    replayAll();

    Response response = resource.createInstance(instance);
    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
    assertEquals(
        String.valueOf(CASSANDRA_ID),
        response.getMetadata().getFirst(HttpHeaders.LOCATION).toString());
  }
  @Test
  @Ignore
  public void addPlantilla() {
    Plantilla plantillaPrecios = preparaPlantilla();

    ClientResponse response =
        resource.type("application/json").post(ClientResponse.class, plantillaPrecios);
    Assert.assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
    RestResponse restResponse = response.getEntity(new GenericType<RestResponse>() {});
    Assert.assertTrue(restResponse.getSuccess());
    Assert.assertNotNull(getFieldFromRestResponse(restResponse, "id"));
    Assert.assertEquals(
        plantillaPrecios.getNombre(), getFieldFromRestResponse(restResponse, "nombre"));
  }
  /* post(entity) /wishlists */
  @Test
  public void testPostWithWishlist() {
    Wishlist wishlist = new Wishlist();
    wishlist.setId(UUID.randomUUID().toString());
    wishlist.setOwner(TestConstants.CUSTOMER);
    instanceList.add(wishlist.getId());

    final Response response = createWishlist(wishlist);

    Assert.assertNotNull("Response must not be null", response);
    Assert.assertEquals(
        "Response does not have expected response code",
        Status.CREATED.getStatusCode(),
        response.getStatus());
  }
Ejemplo n.º 9
0
  @Test
  public void testAddAndQueryOneBook() {
    final String id = UUID.randomUUID().toString();

    Response r =
        createWebClient("/rest/api/bookstore/books")
            .post(new Form().param("id", id).param("name", "Book 1234"));
    assertEquals(Status.CREATED.getStatusCode(), r.getStatus());

    r = createWebClient("/rest/api/bookstore/books").path(id).get();
    assertEquals(Status.OK.getStatusCode(), r.getStatus());

    Book book = r.readEntity(Book.class);
    assertEquals(id, book.getId());
  }
Ejemplo n.º 10
0
  /** {@inheritDoc} */
  public <T> void createProperty(Property<T> value) {
    Util.assertNotNull(value);
    Util.assertHasLength(value.getName());
    if (existProperty(value.getName())) {
      throw new PropertyAlreadyExistException("Property already exist");
    }
    // Now can process upsert through PUT HTTP method
    Response cRes =
        getStore()
            .path(value.getName()) //
            .request(MediaType.APPLICATION_JSON)
            .put(Entity.entity(new PropertyApiBean(value), MediaType.APPLICATION_JSON));

    // Check response code CREATED or raised error
    if (Status.CREATED.getStatusCode() != cRes.getStatus()) {
      throw new FeatureAccessException(
          "Cannot create properties, an HTTP error " + cRes.getStatus() + " occured.");
    }
  }
Ejemplo n.º 11
0
  @Test
  public void should_create_attempt() {

    final String uri = String.format(URI, "rec", "123");

    Response response =
        invoke(uri).post(Entity.entity(new Form(), "application/vnd.resource+json; charset=utf-8"));

    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());

    JsonObject token1 = response.readEntity(JsonObject.class);
    String type = token1.getJsonObject("item").getString("type");
    String value = token1.getJsonObject("item").getString("key");
    Integer attempts = token1.getJsonObject("item").getInt("attempts");

    assertEquals("rec", type);
    assertEquals("123", value);
    assertEquals(1, attempts.intValue());
  }
  @Test
  @Ignore
  public void updatePlantillaAndRemoveNombre() {
    Plantilla plantilla = preparaPlantilla();
    ClientResponse response =
        resource.type("application/json").post(ClientResponse.class, plantilla);
    Assert.assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
    RestResponse restResponse = response.getEntity(new GenericType<RestResponse>() {});

    String id = getFieldFromRestResponse(restResponse, "id");
    Assert.assertNotNull(id);

    plantilla.setNombre("");
    response = resource.path(id).type("application/json").put(ClientResponse.class, plantilla);
    Assert.assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());

    ResultatOperacio parResponseMessage = response.getEntity(ResultatOperacio.class);
    Assert.assertEquals(
        CampoRequeridoException.REQUIRED_FIELD + "Nombre", parResponseMessage.getDescripcio());
  }
  @Test
  public void testPostGetDeleteMessage() {

    // test POST [app/messages]
    Response response =
        webTarget
            .request()
            .post(Entity.entity("Hello World !!!", MediaType.TEXT_PLAIN), Response.class);
    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());

    // test GET [app/messages/{number}]
    URI location = response.getLocation();
    String resourceNum = location.getPath().substring(location.getPath().lastIndexOf("/") + 1);
    Message message =
        webTarget.path(resourceNum).request(MediaType.APPLICATION_XML).get(Message.class);
    assertEquals("Hello World !!!", message.getMessage());

    // test DELETE [app/messages/{number}]
    response = webTarget.path(resourceNum).request().delete();
    assertNotSame(Status.NOT_FOUND.getStatusCode(), response.getStatus());
  }
Ejemplo n.º 14
0
 private void setHeaders(MultivaluedMap<String, Object> headers) {
   this.headers = headers;
   Object location = headers.getFirst(HttpHeaders.LOCATION);
   if (location != null) {
     if (location instanceof URI) {
       final URI locationUri = (URI) location;
       if (!locationUri.isAbsolute()) {
         final URI base =
             (statusType.getStatusCode() == Status.CREATED.getStatusCode())
                 ? request.getAbsolutePath()
                 : request.getBaseUri();
         location =
             UriBuilder.fromUri(base)
                 .path(locationUri.getRawPath())
                 .replaceQuery(locationUri.getRawQuery())
                 .fragment(locationUri.getRawFragment())
                 .build();
       }
       headers.putSingle(HttpHeaders.LOCATION, location);
     }
   }
 }
  @Test
  @Ignore
  public void updatePlantilla() {
    Plantilla plantilla = preparaPlantilla();
    ClientResponse response =
        resource.type("application/json").post(ClientResponse.class, plantilla);
    Assert.assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
    RestResponse restResponse = response.getEntity(new GenericType<RestResponse>() {});

    String id = getFieldFromRestResponse(restResponse, "id");
    Assert.assertNotNull(id);

    plantilla.setNombre("Prueba2");

    response =
        resource
            .path(String.valueOf(plantilla.getId()))
            .type("application/json")
            .put(ClientResponse.class, plantilla);
    Assert.assertEquals(Status.OK.getStatusCode(), response.getStatus());
    restResponse = response.getEntity(new GenericType<RestResponse>() {});

    Assert.assertEquals(plantilla.getNombre(), getFieldFromRestResponse(restResponse, "nombre"));
  }