在调用GET请求之前等待POST完成处理

时间:2016-04-13 16:17:15

标签: java spring rest testing junit

我正在进行集成测试,我调用POST请求将文件发送到服务器。在POST之后,我立即调用一个GET请求来检索文档并获得一个响应,该响应为我提供了该文档的属性。但是,有时候我会收到404错误,说文件不存在,直到几秒后我再次运行它。

我认为POST还没有完成处理,所以我已经实现了一个Thread.sleep等待10秒,但这种方式似乎不太好,因为它可能需要等待的时间超过它需要或者可能不会等待足够长的时间。是否有某种“隐式等待”允许POST在请求GET之前完成处理?

以下是代码段:

@Test
public void PostDocumentThenCheckIfDocumentExistThenRemove() throws IOException, InterruptedException {     
    try {
        String str = fileToStringProcessing("C:/Users/Linh/Desktop/file.xml");
        ResponseEntity<Message> postResponse = getRestTemplate().exchange(getUri() + "documents", HttpMethod.POST, new HttpEntity(str, getHeaders()), Message.class);
        Thread.sleep(10000);
        ResponseEntity<Account> getResponse = getRestTemplate().exchange(getUri() + "account/7452616052/documents?start=2015-01-01&end=2016-03-31", HttpMethod.GET, getHttpEntity(), Account.class);
        ResponseEntity<Message> deleteResponse = getRestTemplate().exchange(getUri() + "documents/file.xml", HttpMethod.DELETE, getHttpEntity(), Message.class);

        assertThat(postResponse.getStatusCode(), is(HttpStatus.CREATED));
        assertThat(getResponse.getStatusCode(), is(HttpStatus.OK));
        assertThat(deleteResponse.getStatusCode(), is(HttpStatus.OK));
    }catch(HttpClientErrorException e) {
        fail("Error! Status code " + e.getStatusCode());
    }   
}

以下是控制台中的404错误。你可以在这里看到POST是成功的但是GET不是。之后,几秒钟之后执行GET将显示成功

11:56:32.700 [main] DEBUG o.s.web.client.RestTemplate - POST request for "https://dpdev.billing.com/tf/dp/documents" resulted in 201 (Created)
11:56:32.700 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Message] as "application/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1dac5ef]
11:56:32.904 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31"
11:56:32.914 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
11:56:33.690 [main] DEBUG o.s.web.client.RestTemplate - GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31" resulted in 404 (Not Found); invoking error handler

在404之后立即发出GET和DELETE请求:

12:00:21.383 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31"
12:00:21.444 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
12:00:23.176 [main] DEBUG o.s.web.client.RestTemplate - GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31" resulted in 200 (OK)
12:00:23.176 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Account] as "application/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@37271612]
12:00:23.380 [main] DEBUG o.s.web.client.RestTemplate - Created DELETE request for "https://dpdev.billing.com/tf/dp/documents/file.xml"
12:00:23.381 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
12:00:25.120 [main] DEBUG o.s.web.client.RestTemplate - DELETE request for "https://dpdev.billing.com/tf/dp/documents/file.xml" resulted in 200 (OK)
12:00:25.120 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Message] as "application/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@37271612]

1 个答案:

答案 0 :(得分:0)

REST服务是否异步接收请求。如果没有,那么当您发出下一个GET请求时,该文档应该可用。 HTTP线程阻塞,直到您收到POST的响应。可能POST正在返回错误代码。在发出get之前,首先从postResponse检查状态代码。

    String str = fileToStringProcessing("C:/Users/Linh/Desktop/file.xml");
    ResponseEntity<Message> postResponse = getRestTemplate().exchange(getUri() + "documents", HttpMethod.POST, new HttpEntity(str, getHeaders()), Message.class);
    if(postResponse.getStatusCode() == HttpStatus.OK){ or other success code
       ResponseEntity<Account> getResponse = getRestTemplate().exchange(getUri() + "account/7452616052/documents?start=2015-01-01&end=2016-03-31", HttpMethod.GET, getHttpEntity(), Account.class);
       ResponseEntity<Message> deleteResponse = getRestTemplate().exchange(getUri() + "documents/2015067452616054", HttpMethod.DELETE, getHttpEntity(), Message.class);
    }
    else{
       //process error response
    }