List of usage examples for org.springframework.mock.http MockHttpOutputMessage MockHttpOutputMessage
MockHttpOutputMessage
From source file:curly.artifactory.ArtifactoryTestHelper.java
public static String json(Object o, HttpMessageConverter<Object> httpMessageConverter) { MockHttpOutputMessage message = new MockHttpOutputMessage(); try {// ww w . j a va 2 s . com httpMessageConverter.write(o, jsonMediaType(), message); } catch (IOException ignore) { } return message.getBodyAsString(); }
From source file:org.opentides.web.json.ViewAwareJsonMessageConverterTest.java
@Test public void testWriteInternal() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); PojoView pojoView = new PojoView(new SampleClass("John", "Doe", "25", "john.doe@opentides.com"), SearchView.class); converter.writeInternal(pojoView, outputMessage); assertEquals("{\"name\":\"John\"}", outputMessage.getBodyAsString()); pojoView = new PojoView(new SampleClass("John", "Doe", "25", "john.doe@opentides.com"), FormView.class); outputMessage = new MockHttpOutputMessage(); converter.writeInternal(pojoView, outputMessage); assertEquals("{\"name\":\"John\",\"lastName\":\"Doe\"}", outputMessage.getBodyAsString()); pojoView = new PojoView(new SampleClass("John", "Doe", "25", "john.doe@opentides.com"), DisplayView.class); outputMessage = new MockHttpOutputMessage(); converter.writeInternal(pojoView, outputMessage); assertEquals("{\"name\":\"John\",\"lastName\":\"Doe\",\"age\":\"25\"}", outputMessage.getBodyAsString()); pojoView = new PojoView(new SampleClass("John", "Doe", "25", "john.doe@opentides.com"), FullView.class); outputMessage = new MockHttpOutputMessage(); converter.writeInternal(pojoView, outputMessage); assertEquals("{\"name\":\"John\",\"lastName\":\"Doe\",\"age\":\"25\",\"email\":\"john.doe@opentides.com\"}", outputMessage.getBodyAsString()); }
From source file:com.epam.ta.reportportal.ws.resolver.JacksonViewAwareSerializationTest.java
@Test public void testWithView() throws HttpMessageNotWritableException, IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(new JacksonViewAware(bean, DemoViewFirst.class), MediaType.APPLICATION_JSON, outputMessage); Assert.assertThat(outputMessage.getBodyAsString(), not(containsString(NO_VIEW_THERE))); Assert.assertThat(outputMessage.getBodyAsString(), containsString(FIELD_WITH_VIEW)); }
From source file:application.controllers.RestControllerTest.java
protected String json(Object o) throws IOException { MockHttpOutputMessage mockHttpOutputMessage = new MockHttpOutputMessage(); this.mappingJackson2HttpMessageConverter.write(o, MediaType.APPLICATION_JSON, mockHttpOutputMessage); return mockHttpOutputMessage.getBodyAsString(); }
From source file:com.epam.ta.reportportal.ws.resolver.JacksonViewAwareSerializationTest.java
@Test public void testWithNoView() throws HttpMessageNotWritableException, IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(bean, MediaType.APPLICATION_JSON, outputMessage); Assert.assertThat(outputMessage.getBodyAsString(), containsString(NO_VIEW_THERE)); Assert.assertThat(outputMessage.getBodyAsString(), containsString(FIELD_WITH_VIEW)); }
From source file:org.oncoblocks.centromere.web.test.util.HttpMessageConverterTests.java
@Test public void writeToJsonNoFilter() throws Exception { List<EntrezGene> genes = EntrezGene.createDummyData(); ResponseEnvelope envelope = new ResponseEnvelope(genes); Assert.isTrue(jsonConverter.canWrite(envelope.getClass(), MediaType.APPLICATION_JSON)); MockHttpOutputMessage message = new MockHttpOutputMessage(); jsonConverter.write(envelope, MediaType.APPLICATION_JSON, message); Object document = Configuration.defaultConfiguration().jsonProvider().parse(message.getBodyAsString()); Assert.isTrue(!((List<String>) JsonPath.read(document, "$")).isEmpty()); Assert.isTrue(((List<String>) JsonPath.read(document, "$")).size() == 5); Assert.notNull(JsonPath.read(document, "$[0].entrezGeneId")); Assert.isTrue((Integer) JsonPath.read(document, "$[0].entrezGeneId") == 1); }
From source file:net.eusashead.hateoas.hal.http.converter.module.HalHttpMessageConverterModuleTest.java
@Test public void testWriteWithModule() throws IOException { // Should be unable to write without module Assert.assertFalse(converter.canWrite(Foo.class, HalHttpMessageConverter.HAL_JSON)); // Add module addModule();//w w w.ja v a 2s. c o m // Should be able to write Assert.assertTrue(converter.canWrite(Foo.class, HalHttpMessageConverter.HAL_JSON)); // Create a test output message HttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(new Foo(), HalHttpMessageConverter.HAL_JSON, outputMessage); Assert.assertNotNull(outputMessage.getBody()); String expected = HalTestUtils.stringFromFile("src/test/resources/foo.json"); Assert.assertEquals(expected, outputMessage.getBody().toString()); }
From source file:org.oncoblocks.centromere.web.test.util.HttpMessageConverterTests.java
@Test public void writeToJsonWithFieldFilter() throws Exception { List<EntrezGene> genes = EntrezGene.createDummyData(); Set<String> fields = new HashSet<>(); fields.add("primaryGeneSymbol"); ResponseEnvelope envelope = new ResponseEnvelope(genes, fields, new HashSet<>()); Assert.isTrue(jsonConverter.canWrite(envelope.getClass(), MediaType.APPLICATION_JSON)); MockHttpOutputMessage message = new MockHttpOutputMessage(); jsonConverter.write(envelope, MediaType.APPLICATION_JSON, message); Object document = Configuration.defaultConfiguration().jsonProvider().parse(message.getBodyAsString()); Assert.isTrue(!((List<String>) JsonPath.read(document, "$")).isEmpty()); Assert.isTrue(((List<String>) JsonPath.read(document, "$")).size() == 5); Map<String, Object> gene = JsonPath.read(document, "$[0]"); Assert.isTrue(!gene.containsKey("entrezGeneId")); Assert.isTrue(!gene.containsKey("aliases")); Assert.isTrue(!gene.containsKey("attributes")); Assert.isTrue(gene.containsKey("primaryGeneSymbol")); Assert.isTrue(gene.get("primaryGeneSymbol").equals("GeneA")); }
From source file:net.eusashead.hateoas.hal.http.converter.HalHttpMessageConverterTest.java
@Test public void testWriteRepresentationJson() throws HttpMessageNotWritableException, IOException { ReadableRepresentation rep = HalTestUtils.halFromFile(BASKET_JSON); HttpOutputMessage message = new MockHttpOutputMessage(); converter.write(rep, HAL_JSON, message); Assert.assertEquals(HalTestUtils.stringFromFile(BASKET_JSON), message.getBody().toString()); }
From source file:io.curly.advisor.web.HttpSecurityAntMatchersTests.java
public String entity() { ReviewEntity reviewEntity = new ReviewEntity(); reviewEntity.setArtifact(ObjectId.get().toHexString()); reviewEntity.setContent("abcd"); reviewEntity.setTitle("some fancy title"); reviewEntity.setRate(BigDecimal.ONE); MockHttpOutputMessage message = new MockHttpOutputMessage(); try {//from w w w . j a v a 2 s . com new MappingJackson2HttpMessageConverter().write(reviewEntity, MediaType.APPLICATION_JSON, message); } catch (IOException ignore) { } return message.getBodyAsString(); }