import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
+import org.sonar.batch.protocol.input.FileData;
import org.sonar.batch.protocol.input.ProjectRepositories;
-import org.sonar.server.ws.WsTester;
+import org.sonar.server.ws.TestResponse;
+import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.WsBatch.WsProjectResponse;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.sonar.test.JsonAssert.assertJson;
public class ProjectActionTest {
ProjectDataLoader projectDataLoader = mock(ProjectDataLoader.class);
- WsTester tester;
+ WsActionTester ws;
@Before
public void setUp() {
- tester = new WsTester(new BatchWs(mock(BatchIndex.class), new ProjectAction(projectDataLoader)));
+ ws = new WsActionTester(new ProjectAction(projectDataLoader));
}
@Test
ArgumentCaptor<ProjectDataQuery> queryArgumentCaptor = ArgumentCaptor.forClass(ProjectDataQuery.class);
when(projectDataLoader.load(queryArgumentCaptor.capture())).thenReturn(projectReferentials);
- WsTester.TestRequest request = tester.newGetRequest("batch", "project")
+ TestResponse response = ws.newRequest()
.setParam("key", projectKey)
.setParam("profile", "Default")
- .setParam("preview", "false");
- request.execute().assertJson("{\"settingsByModule\": {}}");
+ .setParam("preview", "false")
+ .execute();
+ assertJson(response.getInput()).isSimilarTo("{\"settingsByModule\": {}}");
assertThat(queryArgumentCaptor.getValue().getModuleKey()).isEqualTo(projectKey);
assertThat(queryArgumentCaptor.getValue().getProfileName()).isEqualTo("Default");
assertThat(queryArgumentCaptor.getValue().isIssuesMode()).isFalse();
}
+ /**
+ * SONAR-7084
+ */
+ @Test
+ public void do_not_fail_when_a_path_is_null() throws Exception {
+ String projectKey = "org.codehaus.sonar:sonar";
+
+ ProjectRepositories projectRepositories = new ProjectRepositories().addFileData("module-1", null, new FileData(null, null));
+ when(projectDataLoader.load(any(ProjectDataQuery.class))).thenReturn(projectRepositories);
+
+ TestResponse result = ws.newRequest()
+ .setMediaType(MediaTypes.PROTOBUF)
+ .setParam("key", projectKey)
+ .setParam("profile", "Default")
+ .execute();
+
+ WsProjectResponse wsProjectResponse = WsProjectResponse.parseFrom(result.getInputStream());
+ assertThat(wsProjectResponse.getFileDataByModuleAndPath()).isEmpty();
+ }
}
import org.sonar.batch.protocol.input.FileData;
import org.sonar.batch.protocol.input.ProjectRepositories;
import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.source.FileSourceDao;
import org.sonar.db.source.FileSourceDto;
import org.sonar.db.source.FileSourceDto.Type;
-import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.qualityprofile.QProfileName;
import org.sonar.server.tester.ServerTester;
DbSession dbSession;
- ProjectDataLoader loader;
+ ProjectDataLoader underTest;
@Before
public void before() {
tester.clearDbAndIndexes();
dbSession = tester.get(DbClient.class).openSession(false);
- loader = tester.get(ProjectDataLoader.class);
+ underTest = tester.get(ProjectDataLoader.class);
}
@After
// Project properties
tester.get(DbClient.class).propertiesDao().insertProperty(
- dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId())
- );
+ dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId()));
tester.get(DbClient.class).propertiesDao().insertProperty(
- dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId())
- );
+ dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId()));
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
+
Map<String, String> projectSettings = ref.settings(project.key());
assertThat(projectSettings).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
- "sonar.jira.login.secured", "john"
- ));
+ "sonar.jira.login.secured", "john"));
}
@Test
// Project properties
tester.get(DbClient.class).propertiesDao().insertProperty(
- dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId())
- );
+ dbSession, new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR").setResourceId(project.getId()));
tester.get(DbClient.class).propertiesDao().insertProperty(
- dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId())
- );
+ dbSession, new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(project.getId()));
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()).setIssuesMode(true));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()).setIssuesMode(true));
Map<String, String> projectSettings = ref.settings(project.key());
assertThat(projectSettings).isEqualTo(ImmutableMap.of(
- "sonar.jira.project.key", "SONAR"
- ));
+ "sonar.jira.project.key", "SONAR"));
}
@Test
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
- "sonar.jira.login.secured", "john"
- ));
+ "sonar.jira.login.secured", "john"));
assertThat(ref.settings(module.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-SERVER",
"sonar.jira.login.secured", "john",
- "sonar.coverage.exclusions", "**/*.java"
- ));
+ "sonar.coverage.exclusions", "**/*.java"));
}
@Test
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
- "sonar.jira.login.secured", "john"
- ));
+ "sonar.jira.login.secured", "john"));
assertThat(ref.settings(module.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
- "sonar.jira.login.secured", "john"
- ));
+ "sonar.jira.login.secured", "john"));
}
@Test
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
- "sonar.jira.login.secured", "john"
- ));
+ "sonar.jira.login.secured", "john"));
assertThat(ref.settings(module.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-SERVER",
"sonar.jira.login.secured", "john",
- "sonar.coverage.exclusions", "**/*.java"
- ));
+ "sonar.coverage.exclusions", "**/*.java"));
assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-SERVER-DAO",
"sonar.jira.login.secured", "john",
- "sonar.coverage.exclusions", "**/*.java"
- ));
+ "sonar.coverage.exclusions", "**/*.java"));
}
@Test
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
- "sonar.jira.login.secured", "john"
- ));
+ "sonar.jira.login.secured", "john"));
assertThat(ref.settings(module1.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-SERVER",
"sonar.jira.login.secured", "john",
- "sonar.coverage.exclusions", "**/*.java"
- ));
+ "sonar.coverage.exclusions", "**/*.java"));
assertThat(ref.settings(module2.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-APPLICATION",
- "sonar.jira.login.secured", "john"
- ));
+ "sonar.jira.login.secured", "john"));
}
@Test
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
- "sonar.jira.login.secured", "john"
- ));
+ "sonar.jira.login.secured", "john"));
}
@Test
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
assertThat(ref.settings(project.key())).isEmpty();
assertThat(ref.settings(module.key())).isEmpty();
assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john",
- "sonar.coverage.exclusions", "**/*.java"
- ));
+ "sonar.coverage.exclusions", "**/*.java"));
}
@Test
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
assertThat(ref.settings(project.key())).isEmpty();
assertThat(ref.settings(module.key())).isEmpty();
assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john",
- "sonar.coverage.exclusions", "**/*.java"
- ));
+ "sonar.coverage.exclusions", "**/*.java"));
}
@Test
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
assertThat(ref.settings(project.key())).isEmpty();
assertThat(ref.settings(module.key())).isEmpty();
assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john",
- "sonar.coverage.exclusions", "**/*.java"
- ));
+ "sonar.coverage.exclusions", "**/*.java"));
}
@Test
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(subModule.key()));
assertThat(ref.settings(project.key())).isEmpty();
assertThat(ref.settings(module.key())).isEmpty();
assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-SERVER",
"sonar.jira.login.secured", "john",
- "sonar.coverage.exclusions", "**/*.java"
- ));
+ "sonar.coverage.exclusions", "**/*.java"));
}
@Test
dbSession.commit();
try {
- loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+ underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(ForbiddenException.class).hasMessage(Messages.NO_PERMISSION);
dbSession.commit();
try {
- loader.load(ProjectDataQuery.create().setModuleKey(project.key()).setIssuesMode(false));
+ underTest.load(ProjectDataQuery.create().setModuleKey(project.key()).setIssuesMode(false));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(ForbiddenException.class).hasMessage(
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
assertThat(ref.fileDataByPath(project.key())).hasSize(1);
FileData fileData = ref.fileData(project.key(), file.path());
assertThat(fileData.hash()).isEqualTo("123456");
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(project.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(project.key()));
assertThat(ref.fileData(project.key(), projectFile.path()).hash()).isEqualTo("123456");
assertThat(ref.fileData(module.key(), moduleFile.path()).hash()).isEqualTo("789456");
}
dbSession.commit();
- ProjectRepositories ref = loader.load(ProjectDataQuery.create().setModuleKey(module.key()));
+ ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setModuleKey(module.key()));
assertThat(ref.fileData(module.key(), moduleFile.path()).hash()).isEqualTo("789456");
assertThat(ref.fileData(module.key(), moduleFile.path()).revision()).isEqualTo("123456789");
assertThat(ref.fileData(project.key(), projectFile.path())).isNull();