Example #1
0
  public void testArchivedStorage()
      throws MalformedURLException, CoreException, IOException, NoSuchFieldException,
          IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
          InvocationTargetException {
    IssueStorage storage = IssueStorage.getInstance();
    long ts = System.currentTimeMillis();
    String id1 = "id1";
    String id2 = "id2";

    String url = "http://test/bugzilla";
    String qName = "SomeQuery";

    storage.storeArchivedQueryIssues(url, qName, new String[] {id1, id2});
    Map<String, Long> read = storage.readArchivedQueryIssues(url, qName);

    assertEquals(2, read.size());
    assertTrue(ts <= read.get(id1));
    assertTrue(ts <= read.get(id2));

    // wait a sec and set TTL so that the archived issues shuld be cleaned up
    try {
      Thread.currentThread().sleep(1000);
    } catch (InterruptedException ex) {
      Exceptions.printStackTrace(ex);
    }
    Field f = BugtrackingConfig.class.getDeclaredField("DEFAULT_ARCHIVED_TTL");
    f.setAccessible(true);
    f.set(BugtrackingConfig.getInstance(), new Long(0)); // zero time to live

    read = storage.readArchivedQueryIssues(url, qName);
    assertEquals(0, read.size());
  }
Example #2
0
 private File getStorageRootFile()
     throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException,
         InvocationTargetException {
   IssueStorage storage = IssueStorage.getInstance();
   Method m = storage.getClass().getDeclaredMethod("getStorageRootFile");
   m.setAccessible(true);
   return (File) m.invoke(storage, new Object[0]);
 }
Example #3
0
 private void emptyStorage()
     throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException,
         InvocationTargetException, NoSuchFieldException {
   File f = getStorageRootFile();
   BugtrackingUtil.deleteRecursively(f);
   Field field = IssueStorage.class.getDeclaredField("storage");
   field.setAccessible(true);
   field.set(IssueStorage.getInstance(), f);
 }
Example #4
0
  public DefaultIssue createManualIssue(
      String componentKey,
      RuleKey ruleKey,
      @Nullable Integer line,
      @Nullable String message,
      @Nullable String severity) {
    verifyLoggedIn();

    DbSession dbSession = dbClient.openSession(false);
    try {
      Optional<ComponentDto> componentOptional =
          dbClient.componentDao().selectByKey(dbSession, componentKey);
      if (!componentOptional.isPresent()) {
        throw new BadRequestException(
            String.format("Component with key '%s' not found", componentKey));
      }
      ComponentDto component = componentOptional.get();
      ComponentDto project =
          dbClient.componentDao().selectOrFailByUuid(dbSession, component.projectUuid());

      userSession.checkComponentPermission(UserRole.USER, project.getKey());
      if (!ruleKey.isManual()) {
        throw new IllegalArgumentException(
            "Issues can be created only on rules marked as 'manual': " + ruleKey);
      }
      Rule rule = getNullableRuleByKey(ruleKey);
      if (rule == null) {
        throw new IllegalArgumentException("Unknown rule: " + ruleKey);
      }

      DefaultIssue issue =
          new DefaultIssueBuilder()
              .componentKey(component.getKey())
              .projectKey(project.getKey())
              .line(line)
              .message(!Strings.isNullOrEmpty(message) ? message : rule.getName())
              .severity(Objects.firstNonNull(severity, Severity.MAJOR))
              .ruleKey(ruleKey)
              .reporter(userSession.getLogin())
              .assignee(findSourceLineUser(dbSession, component.uuid(), line))
              .build();

      Date now = new Date();
      issue.setCreationDate(now);
      issue.setUpdateDate(now);
      issueStorage.save(issue);
      return issue;
    } finally {
      dbSession.close();
    }
  }
Example #5
0
 void saveIssue(
     DbSession session, DefaultIssue issue, IssueChangeContext context, @Nullable String comment) {
   String projectKey = issue.projectKey();
   if (projectKey == null) {
     throw new IllegalStateException(String.format("Issue '%s' has no project key", issue.key()));
   }
   issueStorage.save(session, issue);
   Rule rule = getNullableRuleByKey(issue.ruleKey());
   ComponentDto project = dbClient.componentDao().selectOrFailByKey(session, projectKey);
   notificationService.scheduleForSending(
       new IssueChangeNotification()
           .setIssue(issue)
           .setChangeAuthorLogin(context.login())
           .setRuleName(rule != null ? rule.getName() : null)
           .setProject(project.getKey(), project.name())
           .setComponent(dbClient.componentDao().selectOrFailByKey(session, issue.componentKey()))
           .setComment(comment));
 }
Example #6
0
  public void testStorage() throws MalformedURLException, CoreException, IOException {
    final IssueStorage storage = IssueStorage.getInstance();

    Map<String, String> attr1 = new HashMap<String, String>();
    attr1.put("dummy1", "dummy3");
    attr1.put("dummy2", "dummy2");
    attr1.put("dummy3", "dummy1");
    Map<String, String> attr2 = new HashMap<String, String>();
    attr2.put("dummy5", "dummy7");
    attr2.put("dummy6", "dummy6");
    attr2.put("dummy7", "dummy5");
    String id1 = "id1";
    String id2 = "id2";

    String url = "http://test/bugzilla";
    String qName = "SomeQuery";
    DummyIssue i1 = new DummyIssue(id1, attr1);
    DummyIssue i2 = new DummyIssue(id2, attr2);

    storage.storeQuery(url, qName, new String[] {id1, id2});

    long lm = System.currentTimeMillis();
    IssueCache<DummyIssue, Object> cache = getCache();
    IssueEntry ie1 = cache.new IssueEntry(i1, i1.id, attr1, -1, -1, false, lm);
    IssueEntry ie2 = cache.new IssueEntry(i2, i2.id, attr2, -1, -1, false, lm);

    storage.storeIssue(url, ie1);
    storage.storeIssue(url, ie2);

    List<String> issues = storage.readQuery(url, qName);
    assertTrue(issues.contains(id1));
    assertTrue(issues.contains(id2));

    ie1 = cache.new IssueEntry(i1, i1.id, null, -1, -1, false, lm);
    ie2 = cache.new IssueEntry(i2, i2.id, null, -1, -1, false, lm);
    storage.readIssue(url, ie1);
    if (ie1.getSeenAttributes() == null) fail("missing issue id [" + id1 + "]");
    assertAttribute(ie1.getSeenAttributes(), "dummy1", "dummy3");
    assertAttribute(ie1.getSeenAttributes(), "dummy2", "dummy2");
    assertAttribute(ie1.getSeenAttributes(), "dummy3", "dummy1");
    storage.readIssue(url, ie2);
    if (ie2.getSeenAttributes() == null) fail("missing issue id [" + id2 + "]");
    assertAttribute(ie2.getSeenAttributes(), "dummy5", "dummy7");
    assertAttribute(ie2.getSeenAttributes(), "dummy6", "dummy6");
    assertAttribute(ie2.getSeenAttributes(), "dummy7", "dummy5");

    // create another query
    String qName2 = "SomeQuery2";
    storage.storeQuery(url, qName2, new String[] {id1, id2});
    issues = storage.readQuery(url, qName2);
    assertEquals(2, issues.size());

    // remove it
    storage.removeQuery(url, qName2);
    issues = storage.readQuery(url, qName2);
    // it's gone
    assertEquals(0, issues.size());
    // first query still exists
    issues = storage.readQuery(url, qName);
    assertEquals(2, issues.size());
  }
Example #7
0
  public void testCleanup()
      throws MalformedURLException, CoreException, IOException, NoSuchFieldException,
          IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
          InvocationTargetException {
    IssueStorage storage = IssueStorage.getInstance();
    long ts = System.currentTimeMillis();
    Map<String, String> attr = new HashMap<String, String>();
    String id1 = "id1";
    String id2 = "id2";

    String url = "http://test/bugzilla";
    String qName = "SomeQuery";

    DummyIssue i1 = new DummyIssue(id1, attr);
    DummyIssue i2 = new DummyIssue(id2, attr);

    long lm = System.currentTimeMillis();
    IssueCache<DummyIssue, Object> cache = getCache();
    IssueEntry ie1 = cache.new IssueEntry(i1, i1.id, attr, -1, -1, false, lm);
    IssueEntry ie2 = cache.new IssueEntry(i2, i2.id, attr, -1, -1, false, lm);

    storage.storeIssue(url, ie1);
    storage.storeIssue(url, ie2);

    // store query
    storage.storeQuery(url, qName, new String[] {id1, id2});
    List<String> stored = storage.readQuery(url, qName);

    assertEquals(2, stored.size());
    File folder = getNameSpaceFolder(url);
    File[] issueFiles =
        folder.listFiles(
            new FilenameFilter() {
              public boolean accept(File dir, String name) {
                return name.endsWith(".i");
              }
            });
    assertEquals(2, issueFiles.length); // issues are there

    // cleanup, yet issues are living in a stored query
    storage.cleanup();
    issueFiles =
        folder.listFiles(
            new FilenameFilter() {
              public boolean accept(File dir, String name) {
                return name.endsWith(".i");
              }
            });
    assertEquals(2, issueFiles.length); // issues are still there

    // cleanup, yet issues are living in archive
    storage.storeQuery(url, qName, new String[] {});
    storage.storeArchivedQueryIssues(url, qName, new String[] {id1, id2});
    storage.cleanup();
    issueFiles =
        folder.listFiles(
            new FilenameFilter() {
              public boolean accept(File dir, String name) {
                return name.endsWith(".i");
              }
            });
    assertEquals(2, issueFiles.length); // issues are still there

    // cleanup, issues aren't in a query or archived
    storage.storeQuery(url, qName, new String[] {});
    storage.storeArchivedQueryIssues(url, qName, new String[] {});
    storage.cleanup();
    issueFiles =
        folder.listFiles(
            new FilenameFilter() {
              public boolean accept(File dir, String name) {
                return name.endsWith(".i");
              }
            });
    assertEquals(0, issueFiles.length); // issues are still there
  }