Example #1
0
 public static final TitanProperty queryHiddenFunctionalProperty(
     InternalTitanVertex node, TitanKey propType) {
   assert ((InternalTitanType) propType).isHidden() : "Expected hidden property key";
   assert propType.isFunctional() : "Expected functional property  type";
   return Iterators.getOnlyElement(
       new AtomicTitanQuery(node).includeHidden().type(propType).propertyIterator(), null);
 }
  @Test
  public void load_issues_from_report() throws Exception {
    when(issueFilter.accept(any(DefaultIssue.class), eq(FILE))).thenReturn(true);
    fileSourceRepository.addLines(FILE_REF, "line 1;", "line 2;");
    ScannerReport.Issue reportIssue =
        ScannerReport.Issue.newBuilder()
            .setTextRange(TextRange.newBuilder().setStartLine(2).build())
            .setMsg("the message")
            .setRuleRepository("java")
            .setRuleKey("S001")
            .setSeverity(Constants.Severity.BLOCKER)
            .setGap(3.14)
            .build();
    reportReader.putIssues(FILE.getReportAttributes().getRef(), asList(reportIssue));
    Input<DefaultIssue> input = underTest.create(FILE);

    Collection<DefaultIssue> issues = input.getIssues();
    assertThat(issues).hasSize(1);
    DefaultIssue issue = Iterators.getOnlyElement(issues.iterator());

    // fields set by analysis report
    assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("java", "S001"));
    assertThat(issue.severity()).isEqualTo(Severity.BLOCKER);
    assertThat(issue.line()).isEqualTo(2);
    assertThat(issue.effortToFix()).isEqualTo(3.14);
    assertThat(issue.gap()).isEqualTo(3.14);
    assertThat(issue.message()).isEqualTo("the message");

    // fields set by compute engine
    assertThat(issue.checksum()).isEqualTo(input.getLineHashSequence().getHashForLine(2));
    assertThat(issue.tags()).isEmpty();
    assertInitializedIssue(issue);
  }
  @Test
  public void load_issues() throws Exception {
    reportReader.putFileSourceLines(FILE.getRef(), "line 1;", "line 2;");
    BatchReport.Issue reportIssue =
        BatchReport.Issue.newBuilder()
            .setLine(2)
            .setMsg("the message")
            .setRuleRepository("java")
            .setRuleKey("S001")
            .setSeverity(Constants.Severity.BLOCKER)
            .setEffortToFix(3.14)
            .build();
    reportReader.putIssues(FILE.getRef(), asList(reportIssue));
    Input<DefaultIssue> input = underTest.create(FILE);

    Collection<DefaultIssue> issues = input.getIssues();
    assertThat(issues).hasSize(1);
    DefaultIssue issue = Iterators.getOnlyElement(issues.iterator());

    // fields set by analysis report
    assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("java", "S001"));
    assertThat(issue.severity()).isEqualTo(Severity.BLOCKER);
    assertThat(issue.line()).isEqualTo(2);
    assertThat(issue.effortToFix()).isEqualTo(3.14);
    assertThat(issue.message()).isEqualTo("the message");

    // fields set by compute engine
    assertThat(issue.checksum()).isEqualTo(input.getLineHashSequence().getHashForLine(2));
    assertThat(issue.tags()).isEmpty();
    assertInitializedIssue(issue);
  }
  @Test
  public void do_not_auto_assign_existing_issues() {
    inputIssue.setIsNew(false);
    inputIssue.setAuthorLogin("charlie");
    when(scmAccountCache.getNullable("charlie")).thenReturn("char.lie");

    process();

    assertThat(Iterators.getOnlyElement(outputIssues.traverse()).assignee()).isNull();
  }
  @Test
  public void auto_assign_new_issues() {
    inputIssue.setIsNew(true);
    inputIssue.setAuthorLogin("charlie");
    when(scmAccountCache.getNullable("charlie")).thenReturn("char.lie");

    process();

    assertThat(Iterators.getOnlyElement(outputIssues.traverse()).assignee()).isEqualTo("char.lie");
  }
  @Test
  public void do_not_guess_author_of_existing_issues() {
    inputIssue.setIsNew(false);
    inputIssue.setLine(3);
    when(lineCache.lineAuthor(3)).thenReturn("charlie");

    process();

    assertThat(Iterators.getOnlyElement(outputIssues.traverse()).authorLogin()).isNull();
  }
  @Test
  public void do_not_fail_if_missing_author_for_new_issues() {
    inputIssue.setIsNew(true);
    inputIssue.setLine(3);
    when(lineCache.lineAuthor(3)).thenReturn(null);

    process();

    assertThat(Iterators.getOnlyElement(outputIssues.traverse()).authorLogin()).isNull();
  }
  @Test
  public void do_not_copy_rule_tags_on_existing_issues() {
    inputIssue.setIsNew(false);
    rule.setTags(ImmutableSet.of("bug", "performance"));
    rule.setSystemTags(ImmutableSet.of("blocker"));

    process();

    assertThat(Iterators.getOnlyElement(outputIssues.traverse()).tags()).isEmpty();
  }
  @Test
  public void guess_author_of_new_issues() {
    inputIssue.setIsNew(true);
    inputIssue.setLine(3);
    when(lineCache.lineAuthor(3)).thenReturn("charlie");

    process();

    assertThat(Iterators.getOnlyElement(outputIssues.traverse()).authorLogin())
        .isEqualTo("charlie");
  }
  @Test
  public void copy_rule_tags_on_new_issues() {
    inputIssue.setIsNew(true);
    rule.setTags(ImmutableSet.of("bug", "performance"));
    rule.setSystemTags(ImmutableSet.of("blocker"));

    process();

    assertThat(Iterators.getOnlyElement(outputIssues.traverse()).tags())
        .containsOnly("blocker", "bug", "performance");
  }
  @Test
  public void do_not_assign_default_assignee_when_not_found_in_index() {
    inputIssue.setIsNew(true);
    String wolinski = "wolinski";
    projectSettings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, wolinski);
    when(userIndex.getNullableByLogin(wolinski)).thenReturn(null);

    process();

    assertThat(Iterators.getOnlyElement(outputIssues.traverse()).assignee()).isNull();
    assertThat(logTester.logs())
        .contains(
            String.format(
                "the %s property was set with an unknown login: %s",
                CoreProperties.DEFAULT_ISSUE_ASSIGNEE, wolinski));
  }
  @Test
  public void do_not_override_author_and_assignee_set_by_old_batch_plugins() {
    inputIssue.setIsNew(true);

    // these fields were provided during project analysis, for instance
    // by developer cockpit or issue-assign plugins
    inputIssue.setAuthorLogin("charlie");
    inputIssue.setAssignee("cabu");

    process();

    // keep the values, without trying to update them
    DefaultIssue cachedIssue = Iterators.getOnlyElement(outputIssues.traverse());
    assertThat(cachedIssue.assignee()).isEqualTo("cabu");
    assertThat(cachedIssue.authorLogin()).isEqualTo("charlie");
    verifyZeroInteractions(scmAccountCache);
  }
Example #13
0
  protected Object parseResponse(String requestUrl, JsonBall response) {
    if (response == null) {
      return null;
    } else if (requestUrl.contains("import")) {
      Type type = new TypeLiteral<Map<String, Set<ParseDomain.RawDomain>>>() {}.getType();
      Map<String, Set<RawDomain>> domainMap = json.fromJson(response.toString(), type);
      Domain domain = Iterators.getOnlyElement(domainMap.get("domains").iterator()).getDomain();

      return domain;
    } else if (requestUrl.contains("export")) {
      Type type = new TypeLiteral<Map<String, String>>() {}.getType();
      Map<String, String> exportMap = json.fromJson(response.toString(), type);
      String contents = exportMap.get("contents");
      List<String> contentsAsList =
          Lists.newArrayList(Splitter.on("\n").omitEmptyStrings().split(contents));

      return contentsAsList;
    } else if (response.toString().contains("domains")) {
      Type type = new TypeLiteral<Map<String, Set<RawDomain>>>() {}.getType();
      Map<String, Set<RawDomain>> domainMap = json.fromJson(response.toString(), type);
      Set<Domain> domains =
          FluentIterable.from(domainMap.get("domains")).transform(toDomain).toSet();

      return domains;
    } else if (response.toString().contains("records")) {
      Type type = new TypeLiteral<Map<String, Set<RawRecord>>>() {}.getType();
      Map<String, Set<RawRecord>> recordMap = json.fromJson(response.toString(), type);
      Set<RecordDetail> records =
          FluentIterable.from(recordMap.get("records")).transform(toRecordDetails).toSet();

      if (isCreateSingleRecord) {
        return Iterables.getOnlyElement(records);
      } else {
        return records;
      }
    } else {
      throw new IllegalStateException(
          "Job parsing problem. Did not recognize any type in job response.\n"
              + response.toString());
    }
  }
  @Test
  public void store_issues_on_disk() {
    process();

    assertThat(Iterators.getOnlyElement(outputIssues.traverse()).key()).isEqualTo("ISSUE_A");
  }
Example #15
0
 /**
  * Returns the single element contained in {@code iterable}, or {@code defaultValue} if the
  * iterable is empty.
  *
  * @throws IllegalArgumentException if the iterator contains multiple elements
  */
 @Nullable
 public static <T> T getOnlyElement(Iterable<? extends T> iterable, @Nullable T defaultValue) {
   return Iterators.getOnlyElement(iterable.iterator(), defaultValue);
 }
Example #16
0
 /**
  * Returns the single element contained in {@code iterable}.
  *
  * @throws NoSuchElementException if the iterable is empty
  * @throws IllegalArgumentException if the iterable contains multiple elements
  */
 public static <T> T getOnlyElement(Iterable<T> iterable) {
   return Iterators.getOnlyElement(iterable.iterator());
 }