@Test
  public void testCollectDocId() throws Exception {
    setUpCharacters();
    Planner.Context plannerContext = new Planner.Context(clusterService(), UUID.randomUUID());
    CollectPhase collectNode = createCollectNode(plannerContext, false);

    List<Bucket> results = getBuckets(collectNode);

    assertThat(results.size(), is(2));
    int seenJobSearchContextId = -1;
    for (Bucket rows : results) {
      assertThat(rows.size(), is(1));
      Object docIdCol = rows.iterator().next().get(0);
      assertNotNull(docIdCol);
      assertThat(docIdCol, instanceOf(Long.class));
      long docId = (long) docIdCol;
      // unpack jobSearchContextId and reader doc id from docId
      int jobSearchContextId = (int) (docId >> 32);
      int doc = (int) docId;
      assertThat(doc, is(0));
      assertThat(jobSearchContextId, greaterThan(-1));
      if (seenJobSearchContextId == -1) {
        assertThat(jobSearchContextId, anyOf(is(0), is(1)));
        seenJobSearchContextId = jobSearchContextId;
      } else {
        assertThat(jobSearchContextId, is(seenJobSearchContextId == 0 ? 1 : 0));
      }
    }
  }
  @Test // Uses of JMockit API: 8
  public void useArgumentMatchers() {
    new Expectations() {
      {
        // Using built-in matchers:
        mockedList.get(anyInt);
        result = "element";

        // Using Hamcrest matchers:
        mockedList.get(withArgThat(is(equalTo(5))));
        result = new IllegalArgumentException();
        minTimes = 0;
        mockedList.contains(withArgThat(hasProperty("bytes")));
        result = true;
        mockedList.containsAll(withArgThat(hasSize(2)));
        result = true;
      }
    };

    assertEquals("element", mockedList.get(999));
    assertTrue(mockedList.contains("abc"));
    assertTrue(mockedList.containsAll(asList("a", "b")));

    new Verifications() {
      {
        mockedList.get(anyInt);
      }
    };
  }
  public void testBulk() {
    String[] bulkShardActions = new String[] {BulkAction.NAME + "[s]", BulkAction.NAME + "[s][r]"};
    interceptTransportActions(bulkShardActions);

    List<String> indices = new ArrayList<>();
    BulkRequest bulkRequest = new BulkRequest();
    int numIndexRequests = iterations(1, 10);
    for (int i = 0; i < numIndexRequests; i++) {
      String indexOrAlias = randomIndexOrAlias();
      bulkRequest.add(new IndexRequest(indexOrAlias, "type", "id").source("field", "value"));
      indices.add(indexOrAlias);
    }
    int numDeleteRequests = iterations(1, 10);
    for (int i = 0; i < numDeleteRequests; i++) {
      String indexOrAlias = randomIndexOrAlias();
      bulkRequest.add(new DeleteRequest(indexOrAlias, "type", "id"));
      indices.add(indexOrAlias);
    }
    int numUpdateRequests = iterations(1, 10);
    for (int i = 0; i < numUpdateRequests; i++) {
      String indexOrAlias = randomIndexOrAlias();
      bulkRequest.add(new UpdateRequest(indexOrAlias, "type", "id").doc("field1", "value1"));
      indices.add(indexOrAlias);
    }

    internalCluster().clientNodeClient().bulk(bulkRequest).actionGet();

    clearInterceptedActions();
    assertIndicesSubset(indices, bulkShardActions);
  }
    public void beforeExecute(Task task) {
      assertTrue(planned.contains(task));

      String taskPath = path(task);
      if (taskPath.startsWith(":buildSrc:")) {
        return;
      }

      executedTasks.add(taskPath);
    }
  @Test(expected = UnexpectedInvocation.class) // Uses of JMockit API: 2
  public void verifyThatInvocationsNeverHappenedWhenTheyDid(@Mocked List<String> mockTwo) {
    mockedList.add("one");
    mockTwo.size();

    new FullVerifications() {
      {
        mockedList.add("one");
      }
    };
  }
 private void extractCauses(Throwable failure, List<Throwable> causes) {
   if (failure instanceof MultipleBuildFailures) {
     MultipleBuildFailures exception = (MultipleBuildFailures) failure;
     for (Throwable componentFailure : exception.getCauses()) {
       extractCauses(componentFailure, causes);
     }
   } else if (failure instanceof LocationAwareException) {
     causes.addAll(((LocationAwareException) failure).getReportableCauses());
   } else {
     causes.add(failure.getCause());
   }
 }
  @Test(expected = UnexpectedInvocation.class) // Uses of JMockit API: 1
  public void verifyAllInvocationsInOrderWhenMoreOfThemHappen() {
    mockedList.add("one");
    mockedList.add("two");
    mockedList.size();

    new FullVerificationsInOrder() {
      {
        mockedList.add("one");
        mockedList.add("two");
      }
    };
  }
 private static List<TransportRequest> consumeTransportRequests(String action) {
   List<TransportRequest> requests = new ArrayList<>();
   Iterable<TransportService> transportServices =
       internalCluster().getInstances(TransportService.class);
   for (TransportService transportService : transportServices) {
     List<TransportRequest> transportRequests =
         ((InterceptingTransportService) transportService).consumeRequests(action);
     if (transportRequests != null) {
       requests.addAll(transportRequests);
     }
   }
   return requests;
 }
  @Test // Uses of JMockit API: 1
  public void verifyAllInvocationsInOrder() {
    mockedList.add("one");
    mockedList.size();
    mockedList.add("two");

    new FullVerificationsInOrder() {
      {
        mockedList.add("one");
        mockedList.size();
        mockedList.add("two");
      }
    };
  }
  @Test // Uses of JMockit API: 3
  public void verifyInOrder(
      @Mocked final List<String> firstMock, @Mocked final List<String> secondMock) {
    // Using mocks:
    firstMock.add("was called first");
    secondMock.add("was called second");

    new VerificationsInOrder() {
      {
        // Verifies that firstMock was called before secondMock:
        firstMock.add("was called first");
        secondMock.add("was called second");
      }
    };
  }
 private static void assertIndicesSubset(List<String> indices, String... actions) {
   // indices returned by each bulk shard request need to be a subset of the original indices
   for (String action : actions) {
     List<TransportRequest> requests = consumeTransportRequests(action);
     assertThat(
         "no internal requests intercepted for action [" + action + "]",
         requests.size(),
         greaterThan(0));
     for (TransportRequest internalRequest : requests) {
       assertThat(internalRequest, instanceOf(IndicesRequest.class));
       for (String index : ((IndicesRequest) internalRequest).indices()) {
         assertThat(indices, hasItem(index));
       }
     }
   }
 }
 @Override
 public void messageReceived(T request, TransportChannel channel) throws Exception {
   synchronized (InterceptingTransportService.this) {
     if (actions.contains(action)) {
       List<TransportRequest> requestList = requests.get(action);
       if (requestList == null) {
         requestList = new ArrayList<>();
         requestList.add(request);
         requests.put(action, requestList);
       } else {
         requestList.add(request);
       }
     }
   }
   requestHandler.messageReceived(request, channel);
 }
  @Test // Uses of JMockit API: 3
  public void returningElementsFromAList() {
    final List<String> list = asList("a", "b", "c");

    new Expectations() {
      {
        mockedList.get(anyInt);
        result = list;
      }
    };

    assertEquals("a", mockedList.get(0));
    assertEquals("b", mockedList.get(1));
    assertEquals("c", mockedList.get(2));
    assertEquals("c", mockedList.get(3));
  }
  @Test
  public void dynamicUpdates() throws Exception {
    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(
            settingsBuilder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0))
        .execute()
        .actionGet();
    client()
        .admin()
        .cluster()
        .prepareHealth()
        .setWaitForEvents(Priority.LANGUID)
        .setWaitForGreenStatus()
        .execute()
        .actionGet();

    int recCount = randomIntBetween(200, 600);
    int numberOfTypes = randomIntBetween(1, 5);
    List<IndexRequestBuilder> indexRequests = Lists.newArrayList();
    for (int rec = 0; rec < recCount; rec++) {
      String type = "type" + (rec % numberOfTypes);
      String fieldName = "field_" + type + "_" + rec;
      indexRequests.add(
          client()
              .prepareIndex("test", type, Integer.toString(rec))
              .setSource(fieldName, "some_value"));
    }
    indexRandom(true, indexRequests);

    logger.info("checking all the documents are there");
    RefreshResponse refreshResponse =
        client().admin().indices().prepareRefresh().execute().actionGet();
    assertThat(refreshResponse.getFailedShards(), equalTo(0));
    CountResponse response = client().prepareCount("test").execute().actionGet();
    assertThat(response.getCount(), equalTo((long) recCount));

    logger.info("checking all the fields are in the mappings");

    for (int rec = 0; rec < recCount; rec++) {
      String type = "type" + (rec % numberOfTypes);
      String fieldName = "field_" + type + "_" + rec;
      assertConcreteMappingsOnAll("test", type, fieldName);
    }
  }
  public void testMultiGet() {
    String multiGetShardAction = MultiGetAction.NAME + "[shard][s]";
    interceptTransportActions(multiGetShardAction);

    List<String> indices = new ArrayList<>();
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    int numDocs = iterations(1, 30);
    for (int i = 0; i < numDocs; i++) {
      String indexOrAlias = randomIndexOrAlias();
      multiGetRequest.add(indexOrAlias, "type", Integer.toString(i));
      indices.add(indexOrAlias);
    }
    internalCluster().clientNodeClient().multiGet(multiGetRequest).actionGet();

    clearInterceptedActions();
    assertIndicesSubset(indices, multiGetShardAction);
  }
 private String[] randomIndicesOrAliases() {
   int count = randomIntBetween(1, indices.size() * 2); // every index has an alias
   String[] indices = new String[count];
   for (int i = 0; i < count; i++) {
     indices[i] = randomIndexOrAlias();
   }
   return indices;
 }
  @Test
  public void reads_a_list_of_entries() throws Exception {
    ZipContents zipContents = new ZipContents(new File("./src/test/resource/test.zip"));

    try {
      zipContents.open();

      List<ZipContentsEntry> entries = zipContents.entries();

      assertThat(entries.size(), is(6));
      ZipContentsEntry firstEntry = entries.get(0);
      assertThat(firstEntry.directory(), is("/"));
      assertThat(firstEntry.name(), is("file_1.txt"));
      assertThat(firstEntry.type(), is("txt"));
    } finally {
      zipContents.close();
    }
  }
  @Test
  public void testRetrieveAllFunds() {
    FundsHelper fh =
        FundsHelper.create(Utils.randomNameGenerator("", 10))
            .externalId(Utils.randomNameGenerator("fund-", 5))
            .build();
    String jsonData = fh.toJSON();

    final Long fundID = createFund(jsonData, this.requestSpec, this.statusOkResponseSpec);
    Assert.assertNotNull(fundID);

    List<FundsHelper> fhList =
        FundsResourceHandler.retrieveAllFunds(this.requestSpec, this.statusOkResponseSpec);

    Assert.assertNotNull(fhList);
    Assert.assertThat(fhList.size(), greaterThanOrEqualTo(1));
    Assert.assertThat(fhList, hasItem(fh));
  }
示例#19
0
  @Test
  public void testNewGetPaymentsApiMethods() throws Exception {
    JbillingAPI api = JbillingAPIFactory.getAPI();

    // Create a user with balance $1.00
    UserWS user = com.sapienter.jbilling.server.user.WSTest.createUser(true, null, null);

    List<PaymentWS> payments = new ArrayList<PaymentWS>();

    for (int i = 0; i < 5; i++) {
      payments.add(
          createPaymentWS(
              user.getUserId(), new DateTime().plusMonths(i).toDate(), String.valueOf(i)));
    }

    // get two latest payments except the latest one.
    Integer[] paymentsId = api.getLastPaymentsPage(user.getUserId(), 2, 1);

    assertEquals(2, paymentsId.length);

    assertEquals("3", api.getPayment(paymentsId[0]).getPaymentNotes());
    assertEquals("2", api.getPayment(paymentsId[1]).getPaymentNotes());

    // get the payments between next month and four months from now.
    Integer[] paymentsId2 =
        api.getPaymentsByDate(
            user.getUserId(),
            new DateTime().plusDays(1).toDate(),
            new DateTime().plusMonths(3).plusDays(1).toDate());

    assertEquals(3, paymentsId2.length);

    assertEquals("3", api.getPayment(paymentsId2[0]).getPaymentNotes());
    assertEquals("2", api.getPayment(paymentsId2[1]).getPaymentNotes());
    assertEquals("1", api.getPayment(paymentsId2[2]).getPaymentNotes());

    // Delete orders
    for (PaymentWS payment : payments) {
      api.deletePayment(payment.getId());
    }
    // Delete user
    api.deleteUser(user.getUserId());
  }
  @Test
  public void testBenchmarkWithErrors() {
    List<SearchRequest> reqList = new ArrayList<>();
    int numQueries = scaledRandomIntBetween(20, 100);
    int numErrors = scaledRandomIntBetween(1, numQueries);
    final boolean containsFatal = randomBoolean();
    if (containsFatal) {
      ScriptScoreFunctionBuilder scriptFunction =
          scriptFunction("DOES NOT COMPILE - fails on any shard");
      SearchRequest searchRequest =
          searchRequest()
              .source(
                  searchSource()
                      .query(functionScoreQuery(FilterBuilders.matchAllFilter(), scriptFunction)));
      reqList.add(searchRequest);
    }
    for (int i = 0; reqList.size() < numErrors; i++) {
      ScriptScoreFunctionBuilder scriptFunction = scriptFunction("throw new RuntimeException();");
      SearchRequest searchRequest =
          searchRequest()
              .source(
                  searchSource()
                      .query(functionScoreQuery(FilterBuilders.matchAllFilter(), scriptFunction)));
      reqList.add(searchRequest);
    }
    logger.info("--> run with [{}] errors ", numErrors);
    for (int i = 0; reqList.size() < numQueries; i++) {

      reqList.add(BenchmarkTestUtil.randomSearch(client(), indices));
    }
    Collections.shuffle(reqList, getRandom());

    final BenchmarkRequest request =
        BenchmarkTestUtil.randomRequest(
            client(),
            indices,
            numExecutorNodes,
            competitionSettingsMap,
            reqList.toArray(new SearchRequest[0]));
    logger.info(
        "--> Submitting benchmark - competitors [{}] iterations [{}]",
        request.competitors().size(),
        request.settings().iterations());
    final BenchmarkResponse response = client().bench(request).actionGet();

    assertThat(response, notNullValue());
    if (response.hasErrors() || containsFatal) {
      assertThat(response.state(), equalTo(BenchmarkResponse.State.FAILED));
    } else {
      assertThat(response.state(), equalTo(BenchmarkResponse.State.COMPLETE));
      for (CompetitionResult result : response.competitionResults().values()) {
        assertThat(result.nodeResults().size(), equalTo(numExecutorNodes));
        validateCompetitionResult(
            result, competitionSettingsMap.get(result.competitionName()), true);
      }
    }
    assertThat(response.benchmarkName(), equalTo(BENCHMARK_NAME));
  }
示例#21
0
  private Integer getOrCreateSuspendedStatus(JbillingAPI api) {
    List<AgeingWS> steps = Arrays.asList(api.getAgeingConfiguration(LANGUAGE_ID));

    for (AgeingWS step : steps) {
      if (step.getSuspended().booleanValue()) {
        return step.getStatusId();
      }
    }

    AgeingWS suspendStep = new AgeingWS();
    suspendStep.setSuspended(Boolean.TRUE);
    suspendStep.setDays(Integer.valueOf(180));
    suspendStep.setStatusStr("Ageing Step 180");
    suspendStep.setFailedLoginMessage("You are suspended");
    suspendStep.setWelcomeMessage("Welcome");
    steps.add(suspendStep);
    api.saveAgeingConfiguration(steps.toArray(new AgeingWS[steps.size()]), LANGUAGE_ID);
    return getOrCreateOrderChangeStatusApply(api);
  }
 @Before
 public void setup() {
   int numIndices = iterations(1, 5);
   for (int i = 0; i < numIndices; i++) {
     indices.add("test" + i);
   }
   for (String index : indices) {
     assertAcked(prepareCreate(index).addAlias(new Alias(index + "-alias")));
   }
   ensureGreen();
 }
  @Test(expected = RuntimeException.class) // Uses of JMockit API: 2
  public void stubVoidMethodsWithExceptions() {
    new Expectations() {
      {
        // void/non-void methods are handled the same way, with a consistent API:
        mockedList.clear();
        result = new RuntimeException();
      }
    };

    mockedList.clear();
  }
  @Test(expected = MissingInvocation.class) // Uses of JMockit API: 1
  public void verifyAllInvocationsInOrderWithOutOfOrderVerifications() {
    mockedList.add("one");
    mockedList.add("two");

    new FullVerificationsInOrder() {
      {
        mockedList.add("two");
        mockedList.add("one");
      }
    };
  }
  @Test // Uses of JMockit API: 8
  public void verifyNumberOfInvocations() {
    // Using mock:
    mockedList.add("once");

    mockedList.add("twice");
    mockedList.add("twice");

    mockedList.add("three times");
    mockedList.add("three times");
    mockedList.add("three times");

    new Verifications() {
      {
        // Following two verifications work exactly the same:
        mockedList.add("once"); // minTimes == 1 is the default
        mockedList.add("once");
        times = 1;

        // Verifies exact number of invocations:
        mockedList.add("twice");
        times = 2;
        mockedList.add("three times");
        times = 3;

        // Verifies no invocations occurred:
        mockedList.add("never happened");
        times = 0;

        // Verifies min/max number of invocations:
        mockedList.add("three times");
        minTimes = 1;
        mockedList.add("three times");
        minTimes = 2;
        mockedList.add("three times");
        maxTimes = 5;
      }
    };
  }
 private static void assertSameIndices(
     IndicesRequest originalRequest, boolean optional, String... actions) {
   for (String action : actions) {
     List<TransportRequest> requests = consumeTransportRequests(action);
     if (!optional) {
       assertThat(
           "no internal requests intercepted for action [" + action + "]",
           requests.size(),
           greaterThan(0));
     }
     for (TransportRequest internalRequest : requests) {
       assertThat(internalRequest, instanceOf(IndicesRequest.class));
       assertThat(
           internalRequest.getClass().getName(),
           ((IndicesRequest) internalRequest).indices(),
           equalTo(originalRequest.indices()));
       assertThat(
           ((IndicesRequest) internalRequest).indicesOptions(),
           equalTo(originalRequest.indicesOptions()));
     }
   }
 }
  private static void duelFieldDataGeoPoint(
      Random random,
      AtomicReaderContext context,
      IndexGeoPointFieldData left,
      IndexGeoPointFieldData right,
      Distance precision)
      throws Exception {
    AtomicGeoPointFieldData leftData =
        random.nextBoolean() ? left.load(context) : left.loadDirect(context);
    AtomicGeoPointFieldData rightData =
        random.nextBoolean() ? right.load(context) : right.loadDirect(context);

    int numDocs = context.reader().maxDoc();
    MultiGeoPointValues leftValues = leftData.getGeoPointValues();
    MultiGeoPointValues rightValues = rightData.getGeoPointValues();
    for (int i = 0; i < numDocs; ++i) {
      leftValues.setDocument(i);
      final int numValues = leftValues.count();
      rightValues.setDocument(i);
      ;
      assertEquals(numValues, rightValues.count());
      List<GeoPoint> leftPoints = Lists.newArrayList();
      List<GeoPoint> rightPoints = Lists.newArrayList();
      for (int j = 0; j < numValues; ++j) {
        GeoPoint l = leftValues.valueAt(j);
        leftPoints.add(new GeoPoint(l.getLat(), l.getLon()));
        GeoPoint r = rightValues.valueAt(j);
        rightPoints.add(new GeoPoint(r.getLat(), r.getLon()));
      }
      for (GeoPoint l : leftPoints) {
        assertTrue(
            "Couldn't find " + l + " among " + rightPoints, contains(l, rightPoints, precision));
      }
      for (GeoPoint r : rightPoints) {
        assertTrue(
            "Couldn't find " + r + " among " + leftPoints, contains(r, leftPoints, precision));
      }
    }
  }
  private List<Bucket> getBuckets(CollectPhase collectNode)
      throws InterruptedException, java.util.concurrent.ExecutionException {
    List<Bucket> results = new ArrayList<>();
    for (String nodeName : internalCluster().getNodeNames()) {
      ContextPreparer contextPreparer =
          internalCluster().getInstance(ContextPreparer.class, nodeName);
      JobContextService contextService =
          internalCluster().getInstance(JobContextService.class, nodeName);

      JobExecutionContext.Builder builder = contextService.newBuilder(collectNode.jobId());
      ListenableFuture<Bucket> future =
          contextPreparer.prepare(
              collectNode.jobId(),
              NodeOperation.withDownstream(collectNode, mock(ExecutionPhase.class)),
              builder);
      assert future != null;

      JobExecutionContext context = contextService.createContext(builder);
      context.start();
      results.add(future.get());
    }
    return results;
  }
  @Test // Uses of JMockit API: 3
  public void stubAndVerifyInvocationWithoutRepeatingItInExpectationAndVerificationBlocks() {
    new Expectations() {
      {
        // Notice that this can't be done in Mockito, which requires the repetition of
        // "mockedList.get(0);" in the verification phase.
        mockedList.get(0);
        result = "first";
        times = 1;
      }
    };

    assertEquals("first", mockedList.get(0));
  }
示例#30
0
  public OrderWS buildOrder(int userId, List<Integer> itemIds, BigDecimal linePrice) {
    OrderWS order = new OrderWS();
    order.setUserId(userId);
    order.setBillingTypeId(Constants.ORDER_BILLING_POST_PAID);
    order.setPeriod(ORDER_PERIOD_ONCE); // once
    order.setCurrencyId(CURRENCY_USD);
    order.setActiveSince(new Date());
    order.setProrateFlag(Boolean.FALSE);

    ArrayList<OrderLineWS> lines = new ArrayList<OrderLineWS>(itemIds.size());
    for (int i = 0; i < itemIds.size(); i++) {
      OrderLineWS nextLine = new OrderLineWS();
      nextLine.setTypeId(Constants.ORDER_LINE_TYPE_ITEM);
      nextLine.setDescription("Order line: " + i);
      nextLine.setItemId(itemIds.get(i));
      nextLine.setQuantity(1);
      nextLine.setPrice(linePrice);
      nextLine.setAmount(nextLine.getQuantityAsDecimal().multiply(linePrice));

      lines.add(nextLine);
    }
    order.setOrderLines(lines.toArray(new OrderLineWS[lines.size()]));
    return order;
  }