feign.jaxrs.JAXRSContract Java Examples

The following examples show how to use feign.jaxrs.JAXRSContract. 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: ApacheHttpClientTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void queryParamsAreRespectedWhenBodyIsEmpty() throws InterruptedException {
  final HttpClient httpClient = HttpClientBuilder.create().build();
  final JaxRsTestInterface testInterface = Feign.builder()
      .contract(new JAXRSContract())
      .client(new ApacheHttpClient(httpClient))
      .target(JaxRsTestInterface.class, "http://localhost:" + server.getPort());

  server.enqueue(new MockResponse().setBody("foo"));
  server.enqueue(new MockResponse().setBody("foo"));

  assertEquals("foo", testInterface.withBody("foo", "bar"));
  final RecordedRequest request1 = server.takeRequest();
  assertEquals("/withBody?foo=foo", request1.getPath());
  assertEquals("bar", request1.getBody().readString(StandardCharsets.UTF_8));

  assertEquals("foo", testInterface.withoutBody("foo"));
  final RecordedRequest request2 = server.takeRequest();
  assertEquals("/withoutBody?foo=foo", request2.getPath());
  assertEquals("", request2.getBody().readString(StandardCharsets.UTF_8));
}
 
Example #2
Source File: ApacheHttp5ClientTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void queryParamsAreRespectedWhenBodyIsEmpty() throws InterruptedException {
  final HttpClient httpClient = HttpClientBuilder.create().build();
  final JaxRsTestInterface testInterface = Feign.builder()
      .contract(new JAXRSContract())
      .client(new ApacheHttp5Client(httpClient))
      .target(JaxRsTestInterface.class, "http://localhost:" + server.getPort());

  server.enqueue(new MockResponse().setBody("foo"));
  server.enqueue(new MockResponse().setBody("foo"));

  assertEquals("foo", testInterface.withBody("foo", "bar"));
  final RecordedRequest request1 = server.takeRequest();
  assertEquals("/withBody?foo=foo", request1.getPath());
  assertEquals("bar", request1.getBody().readString(StandardCharsets.UTF_8));

  assertEquals("foo", testInterface.withoutBody("foo"));
  final RecordedRequest request2 = server.takeRequest();
  assertEquals("/withoutBody?foo=foo", request2.getPath());
  assertEquals("", request2.getBody().readString(StandardCharsets.UTF_8));
}
 
Example #3
Source File: JAXRSClientTest.java    From feign with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumesMultipleWithContentTypeHeaderAndBody() throws Exception {
  server.enqueue(new MockResponse().setBody("AAAAAAAA"));
  final JaxRSClientTestInterfaceWithJaxRsContract api = newBuilder()
      .contract(new JAXRSContract()) // use JAXRSContract
      .target(JaxRSClientTestInterfaceWithJaxRsContract.class,
          "http://localhost:" + server.getPort());

  final Response response =
      api.consumesMultipleWithContentTypeHeaderAndBody("application/json;charset=utf-8", "body");
  assertEquals("AAAAAAAA", Util.toString(response.body().asReader(UTF_8)));

  MockWebServerAssertions.assertThat(server.takeRequest())
      .hasHeaders(MapEntry.entry("Content-Type",
          Collections.singletonList("application/json;charset=utf-8")))
      .hasMethod("POST");
}
 
Example #4
Source File: WaggleDanceIntegrationTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void overwritesConfigOnShutdownAfterAddingFederation() throws Exception {
  runner = WaggleDanceRunner
      .builder(configLocation)
      .databaseResolution(DatabaseResolution.PREFIXED)
      .primary("primary", localServer.getThriftConnectionUri(),
          AccessControlType.READ_AND_WRITE_AND_CREATE_ON_DATABASE_WHITELIST)
      .federate(SECONDARY_METASTORE_NAME, remoteServer.getThriftConnectionUri(), REMOTE_DATABASE)
      .build();

  runWaggleDance(runner);
  FederationsAdminClient restClient = Feign
      .builder()
      .contract(new JAXRSContract())
      .encoder(new JacksonEncoder())
      .decoder(new JacksonDecoder())
      .target(FederationsAdminClient.class, "http://localhost:" + runner.getRestApiPort() + "/");

  FederatedMetaStore newFederation = new FederatedMetaStore("new_waggle_remote",
      newRemoteServer.getThriftConnectionUri());
  restClient.add(newFederation);

  Federations federations = stopServerAndGetConfiguration();

  List<FederatedMetaStore> federatedMetastores = federations.getFederatedMetaStores();
  assertThat(federatedMetastores.size(), is(2));

  FederatedMetaStore remoteMetastore = federatedMetastores.get(0);
  assertThat(remoteMetastore.getName(), is(SECONDARY_METASTORE_NAME));
  assertThat(remoteMetastore.getMappedDatabases().size(), is(1));
  assertThat(remoteMetastore.getMappedDatabases().get(0), is(REMOTE_DATABASE));

  FederatedMetaStore newRemoteMetastore = federatedMetastores.get(1);
  assertThat(newRemoteMetastore.getName(), is("new_waggle_remote"));
  assertThat(newRemoteMetastore.getMappedDatabases(), is(nullValue()));
}
 
Example #5
Source File: WaggleDanceIntegrationTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void doesNotOverwriteConfigOnShutdownAfterAddingFederation() throws Exception {
  runner = WaggleDanceRunner
      .builder(configLocation)
      .databaseResolution(DatabaseResolution.PREFIXED)
      .overwriteConfigOnShutdown(false)
      .primary("primary", localServer.getThriftConnectionUri(),
          AccessControlType.READ_AND_WRITE_AND_CREATE_ON_DATABASE_WHITELIST)
      .federate(SECONDARY_METASTORE_NAME, remoteServer.getThriftConnectionUri(), REMOTE_DATABASE)
      .build();

  runWaggleDance(runner);
  FederationsAdminClient restClient = Feign
      .builder()
      .contract(new JAXRSContract())
      .encoder(new JacksonEncoder())
      .decoder(new JacksonDecoder())
      .target(FederationsAdminClient.class, "http://localhost:" + runner.getRestApiPort() + "/");

  FederatedMetaStore newFederation = new FederatedMetaStore("new_waggle_remote",
      newRemoteServer.getThriftConnectionUri());
  restClient.add(newFederation);

  Federations federations = stopServerAndGetConfiguration();

  List<FederatedMetaStore> federatedMetastores = federations.getFederatedMetaStores();
  assertThat(federatedMetastores.size(), is(1));

  FederatedMetaStore remoteMetastore = federatedMetastores.get(0);
  assertThat(remoteMetastore.getName(), is(SECONDARY_METASTORE_NAME));
  assertThat(remoteMetastore.getMappedDatabases().size(), is(1));
  assertThat(remoteMetastore.getMappedDatabases().get(0), is(REMOTE_DATABASE));
}
 
Example #6
Source File: Client.java    From metacat with Apache License 2.0 5 votes vote down vote up
private Client(
    final String host,
    final feign.Client client,
    final feign.Logger.Level logLevel,
    final RequestInterceptor requestInterceptor,
    final Retryer retryer,
    final Request.Options options
) {
    final MetacatJsonLocator metacatJsonLocator = new MetacatJsonLocator();
    final ObjectMapper mapper = metacatJsonLocator
        .getPrettyObjectMapper()
        .copy()
        .registerModule(new GuavaModule())
        .registerModule(new JaxbAnnotationModule());

    log.info("Connecting to {}", host);
    this.host = host;

    feignBuilder = Feign.builder()
        .client(client)
        .logger(new Slf4jLogger())
        .logLevel(logLevel)
        .contract(new JAXRSContract())
        .encoder(new JacksonEncoder(mapper))
        .decoder(new JacksonDecoder(mapper))
        .errorDecoder(new MetacatErrorDecoder(metacatJsonLocator))
        .requestInterceptor(requestInterceptor)
        .retryer(retryer)
        .options(options);

    api = getApiClient(MetacatV1.class);
    partitionApi = getApiClient(PartitionV1.class);
    metadataApi = getApiClient(MetadataV1.class);
    resolverApi = getApiClient(ResolverV1.class);
    tagApi = getApiClient(TagV1.class);
}
 
Example #7
Source File: ModuleConfigurationIntegrationClient.java    From abixen-platform with GNU Lesser General Public License v2.1 5 votes vote down vote up
@HystrixCommand(fallbackMethod = "getModulesConfigurationPropertiesFallback")
public ModulesConfigurationProperties getModulesConfigurationProperties(String serviceName) {
    log.debug("getModulesConfigurationProperties: " + serviceName);

    if (discoveryClient.getInstances(serviceName).isEmpty()) {
        throw new PlatformCoreException("Can not find any instance for " + serviceName);
    }

    ServiceInstance serviceInstance = discoveryClient.getInstances(serviceName).get(0);
    ModuleConfigurationService moduleConfigurationService = Feign.builder().contract(new JAXRSContract()).encoder(new JacksonEncoder())
            .decoder(new JacksonDecoder()).target(ModuleConfigurationService.class,
                    "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort());

    return moduleConfigurationService.getModulesConfigurationProperties();
}
 
Example #8
Source File: OAuthRequestInterceptor.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an interceptor that authenticates all requests.
 */
public OAuthRequestInterceptor() {
    String username = APIMConfigReader.getInstance().getConfig().getUsername();
    String password = APIMConfigReader.getInstance().getConfig().getPassword();
    dcrClient = Feign.builder().client(new OkHttpClient(Utils.getSSLClient())).logger(new Slf4jLogger())
            .logLevel(Logger.Level.FULL).requestInterceptor(new BasicAuthRequestInterceptor(username,
                    password))
            .contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
            .target(DCRClient.class, Utils.replaceProperties(
                    APIMConfigReader.getInstance().getConfig().getDcrEndpoint()));
}
 
Example #9
Source File: ReactiveFeignIntegrationTest.java    From feign with Apache License 2.0 5 votes vote down vote up
@Test
public void testDifferentContract() throws Exception {
  this.webServer.enqueue(new MockResponse().setBody("1.0"));

  TestJaxRSReactorService service = ReactorFeign.builder()
      .contract(new JAXRSContract())
      .target(TestJaxRSReactorService.class, this.getServerUrl());
  StepVerifier.create(service.version())
      .expectNext("1.0")
      .expectComplete()
      .verify();
  assertThat(webServer.takeRequest().getPath()).isEqualToIgnoringCase("/version");
}
 
Example #10
Source File: GitHubExample.java    From feign with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws InterruptedException {
  GitHub github = Feign.builder()
      .contract(new JAXRSContract())
      .target(GitHub.class, "https://api.github.com");

  System.out.println("Let's fetch and print a list of the contributors to this library.");
  List<Contributor> contributors = github.contributors("netflix", "feign");
  for (Contributor contributor : contributors) {
    System.out.println(contributor.login + " (" + contributor.contributions + ")");
  }
}
 
Example #11
Source File: JAXRS2ContractTest.java    From feign with Apache License 2.0 4 votes vote down vote up
@Override
protected JAXRSContract createContract() {
  return new JAXRS2Contract();
}