@Before
 public void startServer() {
   server =
       new TJWSEmbeddedSpringMVCServer(
           "classpath:spring-typemapping-test-server.xml", TestPortProvider.getPort());
   server.start();
 }
Beispiel #2
0
 @Test
 public void testSubresourceProxy() throws Exception {
   String url = TestPortProvider.generateURL("/foobar");
   InterfaceA a = ProxyFactory.create(InterfaceA.class, url);
   assertEquals("FOO", a.getFoo());
   InterfaceB b = ((ResteasyClientProxy) a).as(InterfaceB.class);
   assertEquals("BAR", b.getBar());
 }
Beispiel #3
0
 @Test
 public void testMe() throws Exception {
   MyTest proxy = ProxyFactory.create(MyTest.class, TestPortProvider.generateURL(""));
   try {
     proxy.postIt("hello");
   } catch (ClientResponseFailure e) {
     Assert.assertEquals(401, e.getResponse().getStatus());
   }
 }
/** @author Attila Kiraly */
public class InputPartDefaultContentTypeEncodingOverwriteTest extends BaseResourceTest {
  protected static final String TEXT_PLAIN_WITH_CHARSET_UTF_8 = "text/plain; charset=utf-8";

  @Path("/mime")
  public static class MyService {

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public String sendDefaultContentType(MultipartInput input) {
      return input.getParts().get(0).getMediaType().toString();
    }
  }

  @Provider
  @ServerInterceptor
  public static class ContentTypeSetterPreProcessorInterceptor implements PreProcessInterceptor {

    public ServerResponse preProcess(HttpRequest request, ResourceMethodInvoker method)
        throws Failure, WebApplicationException {
      request.setAttribute(InputPart.DEFAULT_CONTENT_TYPE_PROPERTY, TEXT_PLAIN_WITH_CHARSET_UTF_8);
      return null;
    }
  }

  @Before
  public void setUp() throws Exception {
    dispatcher.getRegistry().addPerRequestResource(MyService.class);
    dispatcher
        .getProviderFactory()
        .registerProvider(ContentTypeSetterPreProcessorInterceptor.class, false);
  }

  private static final String TEST_URI = TestPortProvider.generateURL("");

  @Test
  public void testContentType() throws Exception {
    String message =
        "--boo\r\n"
            + "Content-Disposition: form-data; name=\"foo\"\r\n"
            + "Content-Transfer-Encoding: 8bit\r\n\r\n"
            + "bar\r\n"
            + "--boo--\r\n";

    ClientRequest request = new ClientRequest(TEST_URI + "/mime");
    request.body("multipart/form-data; boundary=boo", message.getBytes());
    ClientResponse<String> response = request.post(String.class);
    Assert.assertEquals("Status code is wrong.", 20, response.getStatus() / 10);
    Assert.assertEquals(
        "Response text is wrong",
        MediaType.valueOf(TEXT_PLAIN_WITH_CHARSET_UTF_8),
        MediaType.valueOf(response.getEntity()));
  }
}
 @Test
 public void testMe() throws Exception {
   ResteasyClient client = new ResteasyClient();
   MyTest proxy = client.target(TestPortProvider.generateURL("")).proxy(MyTest.class);
   try {
     proxy.postIt("hello");
     Assert.fail();
   } catch (NotAuthorizedException e) {
     Assert.assertEquals(401, e.getResponse().getStatus());
   }
   client.close();
 }
  @Test
  public void testNewSubNotBlockedByTimeoutTask() throws Exception {
    // Default config is 1s interval, 300s timeout.

    // Create a topic
    String testName = "AutoAckTopicTest.testNewSubNotBlockedByTimeoutTask";
    TopicDeployment deployment = new TopicDeployment();
    deployment.setDuplicatesAllowed(true);
    deployment.setDurableSend(false);
    deployment.setName(testName);
    manager.getTopicManager().deploy(deployment);

    // Create a consumer
    ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/topics/" + testName));
    ClientResponse<?> response = request.head();
    response.releaseConnection();
    Assert.assertEquals(200, response.getStatus());
    Link sender = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), response, "create");
    Link subscriptions =
        getLinkByTitle(manager.getTopicManager().getLinkStrategy(), response, "pull-subscriptions");

    // Create the pull-subscription itself.
    ClientResponse<?> res = subscriptions.request().post();
    res.releaseConnection();
    Assert.assertEquals(201, res.getStatus());
    Link sub1 = res.getLocationLink();
    Assert.assertNotNull(sub1);
    Link consumeNext1 =
        getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
    Assert.assertNotNull(consumeNext1);

    // Pull on the topic for 8s (long enoguh to guarantee the rest of the test
    // will pass/fail due to the timeouttask + test operations)
    AcceptWaitListener awlistener = new AcceptWaitListener(consumeNext1.getHref());
    Thread t = new Thread(awlistener);
    t.start();
    // Wait 2 seconds to ensure a new TimeoutTask is running concurrently.
    Thread.sleep(2000);
    // Attempt to create a new pull-subscription. Validate that it takes no longer than 2 seconds
    // (it should take like 20ms, but give it a relatively huge amount of leeway)
    NewPullSubscriber nps = new NewPullSubscriber(subscriptions.getHref());
    Thread npsThread = new Thread(nps);
    npsThread.start();
    Thread.sleep(2000);
    Assert.assertTrue("NewPullSubscriber did not finish in 2 seconds!", nps.isFinished());
    Assert.assertFalse("AcceptWaitListener failed to open connection!", awlistener.isFailed());
    Assert.assertFalse("NewPullSubscriber failed to open new subscription!", nps.isFailed());
    npsThread.interrupt();
    t.interrupt();
  }
  @Test
  public void testProjectedColumns() throws Exception {
    TransformationMetadata metadata =
        RealMetadataFactory.fromDDL(
            ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("northwind.ddl")),
            "northwind",
            "nw");
    EdmDataServices eds = ODataEntitySchemaBuilder.buildMetadata(metadata.getMetadataStore());
    Client client = mock(Client.class);
    stub(client.getMetadataStore()).toReturn(metadata.getMetadataStore());
    stub(client.getMetadata()).toReturn(eds);
    MockProvider.CLIENT = client;
    ArgumentCaptor<String> sql = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EdmEntitySet> entitySet = ArgumentCaptor.forClass(EdmEntitySet.class);

    OEntity entity = createCustomersEntity(eds);
    List<OEntity> result = new ArrayList<OEntity>();
    result.add(entity);

    when(client.sqlExecute(
            anyString(),
            anyListOf(SQLParam.class),
            any(EdmEntitySet.class),
            anyMapOf(String.class, Boolean.class)))
        .thenReturn(result);

    ClientRequest request =
        new ClientRequest(
            TestPortProvider.generateURL(
                "/odata/northwind/Customers?$select=CustomerID,CompanyName,Address"));
    ClientResponse<String> response = request.get(String.class);
    verify(client)
        .sqlExecute(
            sql.capture(),
            anyListOf(SQLParam.class),
            entitySet.capture(),
            anyMapOf(String.class, Boolean.class));

    Assert.assertEquals(
        "SELECT g0.Address, g0.CustomerID, g0.CompanyName FROM Customers AS g0", sql.getValue());
    Assert.assertEquals(200, response.getStatus());
    // Assert.assertEquals("", response.getEntity());
  }
  @Test
  public void testMetadata() throws Exception {
    TransformationMetadata metadata =
        RealMetadataFactory.fromDDL(
            ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("northwind.ddl")),
            "northwind",
            "nw");
    EdmDataServices eds = ODataEntitySchemaBuilder.buildMetadata(metadata.getMetadataStore());
    Client client = mock(Client.class);
    stub(client.getMetadataStore()).toReturn(metadata.getMetadataStore());
    stub(client.getMetadata()).toReturn(eds);
    MockProvider.CLIENT = client;

    StringWriter sw = new StringWriter();

    EdmxFormatWriter.write(eds, sw);

    ClientRequest request =
        new ClientRequest(TestPortProvider.generateURL("/odata/northwind/$metadata"));
    ClientResponse<String> response = request.get(String.class);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(sw.toString(), response.getEntity());
  }
  @Test
  public void testSuccessFirst() throws Exception {
    String testName = "AutoAckTopicTest.testSuccessFirst";
    TopicDeployment deployment = new TopicDeployment();
    deployment.setDuplicatesAllowed(true);
    deployment.setDurableSend(false);
    deployment.setName(testName);
    manager.getTopicManager().deploy(deployment);

    ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/topics/" + testName));

    ClientResponse<?> response = request.head();
    response.releaseConnection();
    Assert.assertEquals(200, response.getStatus());
    Link sender = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), response, "create");
    Link subscriptions =
        getLinkByTitle(manager.getTopicManager().getLinkStrategy(), response, "pull-subscriptions");

    ClientResponse<?> res = subscriptions.request().post();
    res.releaseConnection();
    Assert.assertEquals(201, res.getStatus());
    Link sub1 = res.getLocationLink();
    Assert.assertNotNull(sub1);
    Link consumeNext1 =
        getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
    Assert.assertNotNull(consumeNext1);
    System.out.println("consumeNext1: " + consumeNext1);

    res = subscriptions.request().post();
    res.releaseConnection();
    Assert.assertEquals(201, res.getStatus());
    Link sub2 = res.getLocationLink();
    Assert.assertNotNull(sub2);
    Link consumeNext2 =
        getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
    Assert.assertNotNull(consumeNext2);
    System.out.println("consumeNext2: " + consumeNext2);

    res = sender.request().body("text/plain", "1").post();
    res.releaseConnection();
    Assert.assertEquals(201, res.getStatus());
    res = sender.request().body("text/plain", "2").post();
    res.releaseConnection();
    Assert.assertEquals(201, res.getStatus());

    res = consumeNext1.request().post(String.class);
    Assert.assertEquals(200, res.getStatus());
    Assert.assertEquals("1", res.getEntity(String.class));
    res.releaseConnection();
    consumeNext1 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");

    res = consumeNext1.request().post(String.class);
    Assert.assertEquals(200, res.getStatus());
    Assert.assertEquals("2", res.getEntity(String.class));
    res.releaseConnection();
    consumeNext1 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");

    res = consumeNext2.request().post(String.class);
    Assert.assertEquals(200, res.getStatus());
    Assert.assertEquals("1", res.getEntity(String.class));
    res.releaseConnection();
    consumeNext2 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");

    res = consumeNext2.request().post(String.class);
    Assert.assertEquals(200, res.getStatus());
    Assert.assertEquals("2", res.getEntity(String.class));
    res.releaseConnection();
    consumeNext2 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
    Assert.assertEquals(204, sub1.request().delete().getStatus());
    Assert.assertEquals(204, sub2.request().delete().getStatus());
  }