*/
package org.sonar.batch.phases;
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
import org.sonar.api.CoreProperties;
import org.sonar.batch.index.ResourcePersister;
import javax.persistence.Query;
-
import java.util.List;
public class UpdateStatusJob implements BatchComponent {
Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, false);
boolean isLast = (previousLastSnapshot == null || previousLastSnapshot.getCreatedAt().before(snapshot.getCreatedAt()));
setFlags(snapshot, isLast, Snapshot.STATUS_PROCESSED);
- if (!settings.getBoolean(CoreProperties.DRY_RUN)) {
+ logSuccess(LoggerFactory.getLogger(getClass()));
+ }
+
+ @VisibleForTesting
+ void logSuccess(Logger logger) {
+ if (settings.getBoolean(CoreProperties.DRY_RUN)) {
+ logger.info("ANALYSIS SUCCESSFUL");
+
+ } else {
String baseUrl = settings.getString(CoreProperties.SERVER_BASE_URL);
if (baseUrl.equals(settings.getDefaultValue(CoreProperties.SERVER_BASE_URL))) {
// If server base URL was not configured in Sonar server then is is better to take URL configured on batch side
baseUrl += "/";
}
String url = baseUrl + "dashboard/index/" + project.getKey();
- LoggerFactory.getLogger(getClass()).info("ANALYSIS SUCCESSFUL, you can browse {}", url);
+ logger.info("ANALYSIS SUCCESSFUL, you can browse {}", url);
}
}
package org.sonar.batch.phases;
import org.junit.Test;
+import org.slf4j.Logger;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.database.DatabaseSession;
import org.sonar.batch.bootstrap.ServerClient;
import org.sonar.batch.index.DefaultResourcePersister;
import org.sonar.batch.index.ResourceCache;
+import org.sonar.batch.index.ResourcePersister;
import org.sonar.batch.index.SnapshotCache;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import javax.persistence.Query;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
public class UpdateStatusJobTest extends AbstractDbUnitTestCase {
setupData("sharedFixture", fixture);
DatabaseSession session = getSession();
- UpdateStatusJob sensor = new UpdateStatusJob(new Settings().appendProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"), mock(ServerClient.class), session,
- new DefaultResourcePersister(session, mock(ResourcePermissions.class), mock(SnapshotCache.class), mock(ResourceCache.class)),
- mock(Project.class), loadSnapshot(snapshotId));
- sensor.execute();
+ UpdateStatusJob job = new UpdateStatusJob(new Settings().appendProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"), mock(ServerClient.class), session,
+ new DefaultResourcePersister(session, mock(ResourcePermissions.class), mock(SnapshotCache.class), mock(ResourceCache.class)),
+ mock(Project.class), loadSnapshot(snapshotId));
+ job.execute();
checkTables(fixture, "snapshots");
}
query.setParameter("id", id);
return (Snapshot) query.getSingleResult();
}
+
+ @Test
+ public void should_log_successful_analysis() throws Exception {
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/");
+ Project project = new Project("struts");
+ UpdateStatusJob job = new UpdateStatusJob(settings, mock(ServerClient.class), mock(DatabaseSession.class),
+ mock(ResourcePersister.class), project, mock(Snapshot.class));
+
+ Logger logger = mock(Logger.class);
+ job.logSuccess(logger);
+
+ verify(logger).info("ANALYSIS SUCCESSFUL, you can browse {}", "http://myserver/dashboard/index/struts");
+ }
+
+ @Test
+ public void should_log_successful_dry_run_analysis() throws Exception {
+ Settings settings = new Settings();
+ settings.setProperty("sonar.dryRun", true);
+ Project project = new Project("struts");
+ UpdateStatusJob job = new UpdateStatusJob(settings, mock(ServerClient.class), mock(DatabaseSession.class),
+ mock(ResourcePersister.class), project, mock(Snapshot.class));
+
+ Logger logger = mock(Logger.class);
+ job.logSuccess(logger);
+
+ verify(logger).info("ANALYSIS SUCCESSFUL");
+ }
}