@Test
  public void testGetSearchClauseValuesPresentParamsNull() throws Exception {
    final String searcherId = "searcherId";

    final FieldValuesHolderImpl valuesHolder = new FieldValuesHolderImpl();
    valuesHolder.put(searcherId, null);

    final VersionCustomFieldSearchInputTransformer transformer =
        new VersionCustomFieldSearchInputTransformer(
            searcherId,
            new ClauseNames("clauseName"),
            mockController.getMock(CustomField.class),
            mockController.getMock(VersionIndexInfoResolver.class),
            mockController.getMock(JqlOperandResolver.class),
            mockController.getMock(FieldFlagOperandRegistry.class),
            searchContextVisibilityChecker,
            versionResolver,
            customFieldInputHelper,
            versionManager) {
          @Override
          protected Clause getClauseFromParams(
              final User searcher,
              final CustomFieldParams params,
              final FieldValuesHolder fieldValuesHolder) {
            fail("Should not have been called");
            return null;
          }
        };

    mockController.replay();

    assertNull(transformer.getSearchClause(null, valuesHolder));

    mockController.verify();
  }
  @Test
  public void testGetSearchClauseValuesNotPresent() throws Exception {
    final FieldValuesHolderImpl valuesHolder = new FieldValuesHolderImpl();

    VersionCustomFieldSearchInputTransformer transformer = createTransformer("cf[1000]");

    assertNull(transformer.getSearchClause(null, valuesHolder));

    mockController.verify();
  }
  @Test
  public void testGetClauseFromParamsFilteredValuesEmpty() throws Exception {
    final CustomFieldParams customFieldParams = mockController.getMock(CustomFieldParams.class);
    customFieldParams.getAllValues();
    mockController.setReturnValue(CollectionBuilder.newBuilder("").asMutableSet());

    final VersionCustomFieldSearchInputTransformer inputTransformer =
        createTransformer("cf[12345]");

    assertNull(inputTransformer.getClauseFromParams(theUser, customFieldParams, null));

    mockController.verify();
  }
  @Test
  public void testGetSearchClauseOneProjectIsFlag() throws Exception {
    final CustomFieldParamsImpl customFieldParams = new CustomFieldParamsImpl();
    customFieldParams.addValue(Collections.singleton("123"));
    final Map<String, Object> map =
        MapBuilder.<String, Object>newBuilder()
            .add(urlParameterName, customFieldParams)
            .add(
                SystemSearchConstants.forProject().getUrlParameter(),
                Collections.singletonList("-1"))
            .toMap();
    FieldValuesHolder values = new FieldValuesHolderImpl(map);
    final AtomicBoolean specialCalled = new AtomicBoolean(false);

    final IndexedInputHelper helper =
        new DefaultIndexedInputHelper<Version>(
            indexInfoResolver,
            operandResolver,
            fieldFlagOperandRegistry,
            searchContextVisibilityChecker) {
          @Override
          public Clause getClauseForNavigatorValues(final String clauseName, final Set values) {
            return null;
          }
        };

    transformer =
        new VersionCustomFieldSearchInputTransformer(
            urlParameterName,
            clauseNames,
            customField,
            indexInfoResolver,
            operandResolver,
            fieldFlagOperandRegistry,
            searchContextVisibilityChecker,
            versionResolver,
            customFieldInputHelper,
            versionManager) {
          @Override
          protected DefaultIndexedInputHelper getDefaultIndexedInputHelper() {
            throw new UnsupportedOperationException(
                "Should not have called through to the special indexed input helper");
          }

          @Override
          protected IndexedInputHelper getIndexedInputHelper() {
            specialCalled.set(true);
            return helper;
          }

          @Override
          protected String getClauseName(final User searcher, final ClauseNames clauseNames) {
            return primaryClauseName;
          }
        };
    replay();

    transformer.getSearchClause(theUser, values);
    assertTrue(specialCalled.get());
  }
  @Test
  public void testGetSearchClauseValuesPresentParamsNotEmpty() throws Exception {
    final String searcherId = "searcherId";
    final String clauseName = "clauseName";

    final CustomFieldParamsImpl customFieldParams = new CustomFieldParamsImpl();
    customFieldParams.addValue(Collections.singleton("dontcare"));

    final FieldValuesHolderImpl valuesHolder = new FieldValuesHolderImpl();
    valuesHolder.put(searcherId, customFieldParams);

    final AtomicBoolean wasCalled = new AtomicBoolean(false);
    final Clause expectedClause = new TerminalClauseImpl(clauseName, Operator.EQUALS, "blah");

    final VersionCustomFieldSearchInputTransformer transformer =
        new VersionCustomFieldSearchInputTransformer(
            searcherId,
            new ClauseNames(clauseName),
            mockController.getMock(CustomField.class),
            mockController.getMock(VersionIndexInfoResolver.class),
            mockController.getMock(JqlOperandResolver.class),
            mockController.getMock(FieldFlagOperandRegistry.class),
            searchContextVisibilityChecker,
            versionResolver,
            customFieldInputHelper,
            versionManager) {
          @Override
          protected Clause getClauseFromParams(
              final User searcher,
              final CustomFieldParams params,
              final FieldValuesHolder fieldValuesHolder) {
            wasCalled.set(true);
            return expectedClause;
          }
        };

    mockController.replay();

    assertEquals(expectedClause, transformer.getSearchClause(null, valuesHolder));
    assertTrue(wasCalled.get());

    mockController.verify();
  }
  @Test
  public void testGetParamsFromSearchRequest() throws Exception {
    final String name = "clauseName";
    final User theUser = null;
    final CustomField field = mockController.getMock(CustomField.class);

    final QueryImpl query = new QueryImpl();

    final IndexedInputHelper indexInputHelper = mockController.getMock(IndexedInputHelper.class);
    indexInputHelper.getAllNavigatorValuesForMatchingClauses(
        theUser, new ClauseNames(name), query, searchContext);
    final Set<String> valuesAsStrings = Collections.singleton("Hi");
    mockController.setReturnValue(valuesAsStrings);

    final CustomFieldParams expectedParams = new CustomFieldParamsImpl(field, valuesAsStrings);

    final VersionCustomFieldSearchInputTransformer transformer =
        new VersionCustomFieldSearchInputTransformer(
            name,
            new ClauseNames(name),
            field,
            mockController.getMock(VersionIndexInfoResolver.class),
            mockController.getMock(JqlOperandResolver.class),
            mockController.getMock(FieldFlagOperandRegistry.class),
            searchContextVisibilityChecker,
            versionResolver,
            customFieldInputHelper,
            versionManager) {
          @Override
          IndexedInputHelper getIndexedInputHelper() {
            return indexInputHelper;
          }
        };

    mockController.replay();

    assertEquals(
        expectedParams, transformer.getParamsFromSearchRequest(null, query, searchContext));

    mockController.verify();
  }
  @Test
  public void testGetClauseFromParamsValuesGetFiltered() throws Exception {
    final String clauseName = "clauseName";

    final CustomField customField = mockController.getMock(CustomField.class);
    EasyMock.expect(customField.getName()).andStubReturn("ABC");
    EasyMock.expect(customFieldInputHelper.getUniqueClauseName(theUser, clauseName, "ABC"))
        .andStubReturn(clauseName);

    final CustomFieldParamsImpl customFieldParams = new CustomFieldParamsImpl();
    customFieldParams.addValue(CollectionBuilder.newBuilder("55", "-1", "").asMutableSet());

    final Set<String> expectedFilteredValues = CollectionBuilder.newBuilder("55").asMutableSet();

    final Clause expectedClause = mockController.getMock(Clause.class);

    final IndexedInputHelper indexedInputHelper = mockController.getMock(IndexedInputHelper.class);
    indexedInputHelper.getClauseForNavigatorValues(clauseName, expectedFilteredValues);
    mockController.setReturnValue(expectedClause);

    transformer =
        new VersionCustomFieldSearchInputTransformer(
            clauseName,
            new ClauseNames(clauseName),
            customField,
            indexInfoResolver,
            operandResolver,
            fieldFlagOperandRegistry,
            searchContextVisibilityChecker,
            versionResolver,
            customFieldInputHelper,
            versionManager) {
          @Override
          IndexedInputHelper getIndexedInputHelper() {
            return indexedInputHelper;
          }

          @Override
          boolean isVersionsNotRelatedToProjects(
              final Set<String> versionIdsFromHolder, final FieldValuesHolder fieldValuesHolder) {
            return false;
          }
        };

    mockController.replay();

    FieldValuesHolder holderValues =
        new FieldValuesHolderImpl(MapBuilder.singletonMap(urlParameterName, customFieldParams));
    assertEquals(
        expectedClause, transformer.getClauseFromParams(theUser, customFieldParams, holderValues));

    mockController.verify();
  }
  @Test
  public void testDoRelevantClausesFitDelegates() throws Exception {
    final boolean theResult = false;
    final Query query = null;

    final MockControl mockNavigatorStructureCheckerControl =
        MockClassControl.createControl(NavigatorStructureChecker.class);
    final NavigatorStructureChecker mockNavigatorStructureChecker =
        (NavigatorStructureChecker) mockNavigatorStructureCheckerControl.getMock();
    mockNavigatorStructureChecker.checkSearchRequest(query, searchContext);
    mockNavigatorStructureCheckerControl.setReturnValue(theResult);
    mockNavigatorStructureCheckerControl.replay();

    @SuppressWarnings({"unchecked"})
    final VersionCustomFieldSearchInputTransformer transformer =
        new VersionCustomFieldSearchInputTransformer(
            "clauseName",
            new ClauseNames("clauseName"),
            mockController.getMock(CustomField.class),
            mockController.getMock(VersionIndexInfoResolver.class),
            mockController.getMock(JqlOperandResolver.class),
            mockController.getMock(FieldFlagOperandRegistry.class),
            searchContextVisibilityChecker,
            versionResolver,
            customFieldInputHelper,
            versionManager) {
          @Override
          NavigatorStructureChecker createNavigatorStructureChecker() {
            return mockNavigatorStructureChecker;
          }
        };

    mockController.replay();

    assertEquals(theResult, transformer.doRelevantClausesFitFilterForm(null, query, searchContext));
    mockNavigatorStructureCheckerControl.verify();
    mockController.verify();
  }
  @Test
  public void testGetSearchClauseNoNavigatorValues() throws Exception {
    FieldValuesHolder values = new FieldValuesHolderImpl(MapBuilder.emptyMap());
    transformer =
        new VersionCustomFieldSearchInputTransformer(
            urlParameterName,
            clauseNames,
            customField,
            indexInfoResolver,
            operandResolver,
            fieldFlagOperandRegistry,
            searchContextVisibilityChecker,
            versionResolver,
            customFieldInputHelper,
            versionManager);
    replay();

    final Clause result = transformer.getSearchClause(theUser, values);

    assertNull(result);
  }
 @Test
 public void testGetParamsFromSearchRequestNullSearchRequest() throws Exception {
   VersionCustomFieldSearchInputTransformer transformer = createTransformer("cf[1000]");
   assertNull(transformer.getParamsFromSearchRequest(null, null, searchContext));
   mockController.verify();
 }