Exemple #1
0
  /**
   * Used for injecting a test client in unit tests
   *
   * @param authenticationType The type of authentication to use in the request
   * @param client A specific client object to use for the request
   */
  protected JerseyClient(
      final ClientFactory.AuthenticationType authenticationType, final Client client) {
    this.client =
        client
            .register(GZipEncoder.class)
            .register(EncodingFilter.class)
            .register(createMoxyJsonResolver());

    switch (authenticationType) {
      case PREEMPTIVE_BASIC:
        client.register(HttpAuthenticationFeature.basicBuilder().build());
        break;

      case NON_PREEMPTIVE_BASIC:
        client.register(HttpAuthenticationFeature.basicBuilder().nonPreemptive().build());
        break;

      case DIGEST:
        client.register(HttpAuthenticationFeature.digest());
        break;

      case NON_PREEMPTIVE_BASIC_DIGEST:
        client.register(HttpAuthenticationFeature.universalBuilder().build());

      case NONE:
      default:
    }
  }
  private Client createClient(
      Environment environment,
      JerseyClientConfiguration configuration,
      String federationName,
      FederatedPeer peer)
      throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException,
          CertificateException {
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
    trustManagerFactory.init(initializeTrustStore(peer.getName(), peer.getCertificate()));

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(
        null, trustManagerFactory.getTrustManagers(), SecureRandom.getInstance("SHA1PRNG"));

    SSLConnectionSocketFactory sslConnectionSocketFactory =
        new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier());
    Registry<ConnectionSocketFactory> registry =
        RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslConnectionSocketFactory)
            .build();

    Client client =
        new JerseyClientBuilder(environment)
            .using(configuration)
            .using(registry)
            .build("FederatedClient");

    client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
    client.property(ClientProperties.READ_TIMEOUT, 10000);
    client.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
    client.register(HttpAuthenticationFeature.basic(federationName, peer.getAuthenticationToken()));

    return client;
  }
  @Test
  public void test_creating_cchat_during_customer_signup() {
    Client client = JerseyClientBuilder.createClient();
    HttpAuthenticationFeature feature =
        HttpAuthenticationFeature.basic("*****@*****.**", "password");
    client.register(feature);

    CChat cchat = new CChat("awais-zara");
    List<CChat> cchats = new ArrayList<CChat>();
    cchats.add(cchat);

    Customer customer = new Customer();
    customer.setUsername("*****@*****.**");
    customer.setPassword("password");
    customer.setCchat(cchats);

    Customer customerPersisted =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(new StringBuilder("/customer").append("/signup").toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(customer), Customer.class);

    assertThat(customerPersisted.getId()).isNotNull();
    assertThat(customerPersisted.getCchat().get(0).getId()).isNotNull();
    assertThat(customerPersisted.getCchat().get(0).getUniqueName())
        .isEqualToIgnoringCase("awais-zara");
  }
 private ClientConfig getClientConfig() {
   final ClientConfig cc = new ClientConfig();
   /*2.5-*/
   // cc.register(new HttpDigestAuthFilter("caroline", "zhang"));
   /*2.5+*/
   HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest("caroline", "zhang");
   cc.register(feature);
   return cc;
 }
  @Test
  public void test_edit_customer_shipping() {
    Client client = JerseyClientBuilder.createClient();
    HttpAuthenticationFeature feature =
        HttpAuthenticationFeature.basic("*****@*****.**", "password");
    client.register(feature);

    // CREATE NEW USER
    Customer customer = new Customer();
    customer.setUsername("*****@*****.**");
    customer.setPassword("password");
    Customer customerPersisted =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(new StringBuilder("/customer").append("/signup").toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(customer), Customer.class);
    assertThat(customerPersisted.getId()).isNotNull();

    // ADD CUSTOMERSHIPPING TO USER
    CustomerShipping customerShipping =
        new CustomerShipping(
            "Usman", "Chaudhri", "2460 Fulton", "San Francisco", "CA", "94118", "USA", "Y");
    CustomerShipping persistedCustomerShipping =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(
                new StringBuilder("/customer/")
                    .append(customerPersisted.getId())
                    .append("/customershipping")
                    .toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(customerShipping), CustomerShipping.class);
    assertThat(persistedCustomerShipping.getId()).isNotNull();

    // EDIT CUSTOMER SHIPPING
    CustomerShipping customerShippingNew =
        new CustomerShipping(
            "Zarka", "Ahmed", "92E Street 3", "Lahore", "PJ", "04004", "Pakistan", "Y");
    CustomerShipping customerShippingUpdated =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(
                new StringBuilder("/customer/")
                    .append(customerPersisted.getId())
                    .append("/customershipping/")
                    .append(persistedCustomerShipping.getId())
                    .toString())
            .request(MediaType.APPLICATION_JSON)
            .put(Entity.json(customerShippingNew), CustomerShipping.class);

    assertThat(customerShippingUpdated.getId()).isNotNull();
    assertThat(customerShippingUpdated.getFirstName()).isEqualTo("Zarka");
    assertThat(customerShippingUpdated.getLastName()).isEqualTo("Ahmed");
    assertThat(customerShippingUpdated.getAddress()).isEqualTo("92E Street 3");
    assertThat(customerShippingUpdated.getCity()).isEqualTo("Lahore");
  }
  public WikiServiceJerseyImpl(
      String proxyProtocol,
      String proxyUri,
      String proxyPort,
      String proxyUsername,
      String proxyPassword,
      String wikiUsername,
      String wikiPassword,
      URI uri) {
    this.proxyProtocol = proxyProtocol;
    this.proxyUri = proxyUri;
    this.proxyPort = proxyPort;
    this.proxyUsername = proxyUsername;
    this.proxyPassword = proxyPassword;
    this.wikiUsername = wikiUsername;
    this.wikiPassword = wikiPassword;

    this.uri = uri;

    // BASIC AUTHENTICATION. See https://jersey.java.net/documentation/latest/client.html#d0e4910
    // (5.9.1. Http Authentication Support)
    HttpAuthenticationFeature httpAuthenticationFeature =
        HttpAuthenticationFeature.basic(wikiUsername, wikiPassword);

    this.client =
        ClientBuilder.newClient(
            new ClientConfig()
                // A.3. Client configuration properties:
                // https://jersey.java.net/documentation/latest/user-guide.html#appendix-properties-client
                .property(
                    ClientProperties.PROXY_URI,
                    proxyProtocol
                        + "://"
                        + proxyUri
                        + ":"
                        + proxyPort) // URI of a HTTP proxy the client connector should use. Default
                // value is not set. Currently supported with
                // ApacheConnectorProvider and JettyConnectorProvider only.
                .property(
                    ClientProperties.PROXY_USERNAME,
                    proxyUsername) // User name which will be used for HTTP proxy authentication.
                // Default value is not set. Currently supported with
                // ApacheConnectorProvider and JettyConnectorProvider only.
                .property(
                    ClientProperties.PROXY_PASSWORD,
                    proxyPassword) // Password which will be used for HTTP proxy authentication.
                // Default value is not set. Currently supported with
                // ApacheConnectorProvider and JettyConnectorProvider only.
                // 5.5. Client Transport Connectors:
                // https://jersey.java.net/documentation/latest/user-guide.html#d0e4502
                .connectorProvider(new ApacheConnectorProvider())
                .register(httpAuthenticationFeature));
  }
 private ClientConfig getClientConfig() {
   final ClientConfig cc = new ClientConfig();
   /*2.5-*/
   // cc.register(new HttpBasicAuthFilter("caroline", "zhang"));
   /*2.5+*/
   HttpAuthenticationFeature feature =
       HttpAuthenticationFeature.basicBuilder()
           .nonPreemptive()
           .credentials("caroline", "zhang")
           .build();
   cc.register(feature);
   return cc;
 }
  @Ignore
  @Test
  public void test_delete_customer_shipping_address()
      throws JsonParseException, JsonMappingException, IOException {
    Client client = JerseyClientBuilder.createClient();
    HttpAuthenticationFeature feature =
        HttpAuthenticationFeature.basic("*****@*****.**", "password");
    client.register(feature);

    // CREATE NEW USER
    Customer customer = new Customer();
    customer.setUsername("*****@*****.**");
    customer.setPassword("password");
    Customer customerPersisted =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(new StringBuilder("/customer").append("/signup").toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(customer), Customer.class);
    assertThat(customerPersisted.getId()).isNotNull();

    // ADD CUSTOMERSHIPPING TO USER
    CustomerShipping customerShipping =
        new CustomerShipping(
            "Usman", "Chaudhri", "2460 Fulton", "San Francisco", "CA", "94118", "USA", "Y");
    CustomerShipping persistedCustomerShipping =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(new StringBuilder("/customershipping").toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(customerShipping), CustomerShipping.class);
    assertThat(persistedCustomerShipping.getId()).isNotNull();
    assertThat(persistedCustomerShipping.getIsActive()).isNotNull();

    // DELETED CUSTOMERSHIPPING ADDRESS
    CustomerShipping deletedCustomerShipping =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(
                new StringBuilder("/customershipping/")
                    .append(persistedCustomerShipping.getId())
                    .toString())
            .request(MediaType.APPLICATION_JSON)
            .delete(CustomerShipping.class);
    assertThat(deletedCustomerShipping.getId()).isNotNull();
  }
  @Test
  public void test_customer_update_cchat_list() {
    Client client = JerseyClientBuilder.createClient();
    HttpAuthenticationFeature feature =
        HttpAuthenticationFeature.basic("*****@*****.**", "password");
    client.register(feature);

    CChat cchat1 = new CChat("Usman-Safina-1");
    CChat cchat2 = new CChat("Usman-Amjad-1");
    CChat cchat3 = new CChat("Usman-Talha-1");
    List<CChat> cchats = new ArrayList<CChat>();
    cchats.add(cchat1);
    cchats.add(cchat2);
    cchats.add(cchat3);

    Customer customer = new Customer();
    customer.setUsername("*****@*****.**");
    customer.setPassword("password");
    customer.setCchat(cchats);
    Customer customerPersisted =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(new StringBuilder("/customer").append("/signup").toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(customer), Customer.class);
    assertThat(customerPersisted.getId()).isNotNull();

    CChat cchat4 = new CChat("Usman-Safina-2");
    CChat cchatPersisted =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(
                new StringBuilder("/customer/")
                    .append(customerPersisted.getId())
                    .append("/cchat")
                    .toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(cchat4), CChat.class);

    assertThat(cchatPersisted.getId()).isNotNull();
  }
  @Test
  public void test_find_customer_by_id() {
    Client client = JerseyClientBuilder.createClient();
    HttpAuthenticationFeature feature =
        HttpAuthenticationFeature.basic("*****@*****.**", "password");
    client.register(feature);

    CustomerShipping customerShipping =
        new CustomerShipping(
            "Usman", "Chaudhri", "2460 Fulton", "San Francisco", "CA", "94118", "USA", "Y");
    List<CustomerShipping> shipping = new ArrayList<CustomerShipping>();
    shipping.add(customerShipping);
    Customer customer = new Customer("*****@*****.**", "password");

    Customer customerPersisted =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(new StringBuilder("/customer").append("/signup").toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(customer), Customer.class);
    assertThat(customerPersisted.getId()).isNotNull();
  }
  @Test
  public void test_customer_creating_multiple_chat() {
    Client client = JerseyClientBuilder.createClient();
    HttpAuthenticationFeature feature =
        HttpAuthenticationFeature.basic("*****@*****.**", "password");
    client.register(feature);

    CChat cchat1 = new CChat("Talha-Amjad");
    CChat cchat2 = new CChat("Talha-Talha");
    CChat cchat3 = new CChat("Talha-Zunabia");

    List<CChat> cchats = new ArrayList<CChat>();
    cchats.add(cchat1);
    cchats.add(cchat2);
    cchats.add(cchat3);

    Customer customer = new Customer();
    customer.setUsername("*****@*****.**");
    customer.setPassword("password");
    customer.setCchat(cchats);

    Customer customerPersisted =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(new StringBuilder("/customer").append("/signup").toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(customer), Customer.class);

    assertThat(customerPersisted.getId()).isNotNull();
    List<CChat> cchatsPersisted = customerPersisted.getCchat();

    assertThat(cchatsPersisted.get(0).getId()).isNotNull();
    assertThat(cchatsPersisted.get(1).getId()).isNotNull();
    assertThat(cchatsPersisted.get(2).getId()).isNotNull();

    for (CChat cchat : cchatsPersisted) {
      cchats.contains(cchat.getUniqueName());
    }
  }
  @Test
  public void test_add_customer_shipping_to_customer()
      throws JsonParseException, JsonMappingException, IOException {
    Client client = JerseyClientBuilder.createClient();
    HttpAuthenticationFeature feature =
        HttpAuthenticationFeature.basic("*****@*****.**", "password");
    client.register(feature);

    // CREATE NEW USER
    Customer customer = new Customer();
    customer.setUsername("*****@*****.**");
    customer.setPassword("password");
    Customer customerPersisted =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(new StringBuilder("/customer").append("/signup").toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(customer), Customer.class);
    assertThat(customerPersisted.getId()).isNotNull();

    // ADD CCHAT TO USER
    CChat cchat = new CChat("Usman-Asifa");
    CChat persistedCchat =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(
                new StringBuilder("/customer/")
                    .append(customerPersisted.getId())
                    .append("/cchat")
                    .toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(cchat), CChat.class);
    assertThat(persistedCchat.getId()).isNotNull();
    // assertThat(persistedCchat.getUniqueName()).isEqualTo("Usman-Asifa");

    // ADD CCHAT1 TO USER
    CChat cchat1 = new CChat("Usman-Gulgs");
    CChat persistedCchat1 =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(
                new StringBuilder("/customer/")
                    .append(customerPersisted.getId())
                    .append("/cchat")
                    .toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(cchat1), CChat.class);
    assertThat(persistedCchat1.getId()).isNotNull();
    // assertThat(persistedCchat1.getUniqueName()).isEqualTo("Usman-Gulgs");

    // ADD CUSTOMERSHIPPING TO USER
    CustomerShipping customerShipping =
        new CustomerShipping(
            "Usman", "Chaudhri", "2460 Fulton", "San Francisco", "CA", "94118", "USA", "Y");
    CustomerShipping persistedCustomerShipping =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(
                new StringBuilder("/customer/")
                    .append(customerPersisted.getId())
                    .append("/customershipping")
                    .toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(customerShipping), CustomerShipping.class);
    assertThat(persistedCustomerShipping.getId()).isNotNull();
    assertThat(persistedCustomerShipping.getFirstName()).isEqualTo("Usman");
    assertThat(persistedCustomerShipping.getLastName()).isEqualTo("Chaudhri");

    // ADD ORDER TO USER
    Order orderRequest = new Order();
    orderRequest.setProductId("101");
    orderRequest.setProductCategoryId("10");
    orderRequest.setProductImagePath("/image/listing");
    orderRequest.setProductName("shirt");
    orderRequest.setProductPrice("10.99");
    orderRequest.setProductQuantity("5");
    orderRequest.setProductShopId("40");
    orderRequest.setProductSku("SKU_101");
    orderRequest.setStatus("Processed");
    Order persistedOrder =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(new StringBuilder("/orders").toString())
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.json(orderRequest), Order.class);
    assertThat(persistedOrder.getId()).isNotNull();

    // GET PERSISTED CUSTOMER
    Customer getPersistedCustomer =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(new StringBuilder("/customer").append("/login").toString())
            .request(MediaType.APPLICATION_JSON)
            .get(Customer.class);
    assertThat(getPersistedCustomer.getId()).isNotNull();
    assertThat(getPersistedCustomer.getCustomerShipping().get(0).getId()).isNotNull();
    assertThat(getPersistedCustomer.getCustomerShipping().get(0).getFirstName()).isNotNull();

    // lazy load cchat
    JsonNode getPersistedCustomerCchat =
        client
            .target(String.format(REST_PRODUCT_SERVICE_URL, RULE.getLocalPort()))
            .path(
                new StringBuilder("/customer/")
                    .append(getPersistedCustomer.getId())
                    .append("/cchat")
                    .toString())
            .request(MediaType.APPLICATION_JSON)
            .get(JsonNode.class);

    ObjectMapper mapper = new ObjectMapper();
    List<CChat> getPersistedCChats =
        mapper.readValue(
            mapper.treeAsTokens(getPersistedCustomerCchat), new TypeReference<List<CChat>>() {});
    assertThat(getPersistedCChats.get(0).getId()).isNotNull();
    assertThat(getPersistedCChats.get(1).getId()).isNotNull();
  }
 @Override
 protected void subinit() {
   if (username != null && password != null) {
     client = client.register(HttpAuthenticationFeature.basic(username, password));
   }
 }