@Test public void send_pull_request_characteristic() throws Exception { String orgName = "MyOrg"; when(properties.organizationKey()).thenReturn(Optional.of(orgName)); String branchName = "feature"; String pullRequestId = "pr-123"; when(branchConfiguration.branchName()).thenReturn(branchName); when(branchConfiguration.branchType()).thenReturn(PULL_REQUEST); when(branchConfiguration.pullRequestKey()).thenReturn(pullRequestId); WsResponse response = mock(WsResponse.class); PipedOutputStream out = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(out); Ce.SubmitResponse.newBuilder().build().writeTo(out); out.close(); when(response.failIfNotSuccessful()).thenReturn(response); when(response.contentStream()).thenReturn(in); when(wsClient.call(any(WsRequest.class))).thenReturn(response); underTest.upload(temp.newFile()); ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class); verify(wsClient).call(capture.capture()); WsRequest wsRequest = capture.getValue(); assertThat(wsRequest.getParameters().getKeys()).hasSize(3); assertThat(wsRequest.getParameters().getValues("organization")).containsExactly(orgName); assertThat(wsRequest.getParameters().getValues("projectKey")).containsExactly("org.sonarsource.sonarqube:sonarqube"); assertThat(wsRequest.getParameters().getValues("characteristic")) .containsExactlyInAnyOrder("pullRequest=" + pullRequestId); }
@Test public void submit_task_to_the_queue_and_ask_for_immediate_processing() { when(reportSubmitter.submit(eq(organizationKey), eq("my_project"), isNull(), eq("My Project"), anyMap(), any())).thenReturn(A_CE_TASK); Ce.SubmitResponse submitResponse = tester.newRequest() .setParam("projectKey", "my_project") .setParam("projectName", "My Project") .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar") .setMethod("POST") .executeProtobuf(Ce.SubmitResponse.class); verify(reportSubmitter).submit(eq(organizationKey), eq("my_project"), isNull(), eq("My Project"), anyMap(), any()); assertThat(submitResponse.getTaskId()).isEqualTo("TASK_1"); assertThat(submitResponse.getProjectId()).isEqualTo(PROJECT_UUID); }
@Override public void handle(Request wsRequest, Response wsResponse) throws Exception { String organizationKey = wsRequest.getParam(PARAM_ORGANIZATION_KEY) .emptyAsNull() .or(defaultOrganizationProvider.get()::getKey); String projectKey = wsRequest.mandatoryParam(PARAM_PROJECT_KEY); String projectBranch = wsRequest.param(PARAM_PROJECT_BRANCH); String projectName = abbreviate(defaultIfBlank(wsRequest.param(PARAM_PROJECT_NAME), projectKey), MAX_COMPONENT_NAME_LENGTH); Map<String, String> characteristics = parseTaskCharacteristics(wsRequest); try (InputStream report = new BufferedInputStream(wsRequest.mandatoryParamAsPart(PARAM_REPORT_DATA).getInputStream())) { CeTask task = reportSubmitter.submit(organizationKey, projectKey, projectBranch, projectName, characteristics, report); Ce.SubmitResponse submitResponse = Ce.SubmitResponse.newBuilder() .setTaskId(task.getUuid()) .setProjectId(task.getComponentUuid()) .build(); WsUtils.writeProtobuf(submitResponse, wsRequest, wsResponse); } }
/** * * This is part of the internal API. * This is a POST request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/ce/submit">Further information about this action online (including a response example)</a> * @since 5.2 */ public SubmitResponse submit(SubmitRequest request) { return call( new PostRequest(path("submit")) .setParam("characteristic", request.getCharacteristic()) .setParam("organization", request.getOrganization()) .setParam("projectBranch", request.getProjectBranch()) .setParam("projectKey", request.getProjectKey()) .setParam("projectName", request.getProjectName()) .setParam("report", request.getReport()), SubmitResponse.parser()); }
@Test public void test_send_branches_characteristics() throws Exception { String orgName = "MyOrg"; when(properties.organizationKey()).thenReturn(Optional.of(orgName)); String branchName = "feature"; when(branchConfiguration.branchName()).thenReturn(branchName); when(branchConfiguration.branchType()).thenReturn(SHORT); WsResponse response = mock(WsResponse.class); PipedOutputStream out = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(out); Ce.SubmitResponse.newBuilder().build().writeTo(out); out.close(); when(response.failIfNotSuccessful()).thenReturn(response); when(response.contentStream()).thenReturn(in); when(wsClient.call(any(WsRequest.class))).thenReturn(response); underTest.upload(temp.newFile()); ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class); verify(wsClient).call(capture.capture()); WsRequest wsRequest = capture.getValue(); assertThat(wsRequest.getParameters().getKeys()).hasSize(3); assertThat(wsRequest.getParameters().getValues("organization")).containsExactly(orgName); assertThat(wsRequest.getParameters().getValues("projectKey")).containsExactly("org.sonarsource.sonarqube:sonarqube"); assertThat(wsRequest.getParameters().getValues("characteristic")) .containsExactlyInAnyOrder("branch=" + branchName, "branchType=" + SHORT.name()); }
/** * * This is part of the internal API. * This is a POST request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/ce/submit">Further information about this action online (including a response example)</a> * @since 5.2 */ public SubmitResponse submit(SubmitRequest request) { return call( new PostRequest(path("submit")) .setParam("characteristic", request.getCharacteristic()) .setParam("organization", request.getOrganization()) .setParam("projectBranch", request.getProjectBranch()) .setParam("projectKey", request.getProjectKey()) .setParam("projectName", request.getProjectName()) .setParam("report", request.getReport()), SubmitResponse.parser()); }
@Override public void handle(Request wsRequest, Response wsResponse) throws Exception { String organizationKey = wsRequest.getParam(PARAM_ORGANIZATION_KEY) .emptyAsNull() .or(defaultOrganizationProvider.get()::getKey); String projectKey = wsRequest.mandatoryParam(PARAM_PROJECT_KEY); String deprecatedBranch = wsRequest.param(PARAM_PROJECT_BRANCH); String projectName = abbreviate(defaultIfBlank(wsRequest.param(PARAM_PROJECT_NAME), projectKey), MAX_COMPONENT_NAME_LENGTH); Map<String, String> characteristics = parseTaskCharacteristics(wsRequest); try (InputStream report = new BufferedInputStream(wsRequest.mandatoryParamAsPart(PARAM_REPORT_DATA).getInputStream())) { CeTask task = reportSubmitter.submit(organizationKey, projectKey, deprecatedBranch, projectName, characteristics, report); Ce.SubmitResponse submitResponse = Ce.SubmitResponse.newBuilder() .setTaskId(task.getUuid()) .setProjectId(task.getComponent().get().getUuid()) .build(); WsUtils.writeProtobuf(submitResponse, wsRequest, wsResponse); } }
@Test public void test_ws_parameters() throws Exception { when(properties.organizationKey()).thenReturn(Optional.of("MyOrg")); WsResponse response = mock(WsResponse.class); PipedOutputStream out = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(out); Ce.SubmitResponse.newBuilder().build().writeTo(out); out.close(); when(response.failIfNotSuccessful()).thenReturn(response); when(response.contentStream()).thenReturn(in); when(wsClient.call(any(WsRequest.class))).thenReturn(response); underTest.upload(temp.newFile()); ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class); verify(wsClient).call(capture.capture()); WsRequest wsRequest = capture.getValue(); assertThat(wsRequest.getParameters().getKeys()).containsOnly("organization", "projectKey"); assertThat(wsRequest.getParameters().getValue("organization")).isEqualTo("MyOrg"); assertThat(wsRequest.getParameters().getValue("projectKey")).isEqualTo("org.sonarsource.sonarqube:sonarqube"); }
@Test public void submit_task_with_characteristics() { when(reportSubmitter.submit(eq(organizationKey), eq("my_project"), isNull(), eq("My Project"), anyMap(), any())).thenReturn(A_CE_TASK); String[] characteristics = {"branch=foo", "pullRequest=123", "unsupported=bar"}; Ce.SubmitResponse submitResponse = tester.newRequest() .setParam("projectKey", "my_project") .setParam("projectName", "My Project") .setMultiParam("characteristic", asList(characteristics)) .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar") .setMethod("POST") .executeProtobuf(Ce.SubmitResponse.class); assertThat(submitResponse.getTaskId()).isEqualTo("TASK_1"); verify(reportSubmitter).submit(eq(organizationKey), eq("my_project"), isNull(), eq("My Project"), map.capture(), any()); // unsupported characteristics are ignored assertThat(map.getValue()).containsExactly(entry("branch", "foo"), entry("pullRequest", "123")); }