@Test
@RunAsClient
public void postJsonResource() throws Exception {
// Create a new Resource
final Resource res = new Resource("new-resource");
res.setType(ResourceType.FILE);
res.setContentType(ContentType.TextPlain);
res.setLang(LocaleId.EN_US);
res.setRevision(1);
res.getExtensions(true).add(new PoHeader("This is a PO Header"));
TextFlow tf1 = new TextFlow("tf1", LocaleId.EN_US, "First Text Flow");
tf1.getExtensions(true).add(new SimpleComment("This is one comment"));
res.getTextFlows().add(tf1);
new ResourceRequest(
getRestEndpointUrl("/projects/p/sample-project/iterations/i/1.0/r"),
"POST", getAuthorizedEnvironment()) {
@Override
protected void prepareRequest(ClientRequest request) {
request.queryParameter("ext", PoHeader.ID).queryParameter(
"ext", SimpleComment.ID);
request.body(MediaType.APPLICATION_JSON, jsonMarshal(res));
}
@Override
protected void onResponse(ClientResponse response) {
assertThat(response.getStatus(),
is(Status.CREATED.getStatusCode())); // 201
}
}.run();
// Verify that it was created successfully
ISourceDocResource sourceDocClient =
super.createProxy(
createClientProxyFactory(TRANSLATOR, TRANSLATOR_KEY),
ISourceDocResource.class,
"/projects/p/sample-project/iterations/i/1.0/r");
ClientResponse<Resource> resourceResponse =
sourceDocClient.getResource(res.getName(), new StringSet(
PoHeader.ID + ";" + SimpleComment.ID));
assertThat(resourceResponse.getStatus(), is(Status.OK.getStatusCode())); // 200
Resource createdResource = resourceResponse.getEntity();
assertThat(createdResource.getName(), is(res.getName()));
assertThat(createdResource.getType(), is(res.getType()));
assertThat(createdResource.getContentType(), is(res.getContentType()));
assertThat(createdResource.getLang(), is(res.getLang()));
assertThat(createdResource.getRevision(), is(1)); // Created, so
// revision 1
// Extensions
assertThat(createdResource.getExtensions(true).size(),
greaterThanOrEqualTo(1));
assertThat(
createdResource.getExtensions(true).findByType(PoHeader.class)
.getComment(), is("This is a PO Header"));
// Text Flow
assertThat(createdResource.getTextFlows().size(), is(1));
TextFlow createdTf = createdResource.getTextFlows().get(0);
assertThat(createdTf.getContents(), is(tf1.getContents()));
assertThat(createdTf.getId(), is(tf1.getId()));
assertThat(createdTf.getLang(), is(tf1.getLang()));
assertThat(createdTf.getRevision(), is(1)); // Create, so revision 1