@Test public void testLocationExtraction() throws IOException { // { // "index" : "test", // "source" : { // value : "something" // } // } FastByteArrayOutputStream os = new FastByteArrayOutputStream(); JsonGenerator gen = new JsonFactory().createJsonGenerator(os, JsonEncoding.UTF8); gen.writeStartObject(); gen.writeStringField("index", "test"); gen.writeFieldName("source"); gen.writeStartObject(); gen.writeStringField("value", "something"); gen.writeEndObject(); gen.writeEndObject(); gen.close(); byte[] data = os.copiedByteArray(); JsonParser parser = new JsonFactory().createJsonParser(data); assertThat(parser.nextToken(), equalTo(JsonToken.START_OBJECT)); assertThat(parser.nextToken(), equalTo(JsonToken.FIELD_NAME)); // "index" assertThat(parser.nextToken(), equalTo(JsonToken.VALUE_STRING)); assertThat(parser.nextToken(), equalTo(JsonToken.FIELD_NAME)); // "source" // JsonLocation location1 = parser.getCurrentLocation(); // parser.skipChildren(); // JsonLocation location2 = parser.getCurrentLocation(); // // byte[] sourceData = new byte[(int) (location2.getByteOffset() - // location1.getByteOffset())]; // System.arraycopy(data, (int) location1.getByteOffset(), sourceData, 0, // sourceData.length); // // JsonParser sourceParser = new JsonFactory().createJsonParser(new // FastByteArrayInputStream(sourceData)); // assertThat(sourceParser.nextToken(), equalTo(JsonToken.START_OBJECT)); // assertThat(sourceParser.nextToken(), equalTo(JsonToken.FIELD_NAME)); // "value" // assertThat(sourceParser.nextToken(), equalTo(JsonToken.VALUE_STRING)); // assertThat(sourceParser.nextToken(), equalTo(JsonToken.END_OBJECT)); }
@Test public void testCreateWithErrors() throws Exception { MvcResult result = mvc.perform( post(BASE_URI, ObjectId.get().toHexString()) .principal(MockUser.mock()) .contentType(MediaType.APPLICATION_JSON) .content(parser.encode(new CommentBody()))) .andReturn(); mvc.perform(asyncDispatch(result)).andExpect(status().isBadRequest()); }
@Test public void testEditComment() throws Exception { MvcResult editAction = mvc.perform( put("/inns/{innId}/comments/{comment}", comment.getInn(), comment.getId()) .principal(MockUser.mock()) .contentType(MediaType.APPLICATION_JSON) .content(parser.encode(contentBody("The pool is awesome, you should try it!!"))) .accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(instanceOf(ResponseEntity.class))) .andReturn(); mvc.perform(asyncDispatch(editAction)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.id").isString()) .andExpect(jsonPath("$.content", is("The pool is awesome, you should try it!!"))); }
@Test public void testCreateComment() throws Exception { MvcResult action = mvc.perform( post(BASE_URI, ObjectId.get().toHexString()) .principal(MockUser.mock()) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) .content(parser.encode(contentBody("#foos")))) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(instanceOf(ResponseEntity.class))) .andReturn(); mvc.perform(asyncDispatch(action)) .andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.id").isString()) .andExpect(jsonPath("$.inn").isString()) .andExpect(jsonPath("$.content", is("#foos"))) .andExpect(jsonPath("$.author", is(1))); }