void _test(String type, String value) throws Exception {
   app.apply(Requests.from("/" + type + "/" + value, "GET").build())
       .get()
       .readEntity(String.class);
   app.apply(Requests.from("/" + type + "/wrapper/" + value, "GET").build())
       .get()
       .readEntity(String.class);
 }
 @Test
 public void testLazyConverter() throws Exception {
   final ApplicationHandler application =
       new ApplicationHandler(new ResourceConfig(MyLazyParamProvider.class, Resource.class));
   final ContainerResponse response =
       application.apply(RequestContextBuilder.from("/resource", "GET").build()).get();
   assertEquals(400, response.getStatus());
 }
  @Test
  public void testappBuilderClasses() throws InterruptedException, ExecutionException {
    final ResourceConfig resourceConfig = new ResourceConfig(ResourceA.class);
    final ApplicationHandler application = new ApplicationHandler(resourceConfig);

    ContainerRequest req = RequestContextBuilder.from(BASE_URI, BASE_URI, "GET").build();

    assertEquals("get!", application.apply(req).get().getEntity());
  }
  @Test
  public void testEmptyAppCreationPasses() throws InterruptedException, ExecutionException {
    final ResourceConfig resourceConfig = new ResourceConfig();
    final ApplicationHandler application = new ApplicationHandler(resourceConfig);

    ContainerRequest req = RequestContextBuilder.from(BASE_URI, BASE_URI, "GET").build();

    assertEquals(404, application.apply(req).get().getStatus());
  }
  @Test
  public void testSubResourceMethodWithSameTemplate() throws Exception {
    app = createApplication(SubResourceWithSameTemplate.class);

    assertEquals(
        "FOO", app.apply(RequestContextBuilder.from("/foo", "GET").build()).get().getEntity());
    assertEquals(
        "BAR", app.apply(RequestContextBuilder.from("/foo/bar", "GET").build()).get().getEntity());
  }
 @Test
 public void testClientAndServerProvider2() throws ExecutionException, InterruptedException {
   final ResourceConfig resourceConfig =
       new ResourceConfig(Resource.class, MyServerAndClientContrainedToClientFilter.class);
   ApplicationHandler handler = new ApplicationHandler(resourceConfig);
   final ContainerResponse response =
       handler.apply(RequestContextBuilder.from("/resource", "GET").build()).get();
   Assert.assertEquals(200, response.getStatus());
 }
  @Test
  public void testDuplicate() throws Exception {
    initiateWebApplication(Duplicate.class);

    assertEquals(
        "foo", app.apply(RequestContextBuilder.from("/foo", "GET").build()).get().getEntity());
    assertEquals(
        "bar", app.apply(RequestContextBuilder.from("/foo/bar", "GET").build()).get().getEntity());
  }
  @Test
  public void testPathSegsEndList() throws Exception {
    initiateWebApplication(PathSegsEndList.class);

    assertEquals(
        "xyz", app.apply(RequestContextBuilder.from("/x/y/z", "GET").build()).get().getEntity());
    assertEquals(
        "xyz/", app.apply(RequestContextBuilder.from("/x/y/z/", "GET").build()).get().getEntity());
  }
  @Test
  public void testSubResourceMethodsWithDifferentTemplates() throws Exception {
    app = createApplication(SubResourceMethodsWithDifferentTemplates.class);

    assertEquals(
        "foo", app.apply(RequestContextBuilder.from("/foo", "GET").build()).get().getEntity());
    assertEquals(
        "bar", app.apply(RequestContextBuilder.from("/bar", "POST").build()).get().getEntity());
  }
  @Test
  public void testSubResourceCapturingGroups() throws Exception {
    app = createApplication(SubResourceExplicitRegexCapturingGroups.class);

    assertEquals(
        "123", app.apply(RequestContextBuilder.from("/123", "GET").build()).get().getEntity());
    assertEquals(
        "123-456-789",
        app.apply(RequestContextBuilder.from("/123-456-789", "GET").build()).get().getEntity());
  }
  @Test
  public void testSubResourceXXX() throws Exception {
    app = createApplication(SubResourceXXX.class);

    assertEquals(
        "123",
        app.apply(RequestContextBuilder.from("/123/literal", "GET").build()).get().getEntity());
    assertEquals(
        "123literal789",
        app.apply(RequestContextBuilder.from("/123/literal/789", "GET").build()).get().getEntity());
  }
  @Test
  public void testSubResourceNoSlashMethodWithLimitedTemplate() throws Exception {
    app = createApplication(SubResourceNoSlashMethodWithLimitedTemplate.class);

    assertEquals(
        "topone",
        app.apply(RequestContextBuilder.from("/top?id=one", "GET").build()).get().getEntity());
    assertEquals(
        "a/b/c/d",
        app.apply(RequestContextBuilder.from("/top/a/b/c/d", "GET").build()).get().getEntity());
  }
  @Test
  public void testPathSeg() throws Exception {
    initiateWebApplication(PathSeg.class);

    assertEquals(
        "content",
        app.apply(RequestContextBuilder.from("/a-b/c-d", "GET").build()).get().getEntity());
    assertEquals(
        "sub-content",
        app.apply(RequestContextBuilder.from("/a-b/c-d/e-f", "GET").build()).get().getEntity());
  }
  @Test
  public void testSubResourceMethods() throws Exception {
    app = createApplication(SubResourceMethods.class);

    assertEquals("/", app.apply(RequestContextBuilder.from("/", "GET").build()).get().getEntity());
    assertEquals(
        "/sub", app.apply(RequestContextBuilder.from("/sub", "GET").build()).get().getEntity());
    assertEquals(
        "/sub/sub",
        app.apply(RequestContextBuilder.from("/sub/sub", "GET").build()).get().getEntity());
  }
 @Test
 public void testInvalidSubResource() throws ExecutionException, InterruptedException {
   ApplicationHandler handler = new ApplicationHandler(new ResourceConfig(WrongResource.class));
   _test(handler, "/wrong", "ok");
   try {
     final ContainerResponse response =
         handler.apply(RequestContextBuilder.from("/wrong/locator", "GET").build()).get();
     assertEquals(500, response.getStatus());
     fail("Should throw exception caused by validation errors of Sub Resource.");
   } catch (Throwable e) {
     // ok - Should throw exception caused by validation errors of Sub Resource.
   }
 }
 @Test
 public void testResourceAndProviderConstrainedToServer()
     throws ExecutionException, InterruptedException {
   final ResourceConfig resourceConfig =
       new ResourceConfig(ResourceAndProviderConstrainedToServer.class);
   ApplicationHandler handler = new ApplicationHandler(resourceConfig);
   final ContainerResponse response =
       handler
           .apply(RequestContextBuilder.from("/resource-and-provider-server", "GET").build())
           .get();
   Assert.assertEquals(200, response.getStatus());
   Assert.assertEquals(
       "called", response.getHeaderString("ResourceAndProviderConstrainedToServer"));
 }
Example #17
0
    @Test
    public void testEmptyProduces() throws Exception {
      final ResourceConfig rc = new ResourceConfig(EmptyProducesTestResource.class);
      rc.property(ServerProperties.WADL_FEATURE_DISABLE, false);

      final ApplicationHandler applicationHandler = new ApplicationHandler(rc);

      final ContainerResponse containerResponse =
          applicationHandler
              .apply(
                  new ContainerRequest(
                      URI.create("/"),
                      URI.create("/application.wadl"),
                      "GET",
                      null,
                      new MapPropertiesDelegate()))
              .get();

      assertEquals(200, containerResponse.getStatus());

      ((ByteArrayInputStream) containerResponse.getEntity()).reset();

      final DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
      bf.setNamespaceAware(true);
      bf.setValidating(false);
      if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
      }
      final DocumentBuilder b = bf.newDocumentBuilder();
      final Document d = b.parse((InputStream) containerResponse.getEntity());
      printSource(new DOMSource(d));
      final XPath xp = XPathFactory.newInstance().newXPath();
      xp.setNamespaceContext(
          new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));

      final NodeList responseElements =
          (NodeList)
              xp.evaluate(
                  "/wadl:application/wadl:resources[@path!='application.wadl']//wadl:method/wadl:response",
                  d,
                  XPathConstants.NODESET);

      for (int i = 0; i < responseElements.getLength(); i++) {
        final Node item = responseElements.item(i);

        assertEquals(0, item.getChildNodes().getLength());
        assertEquals(0, item.getAttributes().getLength());
      }
    }
  @Test
  public void testConsumeWildCardBean() throws Exception {
    ApplicationHandler app = createApplication(ConsumeWildCardBean.class);

    assertEquals(
        "HTML",
        app.apply(RequestContextBuilder.from("/a/b", "POST").entity("").type("text/html").build())
            .get()
            .getEntity());
    assertEquals(
        "XHTML",
        app.apply(RequestContextBuilder.from("/a/b", "POST").entity("").type("text/xhtml").build())
            .get()
            .getEntity());
  }
  @Test
  public void testPathSegsList() throws Exception {
    initiateWebApplication(PathSegsList.class);

    assertEquals(
        "xyz-b",
        app.apply(RequestContextBuilder.from("/x/y/z/edit/b", "GET").build()).get().getEntity());
    assertEquals(
        "//xyz-b",
        app.apply(
                RequestContextBuilder.from(
                        "http://localhost/", "http://localhost///x/y/z/edit/b", "GET")
                    .build())
            .get()
            .getEntity());
  }
 private void _test(ApplicationHandler handler, String requestUri, String expected)
     throws InterruptedException, ExecutionException {
   final ContainerResponse response =
       handler.apply(RequestContextBuilder.from(requestUri, "GET").build()).get();
   assertEquals(200, response.getStatus());
   assertEquals(expected, response.getEntity());
 }
 @Test
 public void testStringArgsGet() throws Exception {
   initiateWebApplication(Resource.class);
   assertEquals(
       "content",
       app.apply(RequestContextBuilder.from("/a/b/c", "GET").build()).get().getEntity());
 }
 @Test
 public void testFiltersAnnotated() throws ExecutionException, InterruptedException {
   final ResourceConfig resourceConfig =
       new ResourceConfig(
           MyServerFilter.class,
           MyClientFilter.class,
           MyServerWrongFilter.class,
           MyServerFilterWithoutConstraint.class,
           Resource.class);
   resourceConfig.registerInstances(new MyServerWrongFilter2(), new MyServerFilter2());
   ApplicationHandler handler = new ApplicationHandler(resourceConfig);
   final ContainerResponse response =
       handler.apply(RequestContextBuilder.from("/resource", "GET").build()).get();
   Assert.assertEquals("called", response.getHeaderString("MyServerFilter"));
   Assert.assertEquals("called", response.getHeaderString("MyServerFilter2"));
   Assert.assertEquals("called", response.getHeaderString("MyServerFilterWithoutConstraint"));
 }
  @Test
  public void testSubResources() throws Exception {
    initiateWebApplication(Root.class);

    assertEquals(
        "1234567",
        app.apply(RequestContextBuilder.from("/1/2/3/4/5/6/7", "GET").build()).get().getEntity());
  }
  @Test
  public void testGetResource() throws Exception {
    ApplicationHandler app = createApplication(ResourceA.class, ResourceB.class);

    assertEquals(
        "B: c", app.apply(RequestContextBuilder.from("/a/b/c", "GET").build()).get().getEntity());
    assertEquals(
        "SR: foo",
        app.apply(RequestContextBuilder.from("/a/foo", "GET").build()).get().getEntity());
    assertEquals(
        "null",
        app.apply(RequestContextBuilder.from("/a/is-null", "GET").build()).get().getEntity());
    assertEquals(
        404,
        app.apply(RequestContextBuilder.from("/a/non-instantiable", "GET").build())
            .get()
            .getStatus());
  }
  @Test
  public void testSubResource() throws Exception {
    app = createApplication(SubResourceExplicitRegex.class);

    assertEquals(
        "segments: foo",
        app.apply(RequestContextBuilder.from("/foo", "GET").build()).get().getEntity());
    assertEquals(
        "segments: foo/bar",
        app.apply(RequestContextBuilder.from("/foo/bar", "GET").build()).get().getEntity());

    assertEquals(
        "digit: 123",
        app.apply(RequestContextBuilder.from("/digit/123", "GET").build()).get().getEntity());
    assertEquals(
        "anything: foo",
        app.apply(RequestContextBuilder.from("/digit/foo", "GET").build()).get().getEntity());
  }
 @Test
 public void testSpecificApplication() throws ExecutionException, InterruptedException {
   Application app =
       new Application() {
         @Override
         public Set<Class<?>> getClasses() {
           final HashSet<Class<?>> classes = Sets.newHashSet();
           classes.add(Resource.class);
           classes.add(MyClientFilter.class);
           classes.add(MyServerWrongFilter.class);
           return classes;
         }
       };
   ApplicationHandler handler = new ApplicationHandler(app);
   final ContainerResponse response =
       handler.apply(RequestContextBuilder.from("/resource", "GET").build()).get();
   Assert.assertEquals(200, response.getStatus());
 }
Example #27
0
    @Test
    public void testEnableWadl() throws ExecutionException, InterruptedException {
      final ResourceConfig rc = new ResourceConfig(WidgetsResource.class, ExtraResource.class);
      rc.property(ServerProperties.WADL_FEATURE_DISABLE, false);

      final ApplicationHandler applicationHandler = new ApplicationHandler(rc);

      final ContainerResponse containerResponse =
          applicationHandler
              .apply(
                  new ContainerRequest(
                      URI.create("/"),
                      URI.create("/application.wadl"),
                      "GET",
                      null,
                      new MapPropertiesDelegate()))
              .get();

      assertEquals(200, containerResponse.getStatus());
    }
 public String test(HttpMethodOverrideFilter.Source... sources) {
   ResourceConfig rc = new ResourceConfig(Resource.class);
   HttpMethodOverrideFilter.enableFor(rc, sources);
   ApplicationHandler handler = new ApplicationHandler(rc);
   try {
     ContainerResponse response =
         handler
             .apply(
                 RequestContextBuilder.from("", "/?_method=DELETE", "POST")
                     .header("X-HTTP-Method-Override", "PUT")
                     .build())
             .get();
     if (Response.Status.OK.equals(response.getStatusInfo())) {
       return (String) response.getEntity();
     } else {
       return "" + response.getStatus();
     }
   } catch (Exception e) {
     e.printStackTrace();
     return e.getMessage();
   }
 }
Example #29
0
    /**
     * Test overriding WADL's /application/resources/@base attribute.
     *
     * @throws Exception in case of unexpected test failure.
     */
    @Test
    public void testCustomWadlResourcesBaseUri() throws Exception {
      final ResourceConfig rc = new ResourceConfig(WidgetsResource.class, ExtraResource.class);
      rc.property(ServerProperties.WADL_GENERATOR_CONFIG, MyWadlGeneratorConfig.class.getName());

      final ApplicationHandler applicationHandler = new ApplicationHandler(rc);

      final ContainerResponse containerResponse =
          applicationHandler
              .apply(
                  new ContainerRequest(
                      URI.create("/"),
                      URI.create("/application.wadl"),
                      "GET",
                      null,
                      new MapPropertiesDelegate()))
              .get();

      final DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
      bf.setNamespaceAware(true);
      bf.setValidating(false);
      if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
      }
      final DocumentBuilder b = bf.newDocumentBuilder();

      ((ByteArrayInputStream) containerResponse.getEntity()).reset();

      final Document d = b.parse((InputStream) containerResponse.getEntity());
      printSource(new DOMSource(d));
      final XPath xp = XPathFactory.newInstance().newXPath();
      xp.setNamespaceContext(
          new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));
      // check base URI
      final String val =
          (String) xp.evaluate("/wadl:application/wadl:resources/@base", d, XPathConstants.STRING);
      assertEquals(val, MyWadlGeneratorConfig.MyWadlGenerator.CUSTOM_RESOURCES_BASE_URI);
    }
  @Test
  public void testSubResourceMethodsWithTemplates() throws Exception {
    app = createApplication(SubResourceMethodsWithTemplates.class);

    assertEquals("/", app.apply(RequestContextBuilder.from("/", "GET").build()).get().getEntity());

    assertEquals(
        "value",
        app.apply(RequestContextBuilder.from("/subvalue", "GET").build()).get().getEntity());
    assertEquals(
        "a", app.apply(RequestContextBuilder.from("/sub/a", "GET").build()).get().getEntity());

    assertEquals(
        "value/a",
        app.apply(RequestContextBuilder.from("/subunlimitedvalue/a", "GET").build())
            .get()
            .getEntity());
    assertEquals(
        "a/b/c/d",
        app.apply(RequestContextBuilder.from("/subunlimited/a/b/c/d", "GET").build())
            .get()
            .getEntity());
  }