Java Code Examples for feign.codec.Encoder#encode()

The following examples show how to use feign.codec.Encoder#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JAXBCodecTest.java    From feign with Apache License 2.0 7 votes vote down vote up
@Test
public void encodesXmlWithCustomJAXBSchemaLocation() throws Exception {
  JAXBContextFactory jaxbContextFactory =
      new JAXBContextFactory.Builder()
          .withMarshallerSchemaLocation("http://apihost http://apihost/schema.xsd")
          .build();

  Encoder encoder = new JAXBEncoder(jaxbContextFactory);

  MockObject mock = new MockObject();
  mock.value = "Test";

  RequestTemplate template = new RequestTemplate();
  encoder.encode(mock, MockObject.class, template);

  assertThat(template).hasBody("<?xml version=\"1.0\" encoding=\"UTF-8\" " +
      "standalone=\"yes\"?><mockObject xsi:schemaLocation=\"http://apihost " +
      "http://apihost/schema.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
      "<value>Test</value></mockObject>");
}
 
Example 2
Source File: SOAPCodecTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void decodeAnnotatedParameterizedTypes() throws Exception {
  JAXBContextFactory jaxbContextFactory =
      new JAXBContextFactory.Builder().withMarshallerFormattedOutput(true).build();

  Encoder encoder = new SOAPEncoder(jaxbContextFactory);

  Box<String> boxStr = new Box<>();
  boxStr.set("hello");
  Box<Box<String>> boxBoxStr = new Box<>();
  boxBoxStr.set(boxStr);
  RequestTemplate template = new RequestTemplate();
  encoder.encode(boxBoxStr, Box.class, template);

  Response response = Response.builder()
      .status(200)
      .reason("OK")
      .request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
      .headers(Collections.emptyMap())
      .body(template.body())
      .build();

  new SOAPDecoder(new JAXBContextFactory.Builder().build()).decode(response, Box.class);

}
 
Example 3
Source File: PageableEncoderTests.java    From spring-cloud-openfeign with Apache License 2.0 6 votes vote down vote up
@Test
public void testPaginationAndSortingRequest() {
	Encoder encoder = this.context.getInstance("foo", Encoder.class);
	assertThat(encoder).isNotNull();
	RequestTemplate request = new RequestTemplate();
	encoder.encode(createPageAndSortRequest(), null, request);

	// Request queries shall contain three entries
	assertThat(request.queries()).hasSize(3);
	// Request page shall contain page
	assertThat(request.queries().get("page")).contains(String.valueOf(PAGE));
	// Request size shall contain size
	assertThat(request.queries().get("size")).contains(String.valueOf(SIZE));
	// Request sort size shall contain sort entries
	assertThat(request.queries().get("sort")).hasSize(2);
}
 
Example 4
Source File: SOAPCodecTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void encodesSoapWithCustomJAXBNoSchemaLocation() {
  JAXBContextFactory jaxbContextFactory =
      new JAXBContextFactory.Builder()
          .withMarshallerNoNamespaceSchemaLocation("http://apihost/schema.xsd")
          .build();

  Encoder encoder = new SOAPEncoder(jaxbContextFactory);

  GetPrice mock = new GetPrice();
  mock.item = new Item();
  mock.item.value = "Apples";

  RequestTemplate template = new RequestTemplate();
  encoder.encode(mock, GetPrice.class, template);

  assertThat(template).hasBody("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
      + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
      + "<SOAP-ENV:Header/>"
      + "<SOAP-ENV:Body>"
      + "<GetPrice xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://apihost/schema.xsd\">"
      + "<Item>Apples</Item>"
      + "</GetPrice>"
      + "</SOAP-ENV:Body>"
      + "</SOAP-ENV:Envelope>");
}
 
Example 5
Source File: SOAPCodecTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void encodesSoapWithCustomJAXBSchemaLocation() {
  JAXBContextFactory jaxbContextFactory =
      new JAXBContextFactory.Builder()
          .withMarshallerSchemaLocation("http://apihost http://apihost/schema.xsd")
          .build();

  Encoder encoder = new SOAPEncoder(jaxbContextFactory);

  GetPrice mock = new GetPrice();
  mock.item = new Item();
  mock.item.value = "Apples";

  RequestTemplate template = new RequestTemplate();
  encoder.encode(mock, GetPrice.class, template);

  assertThat(template).hasBody("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
      + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
      + "<SOAP-ENV:Header/>"
      + "<SOAP-ENV:Body>"
      + "<GetPrice xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://apihost http://apihost/schema.xsd\">"
      + "<Item>Apples</Item>"
      + "</GetPrice>"
      + "</SOAP-ENV:Body>"
      + "</SOAP-ENV:Envelope>");
}
 
Example 6
Source File: SOAPCodecTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void encodesSoap() {
  Encoder encoder = new SOAPEncoder.Builder()
      .withJAXBContextFactory(new JAXBContextFactory.Builder().build())
      .build();

  GetPrice mock = new GetPrice();
  mock.item = new Item();
  mock.item.value = "Apples";

  RequestTemplate template = new RequestTemplate();
  encoder.encode(mock, GetPrice.class, template);

  String soapEnvelop = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
      "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
      "<SOAP-ENV:Header/>" +
      "<SOAP-ENV:Body>" +
      "<GetPrice>" +
      "<Item>Apples</Item>" +
      "</GetPrice>" +
      "</SOAP-ENV:Body>" +
      "</SOAP-ENV:Envelope>";
  assertThat(template).hasBody(soapEnvelop);
}
 
Example 7
Source File: JAXBCodecTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void decodeAnnotatedParameterizedTypes() throws Exception {
  JAXBContextFactory jaxbContextFactory =
      new JAXBContextFactory.Builder().withMarshallerFormattedOutput(true).build();

  Encoder encoder = new JAXBEncoder(jaxbContextFactory);

  Box<String> boxStr = new Box<>();
  boxStr.set("hello");
  Box<Box<String>> boxBoxStr = new Box<>();
  boxBoxStr.set(boxStr);
  RequestTemplate template = new RequestTemplate();
  encoder.encode(boxBoxStr, Box.class, template);

  Response response = Response.builder()
      .status(200)
      .reason("OK")
      .request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
      .headers(Collections.<String, Collection<String>>emptyMap())
      .body(template.body())
      .build();

  new JAXBDecoder(new JAXBContextFactory.Builder().build()).decode(response, Box.class);

}
 
Example 8
Source File: JAXBCodecTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void encodesXmlWithCustomJAXBFormattedOutput() {
  JAXBContextFactory jaxbContextFactory =
      new JAXBContextFactory.Builder().withMarshallerFormattedOutput(true).build();

  Encoder encoder = new JAXBEncoder(jaxbContextFactory);

  MockObject mock = new MockObject();
  mock.value = "Test";

  RequestTemplate template = new RequestTemplate();
  encoder.encode(mock, MockObject.class, template);

  // RequestTemplate always expects a UNIX style newline.
  assertThat(template).hasBody(
      new StringBuilder().append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>")
          .append("\n")
          .append("<mockObject>")
          .append("\n")
          .append("    <value>Test</value>")
          .append("\n")
          .append("</mockObject>")
          .append("\n")
          .toString());
}
 
Example 9
Source File: JAXBCodecTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void encodesXmlWithCustomJAXBNoNamespaceSchemaLocation() throws Exception {
  JAXBContextFactory jaxbContextFactory =
      new JAXBContextFactory.Builder()
          .withMarshallerNoNamespaceSchemaLocation("http://apihost/schema.xsd").build();

  Encoder encoder = new JAXBEncoder(jaxbContextFactory);

  MockObject mock = new MockObject();
  mock.value = "Test";

  RequestTemplate template = new RequestTemplate();
  encoder.encode(mock, MockObject.class, template);

  assertThat(template)
      .hasBody(
          "<?xml version=\"1.0\" encoding=\"UTF-8\" "
              + "standalone=\"yes\"?><mockObject xsi:noNamespaceSchemaLocation=\"http://apihost/schema.xsd\" "
              + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
              + "<value>Test</value></mockObject>");
}
 
Example 10
Source File: JAXBCodecTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void encodesXmlWithCustomJAXBEncoding() throws Exception {
  JAXBContextFactory jaxbContextFactory =
      new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-16").build();

  Encoder encoder = new JAXBEncoder(jaxbContextFactory);

  MockObject mock = new MockObject();
  mock.value = "Test";

  RequestTemplate template = new RequestTemplate();
  encoder.encode(mock, MockObject.class, template);

  assertThat(template).hasBody("<?xml version=\"1.0\" encoding=\"UTF-16\" "
      + "standalone=\"yes\"?><mockObject><value>Test</value></mockObject>");
}
 
Example 11
Source File: SpringEncoderTests.java    From spring-cloud-openfeign with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipartFile2() {
	Encoder encoder = this.context.getInstance("foo", Encoder.class);
	assertThat(encoder).isNotNull();
	RequestTemplate request = new RequestTemplate();
	request.header(ACCEPT, MediaType.MULTIPART_FORM_DATA_VALUE);
	request.header(CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE);

	MultipartFile multipartFile = new MockMultipartFile("test_multipart_file",
			"hi".getBytes());
	encoder.encode(multipartFile, MultipartFile.class, request);

	assertThat((String) ((List) request.headers().get(CONTENT_TYPE)).get(0))
			.as("Request Content-Type is not multipart/form-data")
			.contains("multipart/form-data; charset=UTF-8; boundary=");
	assertThat(request.headers().get(CONTENT_TYPE).size())
			.as("There is more than one Content-Type request header").isEqualTo(1);
	assertThat(((List) request.headers().get(ACCEPT)).get(0))
			.as("Request Accept header is not multipart/form-data")
			.isEqualTo(MULTIPART_FORM_DATA_VALUE);
	assertThat(((List) request.headers().get(CONTENT_LENGTH)).get(0))
			.as("Request Content-Length is not equal to 186").isEqualTo("186");
	assertThat(new String(request.requestBody().asBytes()))
			.as("Body content cannot be decoded").contains("hi");
}
 
Example 12
Source File: SpringEncoderTests.java    From spring-cloud-openfeign with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomHttpMessageConverter() {
	Encoder encoder = this.context.getInstance("foo", Encoder.class);
	assertThat(encoder).isNotNull();
	RequestTemplate request = new RequestTemplate();

	encoder.encode("hi", MyType.class, request);

	Collection<String> contentTypeHeader = request.headers().get("Content-Type");
	assertThat(contentTypeHeader).as("missing content type header").isNotNull();
	assertThat(contentTypeHeader.isEmpty()).as("missing content type header")
			.isFalse();

	String header = contentTypeHeader.iterator().next();
	assertThat(header).as("content type header is wrong")
			.isEqualTo("application/mytype");

	assertThat(request.requestCharset()).as("request charset is null").isNotNull();
	assertThat(request.requestCharset()).as("request charset is wrong")
			.isEqualTo(StandardCharsets.UTF_8);
}
 
Example 13
Source File: SpringEncoderTests.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
@Test(expected = EncodeException.class)
public void testMultipartFile1() {
	Encoder encoder = this.context.getInstance("foo", Encoder.class);
	assertThat(encoder).isNotNull();
	RequestTemplate request = new RequestTemplate();

	MultipartFile multipartFile = new MockMultipartFile("test_multipart_file",
			"hi".getBytes());
	encoder.encode(multipartFile, MultipartFile.class, request);
}
 
Example 14
Source File: SpringEncoderTests.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
@Test
public void testBinaryData() {
	Encoder encoder = this.context.getInstance("foo", Encoder.class);
	assertThat(encoder).isNotNull();

	RequestTemplate request = new RequestTemplate();

	encoder.encode("hi".getBytes(), null, request);

	assertThat(((List) request.headers().get(CONTENT_TYPE)).get(0))
			.as("Request Content-Type is not octet-stream")
			.isEqualTo(APPLICATION_OCTET_STREAM_VALUE);
}
 
Example 15
Source File: SOAPCodecTest.java    From feign with Apache License 2.0 5 votes vote down vote up
@Test
public void encodesSoapWithCustomJAXBMarshallerEncoding() {
  JAXBContextFactory jaxbContextFactory =
      new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-16").build();

  Encoder encoder = new SOAPEncoder.Builder()
      // .withWriteXmlDeclaration(true)
      .withJAXBContextFactory(jaxbContextFactory)
      .withCharsetEncoding(StandardCharsets.UTF_16)
      .build();

  GetPrice mock = new GetPrice();
  mock.item = new Item();
  mock.item.value = "Apples";

  RequestTemplate template = new RequestTemplate();
  encoder.encode(mock, GetPrice.class, template);

  String soapEnvelop = "<?xml version=\"1.0\" encoding=\"UTF-16\" ?>" +
      "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
      "<SOAP-ENV:Header/>" +
      "<SOAP-ENV:Body>" +
      "<GetPrice>" +
      "<Item>Apples</Item>" +
      "</GetPrice>" +
      "</SOAP-ENV:Body>" +
      "</SOAP-ENV:Envelope>";
  byte[] utf16Bytes = soapEnvelop.getBytes(StandardCharsets.UTF_16LE);
  assertThat(template).hasBody(utf16Bytes);
}
 
Example 16
Source File: PageableEncoderTests.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnpagedRequest() {
	Encoder encoder = this.context.getInstance("foo", Encoder.class);
	assertThat(encoder).isNotNull();
	RequestTemplate request = new RequestTemplate();

	encoder.encode(Pageable.unpaged(), null, request);
	// Request queries shall contain three entries
	assertThat(request.queries()).isEmpty();
}
 
Example 17
Source File: PageableEncoderTests.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
@Test
public void testSortingRequest() {
	Encoder encoder = this.context.getInstance("foo", Encoder.class);
	assertThat(encoder).isNotNull();
	RequestTemplate request = new RequestTemplate();

	encoder.encode(createSort(), null, request);
	// Request queries shall contain three entries
	assertThat(request.queries().size()).isEqualTo(1);
	// Request sort size shall contain sort entries
	assertThat(request.queries().get("sort")).hasSize(2);
}
 
Example 18
Source File: SOAPCodecTest.java    From feign with Apache License 2.0 5 votes vote down vote up
@Test
public void encodesSoapWithCustomJAXBFormattedOuput() {
  Encoder encoder = new SOAPEncoder.Builder().withFormattedOutput(true)
      .withJAXBContextFactory(new JAXBContextFactory.Builder()
          .build())
      .build();

  GetPrice mock = new GetPrice();
  mock.item = new Item();
  mock.item.value = "Apples";

  RequestTemplate template = new RequestTemplate();
  encoder.encode(mock, GetPrice.class, template);

  assertThat(template).hasBody(
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + System.lineSeparator() +
          "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
          + System.lineSeparator() +
          "    <SOAP-ENV:Header/>" + System.lineSeparator() +
          "    <SOAP-ENV:Body>" + System.lineSeparator() +
          "        <GetPrice>" + System.lineSeparator() +
          "            <Item>Apples</Item>" + System.lineSeparator() +
          "        </GetPrice>" + System.lineSeparator() +
          "    </SOAP-ENV:Body>" + System.lineSeparator() +
          "</SOAP-ENV:Envelope>" + System.lineSeparator() +
          "");
}
 
Example 19
Source File: PageableEncoderTests.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
@Test
public void testPaginationRequest() {
	Encoder encoder = this.context.getInstance("foo", Encoder.class);
	assertThat(encoder).isNotNull();
	RequestTemplate request = new RequestTemplate();
	encoder.encode(createPageAndRequest(), null, request);
	assertThat(request.queries().size()).isEqualTo(2);
	// Request page shall contain page
	assertThat(request.queries().get("page")).contains(String.valueOf(PAGE));
	// Request size shall contain size
	assertThat(request.queries().get("size")).contains(String.valueOf(SIZE));
	// Request sort size shall contain sort entries
	assertThat(request.queries()).doesNotContainKey("sort");
}