Example #1
0
  @Test
  public void testWrongRouting() throws Exception {

    expectedException.expect(UnhandledServerException.class);
    expectedException.expectMessage("unsupported routing");

    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(),
            0,
            "wrong",
            new Routing(
                TreeMapBuilder.<String, Map<String, List<Integer>>>newMapBuilder()
                    .put(
                        "bla",
                        TreeMapBuilder.<String, List<Integer>>newMapBuilder()
                            .put("my_index", Arrays.asList(1))
                            .put("my_index", Arrays.asList(1))
                            .map())
                    .map()),
            ImmutableList.<Symbol>of(),
            EMPTY_PROJECTIONS);
    collectNode.maxRowGranularity(RowGranularity.DOC);
    operation.collect(collectNode, new CollectingProjector(), null);
  }
  @Test
  public void testPutMappingsWithBlocks() throws Exception {
    createIndex("test");
    ensureGreen();

    for (String block : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE)) {
      try {
        enableIndexBlock("test", block);
        assertAcked(
            client()
                .admin()
                .indices()
                .preparePutMapping("test")
                .setType("doc")
                .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}"));
      } finally {
        disableIndexBlock("test", block);
      }
    }

    for (String block : Arrays.asList(SETTING_READ_ONLY, SETTING_BLOCKS_METADATA)) {
      try {
        enableIndexBlock("test", block);
        assertBlocked(
            client()
                .admin()
                .indices()
                .preparePutMapping("test")
                .setType("doc")
                .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}"));
      } finally {
        disableIndexBlock("test", block);
      }
    }
  }
 public ExecutionResult assertTasksNotSkipped(String... taskPaths) {
   Set<String> expected = new HashSet<String>(Arrays.asList(taskPaths));
   Set<String> notSkipped = getNotSkippedTasks();
   assertThat(notSkipped, equalTo(expected));
   outputResult.assertTasksNotSkipped(taskPaths);
   return this;
 }
Example #4
0
  /** Test payInvoice(invoice) API call. */
  @Test
  public void testPayInvoice() {
    // setup
    UserWS user = buildUser(PRANCING_PONY_ACCOUNT_TYPE);
    user.setId(api.createUser(user));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    // testing
    System.out.println("Getting an invoice paid, and validating the payment.");
    OrderWS order = buildOrder(user.getId(), Arrays.asList(item.getId()), new BigDecimal("3.45"));
    Integer invoiceId =
        api.createOrderAndInvoice(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));
    Integer orderId = api.getInvoiceWS(invoiceId).getOrders()[0];
    PaymentAuthorizationDTOEx auth = api.payInvoice(invoiceId);
    assertNotNull("auth can not be null", auth);
    PaymentWS payment = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", payment);
    assertNotNull("auth in payment can not be null", payment.getAuthorizationId());

    // cleanup
    api.deletePayment(auth.getPaymentId());
    api.deleteInvoice(invoiceId);
    api.deleteOrder(orderId);
    api.deleteItem(item.getId());
    api.deleteItemCategory(itemType.getId());
    api.deleteUser(user.getId());
  }
Example #5
0
 private Routing shardRouting(final Integer... shardIds) {
   return new Routing(
       TreeMapBuilder.<String, Map<String, List<Integer>>>newMapBuilder()
           .put(
               TEST_NODE_ID,
               TreeMapBuilder.<String, List<Integer>>newMapBuilder()
                   .put(TEST_TABLE_NAME, Arrays.asList(shardIds))
                   .map())
           .map());
 }
Example #6
0
 public void assertDeleteContent(Store store, DirectoryService service) throws IOException {
   store.deleteContent();
   assertThat(
       Arrays.toString(store.directory().listAll()),
       store.directory().listAll().length,
       equalTo(0));
   assertThat(store.stats().sizeInBytes(), equalTo(0l));
   for (Directory dir : service.build()) {
     assertThat(dir.listAll().length, equalTo(0));
   }
 }
Example #7
0
  @Test
  public void selectFieldWithSpace()
      throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    String[] arr = new String[] {"test field"};
    Set expectedSource = new HashSet(Arrays.asList(arr));

    SearchHits response = query(String.format("SELECT `test field` FROM %s/phrase_2", TEST_INDEX));
    SearchHit[] hits = response.getHits();
    for (SearchHit hit : hits) {
      Assert.assertEquals(expectedSource, hit.getSource().keySet());
    }
  }
  @Test
  @TestLogging(value = "cluster.service:TRACE")
  public void testDeleteCreateInOneBulk() throws Exception {
    internalCluster()
        .startNodesAsync(
            2, Settings.builder().put(DiscoveryModule.DISCOVERY_TYPE_KEY, "zen").build())
        .get();
    assertFalse(client().admin().cluster().prepareHealth().setWaitForNodes("2").get().isTimedOut());
    prepareCreate("test")
        .setSettings(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS, true)
        .addMapping("type")
        .get();
    ensureGreen("test");

    // now that the cluster is stable, remove publishing timeout
    assertAcked(
        client()
            .admin()
            .cluster()
            .prepareUpdateSettings()
            .setTransientSettings(Settings.builder().put(DiscoverySettings.PUBLISH_TIMEOUT, "0")));

    Set<String> nodes = new HashSet<>(Arrays.asList(internalCluster().getNodeNames()));
    nodes.remove(internalCluster().getMasterName());

    // block none master node.
    BlockClusterStateProcessing disruption =
        new BlockClusterStateProcessing(nodes.iterator().next(), getRandom());
    internalCluster().setDisruptionScheme(disruption);
    logger.info("--> indexing a doc");
    index("test", "type", "1");
    refresh();
    disruption.startDisrupting();
    logger.info("--> delete index and recreate it");
    assertFalse(
        client()
            .admin()
            .indices()
            .prepareDelete("test")
            .setTimeout("200ms")
            .get()
            .isAcknowledged());
    assertFalse(
        prepareCreate("test")
            .setTimeout("200ms")
            .setSettings(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS, true)
            .get()
            .isAcknowledged());
    logger.info("--> letting cluster proceed");
    disruption.stopDisrupting();
    ensureGreen(TimeValue.timeValueMinutes(30), "test");
    assertHitCount(client().prepareSearch("test").get(), 0);
  }
Example #9
0
  /**
   * Test for BlacklistUserStatusTask. When a user's status moves to suspended or higher, the user
   * and all their information is added to the blacklist.
   */
  @Test(enabled = false)
  public void testBlacklistUserStatus() {
    UserWS user =
        buildUser(
            PRANCING_PONY_ACCOUNT_TYPE, "BlackListFirst", "BlackListSecond", "4916347258194745");
    user.setId(api.createUser(user));

    // expected filter response messages
    String[] messages = new String[3];
    messages[0] = "User id is blacklisted.";
    messages[1] = "Name is blacklisted.";
    messages[2] = "Credit card number is blacklisted.";

    //	    TODO: for now we do not test for these three
    //        messages[3] = "Address is blacklisted.";
    //        messages[4] = "IP address is blacklisted.";
    //        messages[5] = "Phone number is blacklisted.";

    // check that a user isn't blacklisted
    user = api.getUserWS(user.getId());
    // CXF returns null
    if (user.getBlacklistMatches() != null) {
      assertTrue("User shouldn't be blacklisted yet", user.getBlacklistMatches().length == 0);
    }

    // change their status to suspended
    user.setStatusId(STATUS_SUSPENDED);
    user.setPassword(null);
    api.updateUser(user);

    // check all their records are now blacklisted
    user = api.getUserWS(user.getId());
    assertEquals(
        "User records should be blacklisted.",
        Arrays.toString(messages),
        Arrays.toString(user.getBlacklistMatches()));

    // cleanup
    api.deleteUser(user.getId());
  }
Example #10
0
  @Test
  public void selectSpecificFields()
      throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    String[] arr = new String[] {"age", "account_number"};
    Set expectedSource = new HashSet(Arrays.asList(arr));

    SearchHits response =
        query(String.format("SELECT age, account_number FROM %s/account", TEST_INDEX));
    SearchHit[] hits = response.getHits();
    for (SearchHit hit : hits) {
      Assert.assertEquals(expectedSource, hit.getSource().keySet());
    }
  }
  @Test
  public void testAddUsefulHeaders() throws InterruptedException {
    // take the existing headers into account to make sure this test runs with tests.iters>1 as the
    // list is static
    final Set<String> headers = new HashSet<>();
    headers.addAll(BaseRestHandler.usefulHeaders());
    int iterations = randomIntBetween(1, 5);

    ExecutorService executorService = Executors.newFixedThreadPool(iterations);
    for (int i = 0; i < iterations; i++) {
      int headersCount = randomInt(10);
      final Set<String> newHeaders = new HashSet<>();
      for (int j = 0; j < headersCount; j++) {
        String usefulHeader = randomRealisticUnicodeOfLengthBetween(1, 30);
        newHeaders.add(usefulHeader);
        headers.add(usefulHeader);
      }

      executorService.submit(
          new Runnable() {
            @Override
            public void run() {
              BaseRestHandler.addUsefulHeaders(newHeaders.toArray(new String[newHeaders.size()]));
            }
          });
    }

    executorService.shutdown();
    assertThat(executorService.awaitTermination(1, TimeUnit.SECONDS), equalTo(true));
    String[] usefulHeaders =
        BaseRestHandler.usefulHeaders().toArray(new String[BaseRestHandler.usefulHeaders().size()]);
    assertThat(usefulHeaders.length, equalTo(headers.size()));

    Arrays.sort(usefulHeaders);
    String[] headersArray = new String[headers.size()];
    headersArray = headers.toArray(headersArray);
    Arrays.sort(headersArray);
    assertThat(usefulHeaders, equalTo(headersArray));
  }
  public void testDuelGlobalOrdinals() throws Exception {
    Random random = getRandom();
    final int numDocs = scaledRandomIntBetween(10, 1000);
    final int numValues = scaledRandomIntBetween(10, 500);
    final String[] values = new String[numValues];
    for (int i = 0; i < numValues; ++i) {
      values[i] = new String(RandomStrings.randomAsciiOfLength(random, 10));
    }
    for (int i = 0; i < numDocs; i++) {
      Document d = new Document();
      final int numVals = randomInt(3);
      for (int j = 0; j < numVals; ++j) {
        final String value = RandomPicks.randomFrom(random, Arrays.asList(values));
        d.add(new StringField("string", value, Field.Store.NO));
        d.add(new SortedSetDocValuesField("bytes", new BytesRef(value)));
      }
      writer.addDocument(d);
      if (randomInt(10) == 0) {
        refreshReader();
      }
    }
    refreshReader();

    Map<FieldDataType, Type> typeMap = new HashMap<FieldDataType, DuelFieldDataTests.Type>();
    typeMap.put(
        new FieldDataType("string", ImmutableSettings.builder().put("format", "fst")), Type.Bytes);
    typeMap.put(
        new FieldDataType("string", ImmutableSettings.builder().put("format", "paged_bytes")),
        Type.Bytes);
    typeMap.put(
        new FieldDataType("string", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Bytes);

    for (Map.Entry<FieldDataType, Type> entry : typeMap.entrySet()) {
      ifdService.clear();
      IndexOrdinalsFieldData fieldData =
          getForField(entry.getKey(), entry.getValue().name().toLowerCase(Locale.ROOT));
      RandomAccessOrds left = fieldData.load(readerContext).getOrdinalsValues();
      fieldData.clear();
      RandomAccessOrds right =
          fieldData
              .loadGlobal(topLevelReader)
              .load(topLevelReader.leaves().get(0))
              .getOrdinalsValues();
      assertEquals(left.getValueCount(), right.getValueCount());
      for (long ord = 0; ord < left.getValueCount(); ++ord) {
        assertEquals(left.lookupOrd(ord), right.lookupOrd(ord));
      }
    }
  }
Example #13
0
 @Test
 public void testCollectLiterals() throws Exception {
   List<Symbol> toCollect =
       Arrays.<Symbol>asList(
           Literal.newLiteral("foobar"),
           Literal.newLiteral(true),
           Literal.newLiteral(1),
           Literal.newLiteral(4.2));
   CollectPhase collectNode =
       new CollectPhase(
           UUID.randomUUID(), 0, "literals", testRouting, toCollect, EMPTY_PROJECTIONS);
   Bucket result = getBucket(collectNode);
   assertThat(result, contains(isRow(new BytesRef("foobar"), true, 1, 4.2)));
 }
  public void testRefresh() {
    String[] indexShardActions =
        new String[] {TransportShardRefreshAction.NAME + "[r]", TransportShardRefreshAction.NAME};
    interceptTransportActions(indexShardActions);

    RefreshRequest refreshRequest = new RefreshRequest(randomIndicesOrAliases());
    internalCluster().clientNodeClient().admin().indices().refresh(refreshRequest).actionGet();

    clearInterceptedActions();
    String[] indices =
        new IndexNameExpressionResolver(Settings.EMPTY)
            .concreteIndices(
                client().admin().cluster().prepareState().get().getState(), refreshRequest);
    assertIndicesSubset(Arrays.asList(indices), indexShardActions);
  }
Example #15
0
 @Test
 public void testCollectShardExpressionsLiteralsAndNodeExpressions() throws Exception {
   CollectPhase collectNode =
       new CollectPhase(
           UUID.randomUUID(),
           0,
           "shardCollect",
           shardRouting(0, 1),
           Arrays.asList(testShardIdReference, Literal.newLiteral(true), testNodeReference),
           EMPTY_PROJECTIONS);
   collectNode.maxRowGranularity(RowGranularity.SHARD);
   Bucket result = getBucket(collectNode);
   assertThat(result.size(), is(2));
   assertThat(result, containsInAnyOrder(isRow(0, true, (short) 1), isRow(1, true, (short) 1)));
 }
Example #16
0
 @Test
 public void testCollectFunction() throws Exception {
   Function twoTimesTruthFunction = new Function(TestFunction.info, TO_COLLECT_TEST_REF);
   CollectPhase collectNode =
       new CollectPhase(
           UUID.randomUUID(),
           0,
           "unknown",
           testRouting,
           Arrays.asList(twoTimesTruthFunction, testNodeReference),
           EMPTY_PROJECTIONS);
   collectNode.maxRowGranularity(RowGranularity.NODE);
   Bucket result = getBucket(collectNode);
   assertThat(result.size(), equalTo(1));
   assertThat(result, contains(isRow(2, (short) 1)));
 }
Example #17
0
  /** Removing pre-authorization when the CC number is changed. */
  @Test
  public void testRemoveOnCCChange() {
    UserWS user = buildUser(PRANCING_PONY_ACCOUNT_TYPE);
    user.setId(api.createUser(user));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    // put a pre-auth record on this user
    OrderWS order = buildOrder(user.getId(), Arrays.asList(item.getId()), new BigDecimal("3.45"));

    PaymentAuthorizationDTOEx auth =
        api.createOrderPreAuthorize(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));
    Integer orderId = api.getLatestOrder(user.getId()).getId();

    PaymentWS preAuthPayment = api.getPayment(auth.getPaymentId());
    assertThat(preAuthPayment, is(not(nullValue())));
    assertThat(preAuthPayment.getIsPreauth(), is(1));
    assertThat(preAuthPayment.getDeleted(), is(0)); // NOT deleted

    // update the user's credit card, this should remove the old card
    // and delete any associated pre-authorizations
    DateTimeFormatter format = DateTimeFormat.forPattern(Constants.CC_DATE_FORMAT);
    user = api.getUserWS(user.getId());
    com.sapienter.jbilling.server.user.WSTest.updateMetaField(
        user.getPaymentInstruments().iterator().next().getMetaFields(),
        PaymentMethodHelper.CC_MF_EXPIRY_DATE,
        format.print(new DateMidnight().plusYears(4).withDayOfMonth(1).toDate().getTime()));
    api.updateUser(user);
    System.out.println("User instruments are: " + user.getPaymentInstruments());
    // validate that the pre-auth payment is no longer available
    preAuthPayment = api.getPayment(auth.getPaymentId());
    assertThat(preAuthPayment, is(not(nullValue())));
    assertThat(preAuthPayment.getIsPreauth(), is(1));
    assertThat(preAuthPayment.getDeleted(), is(1)); // is now deleted

    // cleanup
    api.deleteOrder(orderId);
    api.deleteItem(item.getId());
    api.deleteItemCategory(itemType.getId());
    api.deleteUser(user.getId());
  }
Example #18
0
 @Test
 public void testCollectWithFalseWhereClause() throws Exception {
   CollectPhase collectNode =
       new CollectPhase(
           UUID.randomUUID(),
           0,
           "whereClause",
           testRouting,
           TO_COLLECT_TEST_REF,
           EMPTY_PROJECTIONS);
   collectNode.whereClause(
       new WhereClause(
           new Function(
               AndOperator.INFO,
               Arrays.<Symbol>asList(Literal.newLiteral(false), Literal.newLiteral(false)))));
   Bucket result = getBucket(collectNode);
   assertThat(result.size(), is(0));
 }
Example #19
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);
  }
Example #20
0
 @Test
 public void testCollectWithTrueWhereClause() throws Exception {
   CollectPhase collectNode =
       new CollectPhase(
           UUID.randomUUID(),
           0,
           "whereClause",
           testRouting,
           TO_COLLECT_TEST_REF,
           EMPTY_PROJECTIONS);
   collectNode.whereClause(
       new WhereClause(
           new Function(
               AndOperator.INFO,
               Arrays.<Symbol>asList(Literal.newLiteral(true), Literal.newLiteral(true)))));
   collectNode.maxRowGranularity(RowGranularity.NODE);
   Bucket result = getBucket(collectNode);
   assertThat(result, contains(isRow((short) 1)));
 }
Example #21
0
  @Test
  public void testCollectShardExpressionsWhereShardIdIs0() throws Exception {
    EqOperator op =
        (EqOperator)
            functions.get(
                new FunctionIdent(
                    EqOperator.NAME,
                    ImmutableList.<DataType>of(DataTypes.INTEGER, DataTypes.INTEGER)));

    List<Symbol> toCollect = ImmutableList.<Symbol>of(testShardIdReference);
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(), 0, "shardCollect", shardRouting(0, 1), toCollect, EMPTY_PROJECTIONS);
    collectNode.whereClause(
        new WhereClause(
            new Function(op.info(), Arrays.asList(testShardIdReference, Literal.newLiteral(0)))));
    collectNode.maxRowGranularity(RowGranularity.SHARD);
    Bucket result = getBucket(collectNode);
    assertThat(result, contains(isRow(0)));
  }
Example #22
0
 @Test
 public void testCollectWithNullWhereClause() throws Exception {
   EqOperator op =
       (EqOperator)
           functions.get(
               new FunctionIdent(
                   EqOperator.NAME,
                   ImmutableList.<DataType>of(DataTypes.INTEGER, DataTypes.INTEGER)));
   CollectPhase collectNode =
       new CollectPhase(
           UUID.randomUUID(),
           0,
           "whereClause",
           testRouting,
           TO_COLLECT_TEST_REF,
           EMPTY_PROJECTIONS);
   collectNode.whereClause(
       new WhereClause(
           new Function(op.info(), Arrays.<Symbol>asList(Literal.NULL, Literal.NULL))));
   Bucket result = getBucket(collectNode);
   assertThat(result.size(), is(0));
 }
Example #23
0
  static class TestFunction extends Scalar<Integer, Object> {
    public static final FunctionIdent ident =
        new FunctionIdent("twoTimes", Arrays.<DataType>asList(DataTypes.INTEGER));
    public static final FunctionInfo info = new FunctionInfo(ident, DataTypes.INTEGER);

    @Override
    public Integer evaluate(Input<Object>... args) {
      if (args.length == 0) {
        return 0;
      }
      Short value = (Short) args[0].value();
      return value * 2;
    }

    @Override
    public FunctionInfo info() {
      return info;
    }

    @Override
    public Symbol normalizeSymbol(Function symbol) {
      return symbol;
    }
  }
Example #24
0
  /** Tests the PaymentRouterCurrencyTask. */
  @Test
  public void testPaymentRouterCurrencyTask() {
    // prepare
    UserWS userUSD = buildUser(PRANCING_PONY_ACCOUNT_TYPE);
    userUSD.setCurrencyId(CURRENCY_USD);
    userUSD.setId(api.createUser(userUSD));

    UserWS userAUD = buildUser(PRANCING_PONY_ACCOUNT_TYPE);
    userAUD.setCurrencyId(CURRENCY_AUD);
    userAUD.setId(api.createUser(userAUD));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    // testing
    OrderWS order = buildOrder(userUSD.getId(), Arrays.asList(item.getId()), new BigDecimal("10"));
    order.setCurrencyId(userUSD.getCurrencyId());

    // create the order and invoice it
    System.out.println("Creating and invoicing order ...");
    Integer invoiceIdUSD =
        api.createOrderAndInvoice(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));
    Integer orderIdUSD = api.getLastOrders(userUSD.getId(), 1)[0];

    // try paying the invoice in USD
    System.out.println("Making payment in USD...");
    PaymentAuthorizationDTOEx authInfo = api.payInvoice(invoiceIdUSD);

    assertTrue("USD Payment should be successful", authInfo.getResult().booleanValue());
    assertEquals(
        "Should be processed by 'first_fake_processor'",
        authInfo.getProcessor(),
        "first_fake_processor");

    // create a new order in AUD and invoice it
    order.setUserId(userAUD.getId());
    order.setCurrencyId(userAUD.getCurrencyId());

    System.out.println("Creating and invoicing order ...");
    Integer invoiceIdAUD =
        api.createOrderAndInvoice(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));
    Integer orderIdAUD = api.getLastOrders(userAUD.getId(), 1)[0];

    // try paying the invoice in AUD
    System.out.println("Making payment in AUD...");
    authInfo = api.payInvoice(invoiceIdAUD);

    assertTrue("AUD Payment should be successful", authInfo.getResult().booleanValue());
    assertEquals(
        "Should be processed by 'second_fake_processor'",
        authInfo.getProcessor(),
        "second_fake_processor");

    // remove invoices and orders
    System.out.println("Deleting invoices and orders.");
    api.deleteInvoice(invoiceIdUSD);
    api.deleteInvoice(invoiceIdAUD);
    api.deleteOrder(orderIdUSD);
    api.deleteOrder(orderIdAUD);
    api.deleteUser(userUSD.getId());
    api.deleteUser(userAUD.getId());
    api.deleteItem(item.getId());
    api.deleteItemCategory(itemType.getId());
  }
 public ExecutionResult assertTasksSkipped(String... taskPaths) {
   Set<String> expected = new HashSet<String>(Arrays.asList(taskPaths));
   assertThat(skippedTasks, equalTo(expected));
   return this;
 }
 public ExecutionResult assertTasksExecuted(String... taskPaths) {
   List<String> expected = Arrays.asList(taskPaths);
   assertThat(plannedTasks, equalTo(expected));
   return this;
 }
Example #27
0
  /** Tests processPayment API call. */
  @Test
  public void testProcessPayment() {
    // setup
    UserWS user = buildUser(PRANCING_PONY_ACCOUNT_TYPE, "4111111111111111");
    user.setId(api.createUser(user));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    // first, create two unpaid invoices
    OrderWS order = buildOrder(user.getId(), Arrays.asList(item.getId()), new BigDecimal("10.00"));
    Integer invoiceId1 =
        api.createOrderAndInvoice(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));
    Integer invoiceId2 =
        api.createOrderAndInvoice(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));

    // create the payment
    PaymentWS payment = new PaymentWS();
    payment.setAmount(new BigDecimal("5.00"));
    payment.setIsRefund(new Integer(0));
    payment.setMethodId(Constants.PAYMENT_METHOD_VISA);
    payment.setPaymentDate(Calendar.getInstance().getTime());
    payment.setCurrencyId(CURRENCY_USD);
    payment.setUserId(user.getId());

    //  try a credit card number that fails
    // note that creating a payment with a NEW credit card will save it and associate
    // it with the user who made the payment.
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.YEAR, 5);

    PaymentInformationWS cc =
        PaymentMethodHelper.createCreditCard(
            CC_PAYMENT_TYPE, "Frodo Baggins", "4111111111111111", cal.getTime());
    cc.setPaymentMethodId(Constants.PAYMENT_METHOD_VISA);
    payment.getPaymentInstruments().add(cc);

    System.out.println("processing payment.");
    PaymentAuthorizationDTOEx authInfo = api.processPayment(payment, null);

    // check payment failed
    assertNotNull("Payment result not null", authInfo);
    assertFalse(
        "Payment Authorization result should be FAILED", authInfo.getResult().booleanValue());

    // check payment has zero balance
    PaymentWS lastPayment = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", lastPayment);
    assertNotNull("auth in payment can not be null", lastPayment.getAuthorizationId());
    assertEquals("correct payment amount", new BigDecimal("5"), lastPayment.getAmountAsDecimal());
    assertEquals("correct payment balance", BigDecimal.ZERO, lastPayment.getBalanceAsDecimal());

    // check invoices still have balance
    InvoiceWS invoice1 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", new BigDecimal("10.0"), invoice1.getBalanceAsDecimal());
    InvoiceWS invoice2 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", new BigDecimal("10.0"), invoice2.getBalanceAsDecimal());

    // do it again, but using the credit card on file
    // which is also 4111111111111111
    payment.getPaymentInstruments().clear();
    System.out.println("processing payment.");
    authInfo = api.processPayment(payment, null);
    // check payment has zero balance
    PaymentWS lastPayment2 = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", lastPayment2);
    assertNotNull("auth in payment can not be null", lastPayment2.getAuthorizationId());
    assertEquals("correct payment amount", new BigDecimal("5"), lastPayment2.getAmountAsDecimal());
    assertEquals("correct payment balance", BigDecimal.ZERO, lastPayment2.getBalanceAsDecimal());
    assertFalse("Payment is not the same as preiouvs", lastPayment2.getId() == lastPayment.getId());

    // check invoices still have balance
    invoice1 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", new BigDecimal("10"), invoice1.getBalanceAsDecimal());
    invoice2 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", new BigDecimal("10"), invoice2.getBalanceAsDecimal());

    //  do a successful payment of $5
    cc =
        PaymentMethodHelper.createCreditCard(
            CC_PAYMENT_TYPE, "Frodo Baggins", "4111111111111152", cal.getTime());
    cc.setPaymentMethodId(Constants.PAYMENT_METHOD_VISA);
    payment.getPaymentInstruments().add(cc);
    System.out.println("processing payment.");
    authInfo = api.processPayment(payment, null);

    // check payment successful
    assertNotNull("Payment result not null", authInfo);
    assertNotNull("Auth id not null", authInfo.getId());
    assertTrue("Payment Authorization result should be OK", authInfo.getResult().booleanValue());

    // check payment was made
    lastPayment = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", lastPayment);
    assertNotNull("auth in payment can not be null", lastPayment.getAuthorizationId());
    assertEquals("payment ids match", lastPayment.getId(), authInfo.getPaymentId().intValue());
    assertEquals("correct payment amount", new BigDecimal("5"), lastPayment.getAmountAsDecimal());
    assertEquals("correct payment balance", BigDecimal.ZERO, lastPayment.getBalanceAsDecimal());

    // check invoice 1 was partially paid (balance 5)
    invoice1 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", new BigDecimal("5.0"), invoice1.getBalanceAsDecimal());

    // check invoice 2 wan't paid at all
    invoice2 = api.getInvoiceWS(invoiceId2);
    assertEquals("correct invoice balance", new BigDecimal("10.0"), invoice2.getBalanceAsDecimal());

    //  another payment for $10, this time with the user's credit card

    // update the credit card to the one that is good
    user = api.getUserWS(user.getId());
    com.sapienter.jbilling.server.user.WSTest.updateMetaField(
        user.getPaymentInstruments().iterator().next().getMetaFields(),
        PaymentMethodHelper.CC_MF_NUMBER,
        "4111111111111152");
    api.updateUser(user);

    // process a payment without an attached credit card
    // should try and use the user's saved credit card
    payment.getPaymentInstruments().clear();
    payment.setAmount(new BigDecimal("10.00"));
    System.out.println("processing payment.");
    authInfo = api.processPayment(payment, null);

    // check payment successful
    assertNotNull("Payment result not null", authInfo);
    assertTrue("Payment Authorization result should be OK", authInfo.getResult().booleanValue());

    // check payment was made
    lastPayment = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", lastPayment);
    assertNotNull("auth in payment can not be null", lastPayment.getAuthorizationId());
    assertEquals("correct payment amount", new BigDecimal("10"), lastPayment.getAmountAsDecimal());
    assertEquals("correct payment balance", BigDecimal.ZERO, lastPayment.getBalanceAsDecimal());

    // check invoice 1 is fully paid (balance 0)
    invoice1 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", BigDecimal.ZERO, invoice1.getBalanceAsDecimal());

    // check invoice 2 was partially paid (balance 5)
    invoice2 = api.getInvoiceWS(invoiceId2);
    assertEquals("correct invoice balance", new BigDecimal("5"), invoice2.getBalanceAsDecimal());

    // another payment for $10

    payment.getPaymentInstruments().add(cc);
    payment.setAmount(new BigDecimal("10.00"));
    System.out.println("processing payment.");
    authInfo = api.processPayment(payment, null);

    // check payment successful
    assertNotNull("Payment result not null", authInfo);
    assertTrue("Payment Authorization result should be OK", authInfo.getResult().booleanValue());

    // check payment was made
    lastPayment = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", lastPayment);
    assertNotNull("auth in payment can not be null", lastPayment.getAuthorizationId());
    assertEquals("correct  payment amount", new BigDecimal("10"), lastPayment.getAmountAsDecimal());
    assertEquals(
        "correct  payment balance", new BigDecimal("5"), lastPayment.getBalanceAsDecimal());

    // check invoice 1 balance is unchanged
    invoice1 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", BigDecimal.ZERO, invoice1.getBalanceAsDecimal());

    // check invoice 2 is fully paid (balance 0)
    invoice2 = api.getInvoiceWS(invoiceId2);
    assertEquals("correct invoice balance", BigDecimal.ZERO, invoice2.getBalanceAsDecimal());

    // cleanup
    System.out.println("Deleting invoices and orders.");
    api.deleteInvoice(invoice1.getId());
    api.deleteInvoice(invoice2.getId());
    api.deleteOrder(invoice1.getOrders()[0]);
    api.deleteOrder(invoice2.getOrders()[0]);
    api.deleteItem(item.getId());
    api.deleteItemCategory(itemType.getId());
    api.deleteUser(user.getId());
  }
  @Test
  public void testDuelAllTypesSingleValue() throws Exception {
    final String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("bytes")
            .field("type", "string")
            .field("index", "not_analyzed")
            .startObject("fielddata")
            .field("format", LuceneTestCase.defaultCodecSupportsSortedSet() ? "doc_values" : "fst")
            .endObject()
            .endObject()
            .startObject("byte")
            .field("type", "byte")
            .startObject("fielddata")
            .field("format", "doc_values")
            .endObject()
            .endObject()
            .startObject("short")
            .field("type", "short")
            .startObject("fielddata")
            .field("format", "doc_values")
            .endObject()
            .endObject()
            .startObject("integer")
            .field("type", "integer")
            .startObject("fielddata")
            .field("format", "doc_values")
            .endObject()
            .endObject()
            .startObject("long")
            .field("type", "long")
            .startObject("fielddata")
            .field("format", "doc_values")
            .endObject()
            .endObject()
            .startObject("float")
            .field("type", "float")
            .startObject("fielddata")
            .field("format", "doc_values")
            .endObject()
            .endObject()
            .startObject("double")
            .field("type", "double")
            .startObject("fielddata")
            .field("format", "doc_values")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();
    final DocumentMapper mapper = mapperService.documentMapperParser().parse(mapping);
    Random random = getRandom();
    int atLeast = scaledRandomIntBetween(1000, 1500);
    for (int i = 0; i < atLeast; i++) {
      String s = Integer.toString(randomByte());

      XContentBuilder doc = XContentFactory.jsonBuilder().startObject();
      for (String fieldName :
          Arrays.asList("bytes", "byte", "short", "integer", "long", "float", "double")) {
        doc = doc.field(fieldName, s);
      }

      doc = doc.endObject();

      final ParsedDocument d = mapper.parse("type", Integer.toString(i), doc.bytes());

      writer.addDocument(d.rootDoc());

      if (random.nextInt(10) == 0) {
        refreshReader();
      }
    }
    AtomicReaderContext context = refreshReader();
    Map<FieldDataType, Type> typeMap = new HashMap<>();
    typeMap.put(
        new FieldDataType("string", ImmutableSettings.builder().put("format", "fst")), Type.Bytes);
    typeMap.put(
        new FieldDataType("string", ImmutableSettings.builder().put("format", "paged_bytes")),
        Type.Bytes);
    typeMap.put(
        new FieldDataType("byte", ImmutableSettings.builder().put("format", "array")),
        Type.Integer);
    typeMap.put(
        new FieldDataType("short", ImmutableSettings.builder().put("format", "array")),
        Type.Integer);
    typeMap.put(
        new FieldDataType("int", ImmutableSettings.builder().put("format", "array")), Type.Integer);
    typeMap.put(
        new FieldDataType("long", ImmutableSettings.builder().put("format", "array")), Type.Long);
    typeMap.put(
        new FieldDataType("double", ImmutableSettings.builder().put("format", "array")),
        Type.Double);
    typeMap.put(
        new FieldDataType("float", ImmutableSettings.builder().put("format", "array")), Type.Float);
    typeMap.put(
        new FieldDataType("byte", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Integer);
    typeMap.put(
        new FieldDataType("short", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Integer);
    typeMap.put(
        new FieldDataType("int", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Integer);
    typeMap.put(
        new FieldDataType("long", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Long);
    typeMap.put(
        new FieldDataType("double", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Double);
    typeMap.put(
        new FieldDataType("float", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Float);
    typeMap.put(
        new FieldDataType("string", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Bytes);
    ArrayList<Entry<FieldDataType, Type>> list = new ArrayList<>(typeMap.entrySet());
    Preprocessor pre = new ToDoublePreprocessor();
    while (!list.isEmpty()) {
      Entry<FieldDataType, Type> left;
      Entry<FieldDataType, Type> right;
      if (list.size() > 1) {
        left = list.remove(random.nextInt(list.size()));
        right = list.remove(random.nextInt(list.size()));
      } else {
        right = left = list.remove(0);
      }

      ifdService.clear();
      IndexFieldData<?> leftFieldData =
          getForField(left.getKey(), left.getValue().name().toLowerCase(Locale.ROOT));
      ifdService.clear();
      IndexFieldData<?> rightFieldData =
          getForField(right.getKey(), right.getValue().name().toLowerCase(Locale.ROOT));
      duelFieldDataBytes(random, context, leftFieldData, rightFieldData, pre);
      duelFieldDataBytes(random, context, rightFieldData, leftFieldData, pre);

      DirectoryReader perSegment = DirectoryReader.open(writer, true);
      CompositeReaderContext composite = perSegment.getContext();
      List<AtomicReaderContext> leaves = composite.leaves();
      for (AtomicReaderContext atomicReaderContext : leaves) {
        duelFieldDataBytes(random, atomicReaderContext, leftFieldData, rightFieldData, pre);
      }
    }
  }
Example #29
0
public class LocalDataCollectTest extends CrateUnitTest {

  public static final ImmutableList<Projection> EMPTY_PROJECTIONS = ImmutableList.of();

  static class TestFunction extends Scalar<Integer, Object> {
    public static final FunctionIdent ident =
        new FunctionIdent("twoTimes", Arrays.<DataType>asList(DataTypes.INTEGER));
    public static final FunctionInfo info = new FunctionInfo(ident, DataTypes.INTEGER);

    @Override
    public Integer evaluate(Input<Object>... args) {
      if (args.length == 0) {
        return 0;
      }
      Short value = (Short) args[0].value();
      return value * 2;
    }

    @Override
    public FunctionInfo info() {
      return info;
    }

    @Override
    public Symbol normalizeSymbol(Function symbol) {
      return symbol;
    }
  }

  static class ShardIdExpression extends SysShardExpression<Integer>
      implements ShardReferenceImplementation<Integer> {

    private final ShardId shardId;

    @Inject
    public ShardIdExpression(ShardId shardId) {
      this.shardId = shardId;
    }

    @Override
    public Integer value() {
      return shardId.id();
    }

    @Override
    public ReferenceImplementation getChildImplementation(String name) {
      return null;
    }
  }

  private DiscoveryService discoveryService;
  private Functions functions;
  private IndexService indexService = mock(IndexService.class);
  private MapSideDataCollectOperation operation;
  private Routing testRouting =
      new Routing(
          TreeMapBuilder.<String, Map<String, List<Integer>>>newMapBuilder()
              .put(TEST_NODE_ID, new TreeMap<String, List<Integer>>())
              .map());

  private JobContextService jobContextService;

  private final ThreadPool testThreadPool = new ThreadPool(getClass().getSimpleName());
  private static final String TEST_NODE_ID = "test_node";
  private static final String TEST_TABLE_NAME = "test_table";

  private static Reference testNodeReference =
      new Reference(
          SysNodesTableInfo.INFOS.get(new ColumnIdent("os", ImmutableList.of("cpu", "stolen"))));
  public static final List<Symbol> TO_COLLECT_TEST_REF = Arrays.<Symbol>asList(testNodeReference);
  private static Reference testShardIdReference =
      new Reference(SysShardsTableInfo.INFOS.get(new ColumnIdent("id")));

  private static final RamAccountingContext RAM_ACCOUNTING_CONTEXT =
      new RamAccountingContext("dummy", new NoopCircuitBreaker(CircuitBreaker.Name.FIELDDATA));

  class TestModule extends AbstractModule {
    protected MapBinder<FunctionIdent, FunctionImplementation> functionBinder;

    @Override
    protected void configure() {
      functionBinder =
          MapBinder.newMapBinder(binder(), FunctionIdent.class, FunctionImplementation.class);
      functionBinder.addBinding(TestFunction.ident).toInstance(new TestFunction());
      bind(Functions.class).asEagerSingleton();
      bind(ReferenceInfos.class).toInstance(mock(ReferenceInfos.class));
      bind(ThreadPool.class).toInstance(testThreadPool);

      BulkRetryCoordinator bulkRetryCoordinator = mock(BulkRetryCoordinator.class);
      BulkRetryCoordinatorPool bulkRetryCoordinatorPool = mock(BulkRetryCoordinatorPool.class);
      when(bulkRetryCoordinatorPool.coordinator(any(ShardId.class)))
          .thenReturn(bulkRetryCoordinator);
      bind(BulkRetryCoordinatorPool.class).toInstance(bulkRetryCoordinatorPool);

      bind(TransportBulkCreateIndicesAction.class)
          .toInstance(mock(TransportBulkCreateIndicesAction.class));
      bind(CircuitBreakerService.class).toInstance(new NoneCircuitBreakerService());
      bind(ActionFilters.class).toInstance(mock(ActionFilters.class));
      bind(ScriptService.class).toInstance(mock(ScriptService.class));
      bind(SearchService.class).toInstance(mock(InternalSearchService.class));
      bind(AllocationService.class).toInstance(mock(AllocationService.class));
      bind(MetaDataCreateIndexService.class).toInstance(mock(MetaDataCreateIndexService.class));
      bind(DynamicSettings.class)
          .annotatedWith(ClusterDynamicSettings.class)
          .toInstance(mock(DynamicSettings.class));
      bind(MetaDataDeleteIndexService.class).toInstance(mock(MetaDataDeleteIndexService.class));
      bind(ClusterInfoService.class).toInstance(mock(ClusterInfoService.class));
      bind(TransportService.class).toInstance(mock(TransportService.class));
      bind(MapperService.class).toInstance(mock(MapperService.class));

      OsService osService = mock(OsService.class);
      OsStats osStats = mock(OsStats.class);
      when(osService.stats()).thenReturn(osStats);
      OsStats.Cpu osCpu = mock(OsStats.Cpu.class);
      when(osCpu.stolen()).thenReturn((short) 1);
      when(osStats.cpu()).thenReturn(osCpu);

      bind(OsService.class).toInstance(osService);
      bind(NodeService.class).toInstance(mock(NodeService.class));
      bind(Discovery.class).toInstance(mock(Discovery.class));
      bind(NetworkService.class).toInstance(mock(NetworkService.class));

      bind(TransportShardBulkAction.class).toInstance(mock(TransportShardBulkAction.class));
      bind(TransportCreateIndexAction.class).toInstance(mock(TransportCreateIndexAction.class));

      discoveryService = mock(DiscoveryService.class);
      DiscoveryNode discoveryNode = mock(DiscoveryNode.class);
      when(discoveryNode.id()).thenReturn(TEST_NODE_ID);
      when(discoveryService.localNode()).thenReturn(discoveryNode);

      ClusterService clusterService = mock(ClusterService.class);
      ClusterState state = mock(ClusterState.class);
      DiscoveryNodes discoveryNodes = mock(DiscoveryNodes.class);
      when(discoveryNodes.localNodeId()).thenReturn(TEST_NODE_ID);
      when(state.nodes()).thenReturn(discoveryNodes);
      when(clusterService.state()).thenReturn(state);
      when(clusterService.localNode()).thenReturn(discoveryNode);
      bind(ClusterService.class).toInstance(clusterService);

      IndicesService indicesService = mock(IndicesService.class);
      bind(IndicesService.class).toInstance(indicesService);
      bind(Settings.class).toInstance(ImmutableSettings.EMPTY);

      bind(MetaDataUpdateSettingsService.class)
          .toInstance(mock(MetaDataUpdateSettingsService.class));
      bind(Client.class).toInstance(mock(Client.class));

      Provider<TransportCreateIndexAction> transportCreateIndexActionProvider =
          mock(Provider.class);
      when(transportCreateIndexActionProvider.get())
          .thenReturn(mock(TransportCreateIndexAction.class));
      Provider<TransportDeleteIndexAction> transportDeleteActionProvider = mock(Provider.class);
      when(transportDeleteActionProvider.get()).thenReturn(mock(TransportDeleteIndexAction.class));
      Provider<TransportUpdateSettingsAction> transportUpdateSettingsActionProvider =
          mock(Provider.class);
      when(transportUpdateSettingsActionProvider.get())
          .thenReturn(mock(TransportUpdateSettingsAction.class));

      BlobIndices blobIndices =
          new BlobIndices(
              ImmutableSettings.EMPTY,
              transportCreateIndexActionProvider,
              transportDeleteActionProvider,
              transportUpdateSettingsActionProvider,
              indicesService,
              mock(IndicesLifecycle.class),
              mock(BlobEnvironment.class),
              clusterService);
      bind(BlobIndices.class).toInstance(blobIndices);

      bind(ReferenceResolver.class).to(GlobalReferenceResolver.class);

      TransportPutIndexTemplateAction transportPutIndexTemplateAction =
          mock(TransportPutIndexTemplateAction.class);
      bind(TransportPutIndexTemplateAction.class).toInstance(transportPutIndexTemplateAction);

      bind(IndexService.class).toInstance(indexService);
    }
  }

  class TestShardModule extends AbstractModule {

    private final ShardId shardId;
    private final ShardIdExpression shardIdExpression;

    public TestShardModule(int shardId) {
      super();
      this.shardId = new ShardId(TEST_TABLE_NAME, shardId);
      this.shardIdExpression = new ShardIdExpression(this.shardId);
    }

    @Override
    protected void configure() {
      IndexShard shard = mock(IndexShard.class);
      bind(IndexShard.class).toInstance(shard);
      when(shard.shardId()).thenReturn(shardId);
      Index index = new Index(TEST_TABLE_NAME);
      bind(Index.class).toInstance(index);
      bind(ShardId.class).toInstance(shardId);
      MapBinder<ReferenceIdent, ShardReferenceImplementation> binder =
          MapBinder.newMapBinder(
              binder(), ReferenceIdent.class, ShardReferenceImplementation.class);
      binder
          .addBinding(SysShardsTableInfo.INFOS.get(new ColumnIdent("id")).ident())
          .toInstance(shardIdExpression);
      bind(ShardReferenceResolver.class).asEagerSingleton();
      bind(AllocationDecider.class).to(DiskThresholdDecider.class);
      bind(ShardCollectService.class).asEagerSingleton();

      bind(DiscoveryService.class).toInstance(discoveryService);

      // blob stuff
      MapBinder<ReferenceIdent, BlobShardReferenceImplementation> blobBinder =
          MapBinder.newMapBinder(
              binder(), ReferenceIdent.class, BlobShardReferenceImplementation.class);
      bind(Settings.class).annotatedWith(IndexSettings.class).toInstance(ImmutableSettings.EMPTY);
    }
  }

  @Before
  public void configure() {
    Injector injector =
        new ModulesBuilder()
            .add(
                new CircuitBreakerModule(),
                new OperatorModule(),
                new TestModule(),
                new SysNodeExpressionModule())
            .createInjector();
    Injector shard0Injector = injector.createChildInjector(new TestShardModule(0));
    Injector shard1Injector = injector.createChildInjector(new TestShardModule(1));
    functions = injector.getInstance(Functions.class);

    IndicesService indicesService = injector.getInstance(IndicesService.class);
    indexService = injector.getInstance(IndexService.class);

    when(indexService.shardInjectorSafe(0)).thenReturn(shard0Injector);
    when(indexService.shardInjectorSafe(1)).thenReturn(shard1Injector);
    when(indexService.shardSafe(0)).thenReturn(shard0Injector.getInstance(IndexShard.class));
    when(indexService.shardSafe(1)).thenReturn(shard1Injector.getInstance(IndexShard.class));
    when(indicesService.indexServiceSafe(TEST_TABLE_NAME)).thenReturn(indexService);

    NodeSettingsService nodeSettingsService = mock(NodeSettingsService.class);
    jobContextService =
        new JobContextService(ImmutableSettings.EMPTY, testThreadPool, mock(StatsTables.class));

    ClusterService clusterService = injector.getInstance(ClusterService.class);
    operation =
        new MapSideDataCollectOperation(
            clusterService,
            ImmutableSettings.EMPTY,
            mock(TransportActionProvider.class, Answers.RETURNS_DEEP_STUBS.get()),
            injector.getInstance(BulkRetryCoordinatorPool.class),
            functions,
            injector.getInstance(ReferenceResolver.class),
            injector.getInstance(NodeSysExpression.class),
            indicesService,
            testThreadPool,
            new CollectServiceResolver(
                discoveryService,
                new SystemCollectService(
                    discoveryService,
                    functions,
                    new StatsTables(ImmutableSettings.EMPTY, nodeSettingsService))),
            mock(InformationSchemaCollectService.class),
            mock(UnassignedShardsCollectService.class));
  }

  @After
  public void cleanUp() throws Exception {
    testThreadPool.shutdownNow();
  }

  private Routing shardRouting(final Integer... shardIds) {
    return new Routing(
        TreeMapBuilder.<String, Map<String, List<Integer>>>newMapBuilder()
            .put(
                TEST_NODE_ID,
                TreeMapBuilder.<String, List<Integer>>newMapBuilder()
                    .put(TEST_TABLE_NAME, Arrays.asList(shardIds))
                    .map())
            .map());
  }

  @Test
  public void testCollectExpressions() throws Exception {
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(),
            0,
            "collect",
            testRouting,
            Collections.<Symbol>singletonList(testNodeReference),
            EMPTY_PROJECTIONS);
    collectNode.maxRowGranularity(RowGranularity.NODE);

    Bucket result = getBucket(collectNode);

    assertThat(result.size(), equalTo(1));
    assertThat(result, contains(isRow((short) 1)));
  }

  @Test
  public void testWrongRouting() throws Exception {

    expectedException.expect(UnhandledServerException.class);
    expectedException.expectMessage("unsupported routing");

    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(),
            0,
            "wrong",
            new Routing(
                TreeMapBuilder.<String, Map<String, List<Integer>>>newMapBuilder()
                    .put(
                        "bla",
                        TreeMapBuilder.<String, List<Integer>>newMapBuilder()
                            .put("my_index", Arrays.asList(1))
                            .put("my_index", Arrays.asList(1))
                            .map())
                    .map()),
            ImmutableList.<Symbol>of(),
            EMPTY_PROJECTIONS);
    collectNode.maxRowGranularity(RowGranularity.DOC);
    operation.collect(collectNode, new CollectingProjector(), null);
  }

  @Test
  public void testCollectUnknownReference() throws Throwable {
    expectedException.expect(UnhandledServerException.class);
    expectedException.expectMessage("Unknown Reference some.table.some_column");

    Reference unknownReference =
        new Reference(
            new ReferenceInfo(
                new ReferenceIdent(new TableIdent("some", "table"), "some_column"),
                RowGranularity.NODE,
                DataTypes.BOOLEAN));
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(),
            0,
            "unknown",
            testRouting,
            Collections.<Symbol>singletonList(unknownReference),
            EMPTY_PROJECTIONS);
    collectNode.maxRowGranularity(RowGranularity.NODE);
    try {
      getBucket(collectNode);
    } catch (ExecutionException e) {
      throw e.getCause();
    }
  }

  @Test
  public void testCollectFunction() throws Exception {
    Function twoTimesTruthFunction = new Function(TestFunction.info, TO_COLLECT_TEST_REF);
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(),
            0,
            "unknown",
            testRouting,
            Arrays.asList(twoTimesTruthFunction, testNodeReference),
            EMPTY_PROJECTIONS);
    collectNode.maxRowGranularity(RowGranularity.NODE);
    Bucket result = getBucket(collectNode);
    assertThat(result.size(), equalTo(1));
    assertThat(result, contains(isRow(2, (short) 1)));
  }

  @Test
  public void testUnknownFunction() throws Throwable {
    // will be wrapped somewhere above
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("Cannot find implementation for function unknown()");

    Symbol unknownFunction =
        new Function(
            new FunctionInfo(
                new FunctionIdent("unknown", ImmutableList.<DataType>of()), DataTypes.BOOLEAN),
            ImmutableList.<Symbol>of());
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(),
            0,
            "unknownFunction",
            testRouting,
            Collections.singletonList(unknownFunction),
            EMPTY_PROJECTIONS);
    try {
      getBucket(collectNode);
    } catch (ExecutionException e) {
      throw e.getCause();
    }
  }

  @Test
  public void testCollectLiterals() throws Exception {
    List<Symbol> toCollect =
        Arrays.<Symbol>asList(
            Literal.newLiteral("foobar"),
            Literal.newLiteral(true),
            Literal.newLiteral(1),
            Literal.newLiteral(4.2));
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(), 0, "literals", testRouting, toCollect, EMPTY_PROJECTIONS);
    Bucket result = getBucket(collectNode);
    assertThat(result, contains(isRow(new BytesRef("foobar"), true, 1, 4.2)));
  }

  @Test
  public void testCollectWithFalseWhereClause() throws Exception {
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(),
            0,
            "whereClause",
            testRouting,
            TO_COLLECT_TEST_REF,
            EMPTY_PROJECTIONS);
    collectNode.whereClause(
        new WhereClause(
            new Function(
                AndOperator.INFO,
                Arrays.<Symbol>asList(Literal.newLiteral(false), Literal.newLiteral(false)))));
    Bucket result = getBucket(collectNode);
    assertThat(result.size(), is(0));
  }

  @Test
  public void testCollectWithTrueWhereClause() throws Exception {
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(),
            0,
            "whereClause",
            testRouting,
            TO_COLLECT_TEST_REF,
            EMPTY_PROJECTIONS);
    collectNode.whereClause(
        new WhereClause(
            new Function(
                AndOperator.INFO,
                Arrays.<Symbol>asList(Literal.newLiteral(true), Literal.newLiteral(true)))));
    collectNode.maxRowGranularity(RowGranularity.NODE);
    Bucket result = getBucket(collectNode);
    assertThat(result, contains(isRow((short) 1)));
  }

  @Test
  public void testCollectWithNullWhereClause() throws Exception {
    EqOperator op =
        (EqOperator)
            functions.get(
                new FunctionIdent(
                    EqOperator.NAME,
                    ImmutableList.<DataType>of(DataTypes.INTEGER, DataTypes.INTEGER)));
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(),
            0,
            "whereClause",
            testRouting,
            TO_COLLECT_TEST_REF,
            EMPTY_PROJECTIONS);
    collectNode.whereClause(
        new WhereClause(
            new Function(op.info(), Arrays.<Symbol>asList(Literal.NULL, Literal.NULL))));
    Bucket result = getBucket(collectNode);
    assertThat(result.size(), is(0));
  }

  private Bucket getBucket(CollectPhase collectNode)
      throws InterruptedException, ExecutionException {
    CollectingProjector cd = new CollectingProjector();
    JobExecutionContext.Builder builder = jobContextService.newBuilder(collectNode.jobId());
    JobCollectContext jobCollectContext =
        new JobCollectContext(
            collectNode.jobId(), collectNode, operation, RAM_ACCOUNTING_CONTEXT, cd);
    builder.addSubContext(collectNode.executionPhaseId(), jobCollectContext);
    JobExecutionContext context = jobContextService.createContext(builder);
    cd.startProjection(jobCollectContext);
    operation.collect(collectNode, cd, jobCollectContext);
    return cd.result().get();
  }

  @Test
  public void testCollectShardExpressions() throws Exception {
    List<Symbol> toCollect = ImmutableList.<Symbol>of(testShardIdReference);
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(), 0, "shardCollect", shardRouting(0, 1), toCollect, EMPTY_PROJECTIONS);
    collectNode.maxRowGranularity(RowGranularity.SHARD);

    Bucket result = getBucket(collectNode);
    assertThat(result.size(), is(2));
    assertThat(result, containsInAnyOrder(isRow(0), isRow(1)));
  }

  @Test
  public void testCollectShardExpressionsWhereShardIdIs0() throws Exception {
    EqOperator op =
        (EqOperator)
            functions.get(
                new FunctionIdent(
                    EqOperator.NAME,
                    ImmutableList.<DataType>of(DataTypes.INTEGER, DataTypes.INTEGER)));

    List<Symbol> toCollect = ImmutableList.<Symbol>of(testShardIdReference);
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(), 0, "shardCollect", shardRouting(0, 1), toCollect, EMPTY_PROJECTIONS);
    collectNode.whereClause(
        new WhereClause(
            new Function(op.info(), Arrays.asList(testShardIdReference, Literal.newLiteral(0)))));
    collectNode.maxRowGranularity(RowGranularity.SHARD);
    Bucket result = getBucket(collectNode);
    assertThat(result, contains(isRow(0)));
  }

  @Test
  public void testCollectShardExpressionsLiteralsAndNodeExpressions() throws Exception {
    CollectPhase collectNode =
        new CollectPhase(
            UUID.randomUUID(),
            0,
            "shardCollect",
            shardRouting(0, 1),
            Arrays.asList(testShardIdReference, Literal.newLiteral(true), testNodeReference),
            EMPTY_PROJECTIONS);
    collectNode.maxRowGranularity(RowGranularity.SHARD);
    Bucket result = getBucket(collectNode);
    assertThat(result.size(), is(2));
    assertThat(result, containsInAnyOrder(isRow(0, true, (short) 1), isRow(1, true, (short) 1)));
  }
}
  @Test
  public void testDuelIntegers() throws Exception {
    final String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("byte")
            .field("type", "byte")
            .startObject("fielddata")
            .field("format", "doc_values")
            .endObject()
            .endObject()
            .startObject("short")
            .field("type", "short")
            .startObject("fielddata")
            .field("format", "doc_values")
            .endObject()
            .endObject()
            .startObject("integer")
            .field("type", "integer")
            .startObject("fielddata")
            .field("format", "doc_values")
            .endObject()
            .endObject()
            .startObject("long")
            .field("type", "long")
            .startObject("fielddata")
            .field("format", "doc_values")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

    final DocumentMapper mapper = mapperService.documentMapperParser().parse(mapping);
    Random random = getRandom();
    int atLeast = scaledRandomIntBetween(1000, 1500);
    final int maxNumValues = randomBoolean() ? 1 : randomIntBetween(2, 40);
    byte[] values = new byte[maxNumValues];
    for (int i = 0; i < atLeast; i++) {
      int numValues = randomInt(maxNumValues);
      // FD loses values if they are duplicated, so we must deduplicate for this test
      Set<Byte> vals = new HashSet<Byte>();
      for (int j = 0; j < numValues; ++j) {
        vals.add(randomByte());
      }

      numValues = vals.size();
      int upto = 0;
      for (Byte bb : vals) {
        values[upto++] = bb.byteValue();
      }

      XContentBuilder doc = XContentFactory.jsonBuilder().startObject();
      for (String fieldName : Arrays.asList("byte", "short", "integer", "long")) {
        doc = doc.startArray(fieldName);
        for (int j = 0; j < numValues; ++j) {
          doc = doc.value(values[j]);
        }
        doc = doc.endArray();
      }
      doc = doc.endObject();

      final ParsedDocument d = mapper.parse("type", Integer.toString(i), doc.bytes());

      writer.addDocument(d.rootDoc());
      if (random.nextInt(10) == 0) {
        refreshReader();
      }
    }
    AtomicReaderContext context = refreshReader();
    Map<FieldDataType, Type> typeMap = new HashMap<>();
    typeMap.put(
        new FieldDataType("byte", ImmutableSettings.builder().put("format", "array")),
        Type.Integer);
    typeMap.put(
        new FieldDataType("short", ImmutableSettings.builder().put("format", "array")),
        Type.Integer);
    typeMap.put(
        new FieldDataType("int", ImmutableSettings.builder().put("format", "array")), Type.Integer);
    typeMap.put(
        new FieldDataType("long", ImmutableSettings.builder().put("format", "array")), Type.Long);
    typeMap.put(
        new FieldDataType("byte", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Integer);
    typeMap.put(
        new FieldDataType("short", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Integer);
    typeMap.put(
        new FieldDataType("int", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Integer);
    typeMap.put(
        new FieldDataType("long", ImmutableSettings.builder().put("format", "doc_values")),
        Type.Long);
    ArrayList<Entry<FieldDataType, Type>> list = new ArrayList<>(typeMap.entrySet());
    while (!list.isEmpty()) {
      Entry<FieldDataType, Type> left;
      Entry<FieldDataType, Type> right;
      if (list.size() > 1) {
        left = list.remove(random.nextInt(list.size()));
        right = list.remove(random.nextInt(list.size()));
      } else {
        right = left = list.remove(0);
      }
      ifdService.clear();
      IndexNumericFieldData leftFieldData =
          getForField(left.getKey(), left.getValue().name().toLowerCase(Locale.ROOT));
      ifdService.clear();
      IndexNumericFieldData rightFieldData =
          getForField(right.getKey(), right.getValue().name().toLowerCase(Locale.ROOT));

      duelFieldDataLong(random, context, leftFieldData, rightFieldData);
      duelFieldDataLong(random, context, rightFieldData, leftFieldData);

      DirectoryReader perSegment = DirectoryReader.open(writer, true);
      CompositeReaderContext composite = perSegment.getContext();
      List<AtomicReaderContext> leaves = composite.leaves();
      for (AtomicReaderContext atomicReaderContext : leaves) {
        duelFieldDataLong(random, atomicReaderContext, leftFieldData, rightFieldData);
      }
    }
  }