Esempio n. 1
0
  @Test
  public void testHttpServer() throws IOException, ExecutionException, InterruptedException {
    final CountDownLatch lock = new CountDownLatch(CONCURRENT_REQUESTS);

    for (int i = 0; i < CONCURRENT_REQUESTS; i++) {
      TrackingDataTO trackingDataTO = bootstrapTrackingData();
      String requestAsJson = gson.toJson(trackingDataTO);

      AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
      asyncHttpClient
          .preparePost("http://localhost:8080/" + trackingDataTO.getType().getLabel())
          .setBody(requestAsJson)
          .execute(
              new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                  ResultTO resultTo = gson.fromJson(response.getResponseBody(), ResultTO.class);
                  if (resultTo.getCode().equals(ResultCode.SUCCESS)) {
                    lock.countDown();
                  }
                  return response;
                }

                @Override
                public void onThrowable(Throwable t) {
                  log.error(t.toString());
                }
              });
    }

    lock.await(TIMEOUT, TimeUnit.MILLISECONDS);
    assertEquals(0, lock.getCount());
  }
 /**
  * Accepts JSON object and submits it to the JMS queue for processing
  *
  * @param requestAsJson JSON formatted string. See {@link workshop.dto.TrackingDataTO}
  * @return ResultTO to be passed on to the HTTP client with possible errorcodes
  */
 public ResultTO submitPayload(String requestAsJson) {
   ResultTO resultTO;
   if (trackingType == TrackingType.UNKNOWN) {
     resultTO = new ResultTO(ResultCode.UNKNOWN_ROUTE);
   } else {
     try {
       TrackingDataTO trackingDataTO = gson.fromJson(requestAsJson, TrackingDataTO.class);
       trackingDataTO.setType(trackingType);
       trackingDataTO.setTimestamp(DateTime.now().toDate());
       queueSend.send(trackingDataTO);
       resultTO = new ResultTO(ResultCode.SUCCESS);
     } catch (JsonSyntaxException e) {
       resultTO = new ResultTO(ResultCode.MALFORMED_INPUT);
     } catch (JMSException e) {
       resultTO = new ResultTO(ResultCode.INTERNAL_ERROR);
     }
   }
   return resultTO;
 }
Esempio n. 3
0
 private TrackingDataTO bootstrapTrackingData() {
   TrackingDataTO trackingDataTO = new TrackingDataTO();
   trackingDataTO.setType(randomRoute());
   trackingDataTO.setUpdatetime(new Date(random.nextLong()).toString());
   trackingDataTO.setCarrier(random.nextInt());
   trackingDataTO.setLatitude(BigDecimal.valueOf(random.nextLong()));
   trackingDataTO.setLongitude(BigDecimal.valueOf(random.nextLong()));
   trackingDataTO.setSpeed(BigDecimal.valueOf(random.nextLong()));
   trackingDataTO.setHeading(random.nextInt());
   trackingDataTO.setSatellites(random.nextInt());
   trackingDataTO.setFixStatus(random.nextInt());
   trackingDataTO.setCarrier(random.nextInt());
   trackingDataTO.setRssi(random.nextInt());
   trackingDataTO.setCommState(random.nextInt());
   trackingDataTO.setHdop(BigDecimal.valueOf(random.nextLong()));
   trackingDataTO.setInputs(random.nextInt());
   trackingDataTO.setUnitStatus(random.nextInt());
   trackingDataTO.setEventIndex(getNextCounter());
   trackingDataTO.setEventCode(random.nextInt());
   trackingDataTO.setSpare(random.nextInt());
   List<Integer> accum = bootstrapAccumsData();
   trackingDataTO.setAccums(accum.size());
   trackingDataTO.setAccum(accum);
   return trackingDataTO;
 }