<commit_before>package edu.umd.cs.findbugs.flybush;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;

import com.google.common.collect.Sets;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.WebCloudProtoUtil;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.Evaluation;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.FindIssues;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.Issue;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.LogIn;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.SetBugLink;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.SetBugLink.Builder;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.UpdateIssueTimestamps;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.UpdateIssueTimestamps.IssueGroup;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.UploadEvaluation;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.UploadIssues;

import static edu.umd.cs.findbugs.cloud.appEngine.protobuf.WebCloudProtoUtil.encodeHash;
import static edu.umd.cs.findbugs.flybush.UpdateServlet.ONE_DAY_IN_MILLIS;

@SuppressWarnings({ "UnusedDeclaration" })
public abstract class UpdateServletTest extends AbstractFlybushServletTest {

    @Override
    protected AbstractFlybushServlet createServlet() {
        return new UpdateServlet();
    }

    @SuppressWarnings({ "unchecked" })
    public void testExpireSqlSessions() throws Exception {
        DbUser oldUser = persistenceHelper.createDbUser("http://some.website", "<EMAIL>");
        SqlCloudSession oldSession = persistenceHelper.createSqlCloudSession(100, new Date(System.currentTimeMillis() - 8
                * ONE_DAY_IN_MILLIS), oldUser.createKeyObject(), "<EMAIL>");
        DbUser recentUser = persistenceHelper.createDbUser("http://some.website2", "<EMAIL>");
        SqlCloudSession recentSession = persistenceHelper.createSqlCloudSession(101, new Date(System.currentTimeMillis() - 6
                * ONE_DAY_IN_MILLIS), recentUser.createKeyObject(), "<EMAIL>");
        getPersistenceManager().makePersistentAll(oldUser, recentUser, oldSession, recentSession);

        assertEquals("<EMAIL>", getDbUser(findSqlSession(100).get(0).getUser()).getEmail());
        assertEquals("<EMAIL>", getDbUser(findSqlSession(101).get(0).getUser()).getEmail());

        executeGet("/expire-sql-sessions");

        assertEquals(0, findSqlSession(100).size());
        assertEquals("<EMAIL>", getDbUser(findSqlSession(101).get(0).getUser()).getEmail());
    }

    @SuppressWarnings({ "unchecked" })
    public void testUpdateDbJune29CommentStyle() throws Exception {
        DbIssue issue = createDbIssue("fad2");
        DbEvaluation eval = createEvaluation(issue, "someone", 200);
        persistenceHelper.convertToOldCommentStyleForTesting(eval);
        getPersistenceManager().makePersistentAll(issue);
        assertTrue(persistenceHelper.isOldCommentStyle(eval));

        executeGet("/update-db-jun29");
        getPersistenceManager().refreshAll(issue);
        assertFalse(persistenceHelper.isOldCommentStyle(eval));
    }

    @SuppressWarnings({ "unchecked" })
    public void testUpdateDbJune29PrimaryClass() throws Exception {
        DbIssue issue = createDbIssue("fad2");
        DbEvaluation eval = createEvaluation(issue, "someone", 200);
        eval.setPrimaryClass(null);
        assertNull(eval.getPrimaryClass());
        getPersistenceManager().makePersistentAll(issue);

        executeGet("/update-db-jun29");
        getPersistenceManager().refreshAll(issue);
        assertEquals("my.class", eval.getPrimaryClass());
    }

    @SuppressWarnings({ "unchecked" })
    public void testUpdateDbJune29Package() throws Exception {
        DbIssue issue = createDbIssue("fad2");
        issue.setPrimaryClass("a.b.c.D");
        DbEvaluation eval = createEvaluation(issue, "someone", 200);
        eval.setPackages(Sets.<String> newHashSet());
        getPersistenceManager().makePersistentAll(issue);

        executeGet("/update-db-jun29");
        getPersistenceManager().refreshAll(issue);
        assertTrue(eval.getPackages().contains("a"));
        assertTrue(eval.getPackages().contains("a.b"));
        assertTrue(eval.getPackages().contains("a.b.c"));
    }

    @SuppressWarnings({ "unchecked" })
    public void testUpdateEvaluationEmails() throws Exception {
        // setup
        DbUser userA = persistenceHelper.createDbUser("http://some.website", "<EMAIL>");
        DbEvaluation evalA = persistenceHelper.createDbEvaluation();
        evalA.setWho(userA.createKeyObject());
        evalA.setEmail("<EMAIL>");

        DbUser userB = persistenceHelper.createDbUser("http://some.website2", "<EMAIL>");
        DbEvaluation evalB = persistenceHelper.createDbEvaluation();
        evalB.setWho(userB.createKeyObject());

        PersistenceManager pm = getPersistenceManager();
        pm.makePersistentAll(userA, userB, evalA, evalB);

        // execute
        executeGet("/update-evaluation-emails");

        // verify
        pm.refreshAll(evalA, evalB);
        assertEquals("<EMAIL>", evalA.getEmail());
        assertEquals("<EMAIL>", evalB.getEmail());
    }

    public void testUpdateIssueTimestampsNotAuthenticated() throws Exception {
        UpdateIssueTimestamps tsCmd = UpdateIssueTimestamps
                .newBuilder()
                .setSessionId(555)
                .addIssueGroups(
                        IssueGroup.newBuilder().setTimestamp(SAMPLE_TIMESTAMP + 50).addIssueHashes(encodeHash("fad1")).build())
                .build();
        executePost("/update-issue-timestamps", tsCmd.toByteArray());
        checkResponse(403);
    }

    public void testUpdateIssueTimestamps() throws Exception {
        createCloudSession(555);

        PersistenceManager pm = getPersistenceManager();
        DbIssue issue1 = createDbIssue("fad1");
        DbIssue issue2 = createDbIssue("fad2");
        pm.makePersistentAll(issue1, issue2);

        UpdateIssueTimestamps tsCmd = UpdateIssueTimestamps
                .newBuilder()
                .setSessionId(555)
                .addIssueGroups(
                        IssueGroup.newBuilder().setTimestamp(SAMPLE_TIMESTAMP + 50).addIssueHashes(encodeHash("fad1"))
                                .addIssueHashes(encodeHash("fad2")).build()).build();
        executePost("/update-issue-timestamps", tsCmd.toByteArray());
        pm.refreshAll(issue1, issue2);
        checkResponse(200);
        assertEquals(SAMPLE_TIMESTAMP + 50, issue1.getFirstSeen());
        assertEquals(SAMPLE_TIMESTAMP + 50, issue2.getFirstSeen());
    }

    public void testUpdateIssueTimestampsZeroStored() throws Exception {
        createCloudSession(555);

        PersistenceManager pm = getPersistenceManager();
        DbIssue issue1 = createDbIssue("fad1");
        DbIssue issue2 = createDbIssue("fad2");
        issue2.setFirstSeen(0);
        pm.makePersistentAll(issue1, issue2);

        UpdateIssueTimestamps tsCmd = UpdateIssueTimestamps
                .newBuilder()
                .setSessionId(555)
                .addIssueGroups(
                        IssueGroup.newBuilder().setTimestamp(SAMPLE_TIMESTAMP + 50).addIssueHashes(encodeHash("fad1"))
                                .addIssueHashes(encodeHash("fad2")).build()).build();
        executePost("/update-issue-timestamps", tsCmd.toByteArray());
        pm.refreshAll(issue1, issue2);
        checkResponse(200);
        assertEquals(SAMPLE_TIMESTAMP + 50, issue1.getFirstSeen());
        assertEquals(SAMPLE_TIMESTAMP + 50, issue2.getFirstSeen());
    }

    public void testUpdateIssueTimestampsIgnoreOldTimestamp() throws Exception {
        createCloudSession(555);

        PersistenceManager pm = getPersistenceManager();
        DbIssue issue1 = createDbIssue("fad1");
        pm.makePersistentAll(issue1, issue1);

        UpdateIssueTimestamps tsCmd = UpdateIssueTimestamps.newBuilder().setSessionId(555)
                .addIssueGroups(IssueGroup.newBuilder().setTimestamp(50).addIssueHashes(encodeHash("fad1")).build()).build();
        executePost("/update-issue-timestamps", tsCmd.toByteArray());
        pm.refreshAll(issue1);
        checkResponse(200);
        assertEquals(SAMPLE_TIMESTAMP + 100, issue1.getFirstSeen());
    }

    public void testUpdateIssueTimestampsMultipleGroups() throws Exception {
        createCloudSession(555);
<commit_msg>make timestamp relative to sample time stamp<commit_after>package edu.umd.cs.findbugs.flybush;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;

import com.google.common.collect.Sets;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.WebCloudProtoUtil;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.Evaluation;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.FindIssues;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.Issue;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.LogIn;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.SetBugLink;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.SetBugLink.Builder;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.UpdateIssueTimestamps;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.UpdateIssueTimestamps.IssueGroup;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.UploadEvaluation;
import edu.umd.cs.findbugs.cloud.appEngine.protobuf.ProtoClasses.UploadIssues;

import static edu.umd.cs.findbugs.cloud.appEngine.protobuf.WebCloudProtoUtil.encodeHash;
import static edu.umd.cs.findbugs.flybush.UpdateServlet.ONE_DAY_IN_MILLIS;

@SuppressWarnings({ "UnusedDeclaration" })
public abstract class UpdateServletTest extends AbstractFlybushServletTest {

    @Override
    protected AbstractFlybushServlet createServlet() {
        return new UpdateServlet();
    }

    @SuppressWarnings({ "unchecked" })
    public void testExpireSqlSessions() throws Exception {
        DbUser oldUser = persistenceHelper.createDbUser("http://some.website", "<EMAIL>");
        SqlCloudSession oldSession = persistenceHelper.createSqlCloudSession(100, new Date(System.currentTimeMillis() - 8
                * ONE_DAY_IN_MILLIS), oldUser.createKeyObject(), "<EMAIL>");
        DbUser recentUser = persistenceHelper.createDbUser("http://some.website2", "<EMAIL>");
        SqlCloudSession recentSession = persistenceHelper.createSqlCloudSession(101, new Date(System.currentTimeMillis() - 6
                * ONE_DAY_IN_MILLIS), recentUser.createKeyObject(), "<EMAIL>");
        getPersistenceManager().makePersistentAll(oldUser, recentUser, oldSession, recentSession);

        assertEquals("<EMAIL>", getDbUser(findSqlSession(100).get(0).getUser()).getEmail());
        assertEquals("<EMAIL>", getDbUser(findSqlSession(101).get(0).getUser()).getEmail());

        executeGet("/expire-sql-sessions");

        assertEquals(0, findSqlSession(100).size());
        assertEquals("<EMAIL>", getDbUser(findSqlSession(101).get(0).getUser()).getEmail());
    }

    @SuppressWarnings({ "unchecked" })
    public void testUpdateDbJune29CommentStyle() throws Exception {
        DbIssue issue = createDbIssue("fad2");
        DbEvaluation eval = createEvaluation(issue, "someone", 200);
        persistenceHelper.convertToOldCommentStyleForTesting(eval);
        getPersistenceManager().makePersistentAll(issue);
        assertTrue(persistenceHelper.isOldCommentStyle(eval));

        executeGet("/update-db-jun29");
        getPersistenceManager().refreshAll(issue);
        assertFalse(persistenceHelper.isOldCommentStyle(eval));
    }

    @SuppressWarnings({ "unchecked" })
    public void testUpdateDbJune29PrimaryClass() throws Exception {
        DbIssue issue = createDbIssue("fad2");
        DbEvaluation eval = createEvaluation(issue, "someone", 200);
        eval.setPrimaryClass(null);
        assertNull(eval.getPrimaryClass());
        getPersistenceManager().makePersistentAll(issue);

        executeGet("/update-db-jun29");
        getPersistenceManager().refreshAll(issue);
        assertEquals("my.class", eval.getPrimaryClass());
    }

    @SuppressWarnings({ "unchecked" })
    public void testUpdateDbJune29Package() throws Exception {
        DbIssue issue = createDbIssue("fad2");
        issue.setPrimaryClass("a.b.c.D");
        DbEvaluation eval = createEvaluation(issue, "someone", 200);
        eval.setPackages(Sets.<String> newHashSet());
        getPersistenceManager().makePersistentAll(issue);

        executeGet("/update-db-jun29");
        getPersistenceManager().refreshAll(issue);
        assertTrue(eval.getPackages().contains("a"));
        assertTrue(eval.getPackages().contains("a.b"));
        assertTrue(eval.getPackages().contains("a.b.c"));
    }

    @SuppressWarnings({ "unchecked" })
    public void testUpdateEvaluationEmails() throws Exception {
        // setup
        DbUser userA = persistenceHelper.createDbUser("http://some.website", "<EMAIL>");
        DbEvaluation evalA = persistenceHelper.createDbEvaluation();
        evalA.setWho(userA.createKeyObject());
        evalA.setEmail("<EMAIL>");

        DbUser userB = persistenceHelper.createDbUser("http://some.website2", "<EMAIL>");
        DbEvaluation evalB = persistenceHelper.createDbEvaluation();
        evalB.setWho(userB.createKeyObject());

        PersistenceManager pm = getPersistenceManager();
        pm.makePersistentAll(userA, userB, evalA, evalB);

        // execute
        executeGet("/update-evaluation-emails");

        // verify
        pm.refreshAll(evalA, evalB);
        assertEquals("<EMAIL>", evalA.getEmail());
        assertEquals("<EMAIL>", evalB.getEmail());
    }

    public void testUpdateIssueTimestampsNotAuthenticated() throws Exception {
        UpdateIssueTimestamps tsCmd = UpdateIssueTimestamps
                .newBuilder()
                .setSessionId(555)
                .addIssueGroups(
                        IssueGroup.newBuilder().setTimestamp(SAMPLE_TIMESTAMP + 50).addIssueHashes(encodeHash("fad1")).build())
                .build();
        executePost("/update-issue-timestamps", tsCmd.toByteArray());
        checkResponse(403);
    }

    public void testUpdateIssueTimestamps() throws Exception {
        createCloudSession(555);

        PersistenceManager pm = getPersistenceManager();
        DbIssue issue1 = createDbIssue("fad1");
        DbIssue issue2 = createDbIssue("fad2");
        pm.makePersistentAll(issue1, issue2);

        UpdateIssueTimestamps tsCmd = UpdateIssueTimestamps
                .newBuilder()
                .setSessionId(555)
                .addIssueGroups(
                        IssueGroup.newBuilder().setTimestamp(SAMPLE_TIMESTAMP + 50).addIssueHashes(encodeHash("fad1"))
                                .addIssueHashes(encodeHash("fad2")).build()).build();
        executePost("/update-issue-timestamps", tsCmd.toByteArray());
        pm.refreshAll(issue1, issue2);
        checkResponse(200);
        assertEquals(SAMPLE_TIMESTAMP + 50, issue1.getFirstSeen());
        assertEquals(SAMPLE_TIMESTAMP + 50, issue2.getFirstSeen());
    }

    public void testUpdateIssueTimestampsZeroStored() throws Exception {
        createCloudSession(555);

        PersistenceManager pm = getPersistenceManager();
        DbIssue issue1 = createDbIssue("fad1");
        DbIssue issue2 = createDbIssue("fad2");
        issue2.setFirstSeen(0);
        pm.makePersistentAll(issue1, issue2);

        UpdateIssueTimestamps tsCmd = UpdateIssueTimestamps
                .newBuilder()
                .setSessionId(555)
                .addIssueGroups(
                        IssueGroup.newBuilder().setTimestamp(SAMPLE_TIMESTAMP + 50).addIssueHashes(encodeHash("fad1"))
                                .addIssueHashes(encodeHash("fad2")).build()).build();
        executePost("/update-issue-timestamps", tsCmd.toByteArray());
        pm.refreshAll(issue1, issue2);
        checkResponse(200);
        assertEquals(SAMPLE_TIMESTAMP + 50, issue1.getFirstSeen());
        assertEquals(SAMPLE_TIMESTAMP + 50, issue2.getFirstSeen());
    }

    public void testUpdateIssueTimestampsIgnoreOldTimestamp() throws Exception {
        createCloudSession(555);

        PersistenceManager pm = getPersistenceManager();
        DbIssue issue1 = createDbIssue("fad1");
        pm.makePersistentAll(issue1, issue1);

        UpdateIssueTimestamps tsCmd = UpdateIssueTimestamps.newBuilder().setSessionId(555)
                .addIssueGroups(IssueGroup.newBuilder().setTimestamp(SAMPLE_TIMESTAMP + 50).addIssueHashes(encodeHash("fad1")).build()).build();
        executePost("/update-issue-timestamps", tsCmd.toByteArray());
        pm.refreshAll(issue1);
        checkResponse(200);
        assertEquals(SAMPLE_TIMESTAMP + 100, issue1.getFirstSeen());
    }

    public void testUpdateIssueTimestampsMultipleGroups() throws Exception {
        createCloudSession(555);

{'fraction_non_alphanumeric': 0.11996993524514339, 'fraction_numerical': 0.01607308048103608, 'mean_word_length': 3.574715683681566, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1795156', 'n_tokens_mistral': 5247, 'n_tokens_neox': 4829, 'n_words': 683}