public ClauseContext getClauseContext(final User searcher, final TerminalClause terminalClause) {
    final Operator operator = terminalClause.getOperator();

    if (!handlesOperator(operator)) {
      return ClauseContextImpl.createGlobalClauseContext();
    }

    final Set<Project> associatedProjects =
        getAssociatedProjectsFromClause(searcher, terminalClause);
    final Set<ProjectIssueTypeContext> contexts = getContextsForProjects(associatedProjects);
    return contexts.isEmpty()
        ? ClauseContextImpl.createGlobalClauseContext()
        : new ClauseContextImpl(contexts);
  }
  public ClauseContext getClauseContext(final User searcher, final TerminalClause terminalClause) {
    final Operator operator = terminalClause.getOperator();
    boolean isEquality = isEqualityOperator(operator);

    if (!handlesOperator(operator)) {
      return ClauseContextImpl.createGlobalClauseContext();
    }

    final Set<ProjectIssueTypeContext> projectIssueTypeContexts =
        getContextFromStatusValues(searcher, terminalClause, isEquality);

    return projectIssueTypeContexts.isEmpty()
        ? ClauseContextImpl.createGlobalClauseContext()
        : new ClauseContextImpl(projectIssueTypeContexts);
  }
  @Test
  public void testGlobalSchemeConfig() throws Exception {
    final TerminalClause testClause = new TerminalClauseImpl("one", Operator.LIKE, "fine");
    final ClauseContext context3 = createContextForProjects(64);

    final FieldConfigScheme scheme = mock(FieldConfigScheme.class);

    when(customField.getConfigurationSchemes()).thenReturn(Arrays.asList(scheme, scheme, scheme));
    when(fieldConfigSchemeClauseContextUtil.getContextForConfigScheme(theUser, scheme))
        .thenReturn(context3);
    when(scheme.isGlobal()).thenReturn(false, true);

    final CascadingSelectCustomFieldClauseContextFactory factory =
        new CascadingSelectCustomFieldClauseContextFactory(
            customField,
            contextSetUtil,
            jqlSelectOptionsUtil,
            fieldConfigSchemeClauseContextUtil,
            jqlOperandResolver,
            jqlCascadingSelectLiteralUtil,
            operatorUsageValidator);

    assertEquals(
        ClauseContextImpl.createGlobalClauseContext(),
        factory.getClauseContext(theUser, testClause));
  }
  public ClauseContext getClauseContext(final User searcher, final TerminalClause terminalClause) {
    final Operator operator = terminalClause.getOperator();
    if (!handlesOperator(operator)) {
      return ClauseContextImpl.createGlobalClauseContext();
    }

    final List<QueryLiteral> values =
        jqlOperandResolver.getValues(searcher, terminalClause.getOperand(), terminalClause);
    Set<String> issueTypeIds = new HashSet<String>();
    if (values != null) {
      for (QueryLiteral value : values) {
        // if we have an empty literal, the Global context will not impact on any existing contexts,
        // so do nothing
        if (!value.isEmpty()) {
          issueTypeIds.addAll(getIds(value));
        }
      }
    }

    if (!issueTypeIds.isEmpty() && isNegationOperator(operator)) {
      final Set<String> allIssueTypes = new HashSet<String>(constantsManager.getAllIssueTypeIds());
      allIssueTypes.removeAll(issueTypeIds);
      issueTypeIds = allIssueTypes;
    }

    if (issueTypeIds.isEmpty()) {
      return ClauseContextImpl.createGlobalClauseContext();
    }

    final Set<ProjectIssueTypeContext> contexts = new HashSet<ProjectIssueTypeContext>();
    for (String issueTypeId : issueTypeIds) {
      contexts.add(
          new ProjectIssueTypeContextImpl(
              AllProjectsContext.INSTANCE, new IssueTypeContextImpl(issueTypeId)));
    }

    return new ClauseContextImpl(contexts);
  }
  @Test
  public void testNoGeneratedContext() throws Exception {
    final TerminalClause testClause = new TerminalClauseImpl("one", Operator.EQUALS, "fine");
    final MockOption option1 = new MockOption(null, null, null, null, null, 25L);
    final MockOption option2 = new MockOption(null, null, null, null, null, 26L);
    final ClauseContext context1 = createContextForProjects(1, 2);
    final ClauseContext context2 = createContextForProjects(5);

    final FieldConfigScheme scheme = mock(FieldConfigScheme.class);
    when(customField.getConfigurationSchemes()).thenReturn(Arrays.asList(scheme, scheme));
    when(jqlSelectOptionsUtil.getOptionsForScheme(scheme))
        .thenReturn(Arrays.<Option>asList(option1, option2));
    when(fieldConfigSchemeClauseContextUtil.getContextForConfigScheme(theUser, scheme))
        .thenReturn(context1, context2);
    when(contextSetUtil.union(CollectionBuilder.newBuilder(context1, context2).asSet()))
        .thenReturn(new ClauseContextImpl());

    final CascadingSelectCustomFieldClauseContextFactory factory =
        new CascadingSelectCustomFieldClauseContextFactory(
            customField,
            contextSetUtil,
            jqlSelectOptionsUtil,
            fieldConfigSchemeClauseContextUtil,
            jqlOperandResolver,
            jqlCascadingSelectLiteralUtil,
            operatorUsageValidator) {
          @Override
          void fillOptions(
              final User user,
              final TerminalClause clause,
              final Set<Option> positiveOption,
              final Set<Option> negativeOption) {

            assertEquals(theUser, user);
            assertEquals(testClause, clause);

            positiveOption.add(option1);
          }
        };

    assertEquals(
        ClauseContextImpl.createGlobalClauseContext(),
        factory.getClauseContext(theUser, testClause));
  }
  @Test
  public void testNullSchemes() throws Exception {
    final TerminalClause clause = new TerminalClauseImpl("one", Operator.EQUALS, "fine");

    when(customField.getConfigurationSchemes()).thenReturn(null);

    final CascadingSelectCustomFieldClauseContextFactory factory =
        new CascadingSelectCustomFieldClauseContextFactory(
            customField,
            contextSetUtil,
            jqlSelectOptionsUtil,
            fieldConfigSchemeClauseContextUtil,
            jqlOperandResolver,
            jqlCascadingSelectLiteralUtil,
            operatorUsageValidator);
    final ClauseContext clauseContext = factory.getClauseContext(theUser, clause);

    assertEquals(ClauseContextImpl.createGlobalClauseContext(), clauseContext);
  }
  @Test
  public void testPositiveQueryWithNoPositive() throws Exception {
    final TerminalClause testClause = new TerminalClauseImpl("one", Operator.EQUALS, "fine");
    final MockOption option1 = new MockOption(null, null, null, null, null, 25L);
    final MockOption option2 = new MockOption(null, null, null, null, null, 26L);
    final MockOption option2child2 = new MockOption(option2, null, null, null, null, 28L);
    final MockOption option2child1 = new MockOption(option2, null, null, null, null, 27L);
    option2.setChildOptions(Arrays.asList(option2child1, option2child2));

    final FieldConfigScheme scheme = mock(FieldConfigScheme.class);

    when(customField.getConfigurationSchemes()).thenReturn(Arrays.asList(scheme, scheme));
    when(scheme.isGlobal()).thenReturn(true);

    final CascadingSelectCustomFieldClauseContextFactory factory =
        new CascadingSelectCustomFieldClauseContextFactory(
            customField,
            contextSetUtil,
            jqlSelectOptionsUtil,
            fieldConfigSchemeClauseContextUtil,
            jqlOperandResolver,
            jqlCascadingSelectLiteralUtil,
            operatorUsageValidator) {
          @Override
          void fillOptions(
              final User user,
              final TerminalClause clause,
              final Set<Option> positiveOption,
              final Set<Option> negativeOption) {
            assertEquals(theUser, user);
            assertEquals(testClause, clause);

            negativeOption.add(option2);
            negativeOption.add(option1);
          }
        };

    assertEquals(
        ClauseContextImpl.createGlobalClauseContext(),
        factory.getClauseContext(theUser, testClause));
  }
  @Test
  public void testPositiveQueryWithPositiveAndEmpty() throws Exception {
    final TerminalClause testClause = new TerminalClauseImpl("one", Operator.EQUALS, "fine");
    final ClauseContext context1 = createContextForProjects(1, 56);

    final FieldConfigScheme scheme = mock(FieldConfigScheme.class);

    when(customField.getConfigurationSchemes()).thenReturn(Arrays.asList(scheme, scheme));
    when(scheme.isGlobal()).thenReturn(false, true);
    when(fieldConfigSchemeClauseContextUtil.getContextForConfigScheme(theUser, scheme))
        .thenReturn(context1);

    final CascadingSelectCustomFieldClauseContextFactory factory =
        new CascadingSelectCustomFieldClauseContextFactory(
            customField,
            contextSetUtil,
            jqlSelectOptionsUtil,
            fieldConfigSchemeClauseContextUtil,
            jqlOperandResolver,
            jqlCascadingSelectLiteralUtil,
            operatorUsageValidator) {
          @Override
          void fillOptions(
              final User user,
              final TerminalClause clause,
              final Set<Option> positiveOption,
              final Set<Option> negativeOption) {
            assertEquals(theUser, user);
            assertEquals(testClause, clause);

            positiveOption.add(null);
          }
        };

    assertEquals(
        ClauseContextImpl.createGlobalClauseContext(),
        factory.getClauseContext(theUser, testClause));
  }