Example #1
0
  public Frete calculaFrete(Pedido p) throws Throwable {
    Frete frete = null;

    URL obj =
        new URL(
            "http://localhost:8090/calcularFrete?peso="
                + p.getPeso()
                + "&largura="
                + p.getLargura()
                + "&altura="
                + p.getAltura()
                + "&comprimento="
                + p.getComprimento()
                + "&tipoEntrega="
                + p.getTipoEntrega()
                + "&cep="
                + p.getCep());
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");

    int responseCode = con.getResponseCode();

    if (responseCode == 200) {
      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();

      while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
      }
      in.close();

      JSONObject json = (JSONObject) new JSONParser().parse(response.toString());

      String valorResp = json.get("Valor").toString();
      String tempoEntregaResp = json.get("PrazoEntrega").toString();

      frete = new Frete();
      frete.setTempoEntrega(Integer.valueOf(tempoEntregaResp));
      frete.setValor(Double.valueOf(valorResp));

      dao.saveDadosDeEntrega(frete.getValor(), frete.getTempoEntrega());
    } else if (responseCode == 400) {
      throw new Throwable("The zipcode is not valid");
    } else if (responseCode == 500) {
      throw new Throwable("CorreioServices is offline right now");
    }
    return frete;
  }
Example #2
0
  @When("^I ask to calculate shipping$")
  public void I_ask_to_calculate_shipping() {
    int valor = 0;
    int tempoEntrega = 0;

    if (correioServicesStatus) {
      if (correio.validaCep(pedido.getCep())) {
        if (pedido.getTipoEntrega().equals("PAC")) {
          valor = 5;
          tempoEntrega = 5;
        } else if (pedido.getTipoEntrega().equals("SEDEX")) {
          valor = 20;
          tempoEntrega = 3;
        } else if (pedido.getTipoEntrega().equals("SEDEX10")) {
          valor = 100;
          tempoEntrega = 1;
        }

        stubFor(
            get(urlEqualTo(
                    "/calcularFrete?peso="
                        + pedido.getPeso()
                        + "&largura="
                        + pedido.getLargura()
                        + "&altura="
                        + pedido.getAltura()
                        + "&comprimento="
                        + pedido.getComprimento()
                        + "&tipoEntrega="
                        + pedido.getTipoEntrega()
                        + "&cep="
                        + pedido.getCep()))
                .willReturn(
                    aResponse()
                        .withHeader("Content-Type", "text/plain")
                        .withBody(
                            "{\"Valor\":\""
                                + valor
                                + "\",\"PrazoEntrega\":\""
                                + tempoEntrega
                                + "\"}")));
      } else {
        stubFor(
            get(urlEqualTo(
                    "/calcularFrete?peso="
                        + pedido.getPeso()
                        + "&largura="
                        + pedido.getLargura()
                        + "&altura="
                        + pedido.getAltura()
                        + "&comprimento="
                        + pedido.getComprimento()
                        + "&tipoEntrega="
                        + pedido.getTipoEntrega()
                        + "&cep="
                        + pedido.getCep()))
                .willReturn(aResponse().withStatus(400)));
      }
    } else {
      stubFor(
          get(urlEqualTo(
                  "/calcularFrete?peso="
                      + pedido.getPeso()
                      + "&largura="
                      + pedido.getLargura()
                      + "&altura="
                      + pedido.getAltura()
                      + "&comprimento="
                      + pedido.getComprimento()
                      + "&tipoEntrega="
                      + pedido.getTipoEntrega()
                      + "&cep="
                      + pedido.getCep()))
              .willReturn(aResponse().withStatus(500)));
    }
    try {
      frete = correio.calculaFrete(pedido);
    } catch (Throwable e) {
      throwable = e;
    }
  }
Example #3
0
 @Given("^zipcode is \"(.*)\"$")
 public void zipcode_is(String arg1) {
   pedido.setCep(arg1);
 }
Example #4
0
 @Given("^shipping type is \"(.*)\"$")
 public void shipping_type_is(String arg1) {
   pedido.setTipoEntrega(arg1);
 }
Example #5
0
 @Given("^product length is (\\d+) cm$")
 public void product_length_is__cm(int param1) {
   pedido.setComprimento(param1);
 }
Example #6
0
 @Given("^product height is (\\d+) cm$")
 public void product_height_is__cm(int param1) {
   pedido.setAltura(param1);
 }
Example #7
0
 @Given("^product width is (\\d+) cm$")
 public void product_width_is__cm(int param1) {
   pedido.setLargura(param1);
 }
Example #8
0
 @Given("^product weight is (\\d+) g$")
 public void product_weight_is__g(int param1) {
   pedido.setPeso(param1);
 }