Skip to content

Commit

Permalink
fix some sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sephiroth-j committed Dec 21, 2024
1 parent 5c0f542 commit d36223d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -140,7 +141,7 @@ void testConnectionTestWithErrors() throws IOException {
doThrow(new ConnectException("oops"))
.when(call).execute();

assertThatCode(() -> uut.testConnection())
assertThatCode(uut::testConnection)
.hasMessage(Messages.ApiClient_Error_Connection("", ""))
.hasCauseInstanceOf(ConnectException.class);
verify(httpClient, times(2)).newCall(any(okhttp3.Request.class));
Expand All @@ -156,14 +157,14 @@ void testConnectionTestInternalError(JenkinsRule r) {

ApiClient uut = createClient();

assertThatCode(() -> uut.testConnection()).isInstanceOf(ApiClientException.class)
assertThatCode(uut::testConnection).isInstanceOf(ApiClientException.class)
.hasNoCause()
.hasMessage(Messages.ApiClient_Error_Connection(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), HttpResponseStatus.INTERNAL_SERVER_ERROR.reasonPhrase()));

verify(logger).log("something went wrong");
}

@Test()
@Test
void getProjectsTest(JenkinsRule r) throws ApiClientException {
server = HttpServer.create()
.host("localhost")
Expand All @@ -178,13 +179,14 @@ void getProjectsTest(JenkinsRule r) throws ApiClientException {
assertThat(Integer.valueOf(p)).isBetween(1, 3);
});
int page = Integer.parseInt(query.parameters().get("page").get(0));
switch (page) {
case 1:
return response.sendString(Mono.just("[{\"name\":\"Project 1\",\"uuid\":\"uuid-1\",\"version\":null},{\"name\":\"Project 2\",\"uuid\":\"uuid-2\",\"version\":\"null\"}]"));
case 2:
return response.sendString(Mono.just("[{\"name\":\"Project 3\",\"uuid\":\"uuid-3\",\"version\":\"1.2.3\",\"lastBomImportStr\":\"2007-12-03T10:15:30\",\"tags\":[{\"name\":\"tag1\"},{\"name\":\"tag2\"},{\"name\":null}]}]"));
}
return response.sendNotFound();
return switch (page) {
case 1 ->
response.sendString(Mono.just("[{\"name\":\"Project 1\",\"uuid\":\"uuid-1\",\"version\":null},{\"name\":\"Project 2\",\"uuid\":\"uuid-2\",\"version\":\"null\"}]"));
case 2 ->
response.sendString(Mono.just("[{\"name\":\"Project 3\",\"uuid\":\"uuid-3\",\"version\":\"1.2.3\",\"lastBomImportStr\":\"2007-12-03T10:15:30\",\"tags\":[{\"name\":\"tag1\"},{\"name\":\"tag2\"},{\"name\":null}]}]"));
default ->
response.sendNotFound();
};
}))
.bindNow();

Expand All @@ -207,7 +209,7 @@ void getProjectsTestWithErrors() throws IOException {
doThrow(new ConnectException("oops"))
.when(call).execute();

assertThatCode(() -> uut.getProjects())
assertThatCode(uut::getProjects)
.hasMessage(Messages.ApiClient_Error_Connection("", ""))
.hasCauseInstanceOf(ConnectException.class);
verify(httpClient, times(2)).newCall(any(okhttp3.Request.class));
Expand Down Expand Up @@ -268,12 +270,12 @@ void getFindingsTest(JenkinsRule r) throws ApiClientException {
assertCommonHeaders(request);
assertThat(request.param("uuid")).isNotEmpty();
String uuid = request.param("uuid");
switch (uuid) {
case "uuid-1":
return response.sendString(Mono.just("[]"));
default:
return response.sendNotFound();
}
return switch (uuid) {
case "uuid-1" ->
response.sendString(Mono.just("[]"));
default ->
response.sendNotFound();
};
}))
.bindNow();

Expand Down Expand Up @@ -310,12 +312,12 @@ void getViolationsTest(JenkinsRule r) throws ApiClientException {
assertCommonHeaders(request);
assertThat(request.param("uuid")).isNotEmpty();
String uuid = request.param("uuid");
switch (uuid) {
case "uuid-1":
return response.sendString(Mono.just("[]"));
default:
return response.sendNotFound();
}
return switch (uuid) {
case "uuid-1" ->
response.sendString(Mono.just("[]"));
default ->
response.sendNotFound();
};
}))
.bindNow();

Expand Down Expand Up @@ -357,8 +359,8 @@ void uploadTestWithUuid(@TempDir Path tmp, JenkinsRule r) throws IOException, In
assertPOSTHeaders(request);
return response.sendString(
request.receive().asString(StandardCharsets.UTF_8)
.doOnNext(body -> requestBody.set(body))
.doOnComplete(() -> completionSignal.countDown())
.doOnNext(requestBody::set)
.doOnComplete(completionSignal::countDown)
.map(body -> "{\"token\":\"uuid-1\"}")
);
}))
Expand Down Expand Up @@ -392,8 +394,8 @@ void uploadTestWithName(@TempDir Path tmp, JenkinsRule r) throws IOException, In
assertPOSTHeaders(request);
return response.sendString(
request.receive().asString(StandardCharsets.UTF_8)
.doOnNext(body -> requestBody.set(body))
.doOnComplete(() -> completionSignal.countDown())
.doOnNext(requestBody::set)
.doOnComplete(completionSignal::countDown)
.map(body -> "")
);
}))
Expand Down Expand Up @@ -472,12 +474,12 @@ void isTokenBeingProcessedTest(JenkinsRule r) throws ApiClientException {
assertCommonHeaders(request);
assertThat(request.param("uuid")).isNotEmpty();
String uuid = request.param("uuid");
switch (uuid) {
case "uuid-1":
return response.sendString(Mono.just("{\"processing\":true}"));
default:
return response.sendNotFound();
}
return switch (uuid) {
case "uuid-1" ->
response.sendString(Mono.just("{\"processing\":true}"));
default ->
response.sendNotFound();
};
}))
.bindNow();

Expand Down Expand Up @@ -519,8 +521,8 @@ void updateProjectPropertiesTest(JenkinsRule r) throws InterruptedException {
assertPOSTHeaders(request);
return response.sendString(
request.receive().asString(StandardCharsets.UTF_8)
.doOnNext(body -> requestBody.set(body))
.doOnComplete(() -> completionSignal.countDown())
.doOnNext(requestBody::set)
.doOnComplete(completionSignal::countDown)
);
})
)
Expand Down Expand Up @@ -593,6 +595,16 @@ void updateProjectPropertiesTestWithErrorsOnUpdate() throws IOException {
verify(httpClient, times(2)).newCall(any(okhttp3.Request.class));
}

@Test
void updateProjectPropertiesTestWithEmptyProperties() throws IOException {
final var httpClient = mock(OkHttpClient.class);
final var uut = createClient(httpClient);

uut.updateProjectProperties("foo", new ProjectProperties());

verify(httpClient, never()).newCall(any(okhttp3.Request.class));
}

@Test
void getTeamPermissionsTest(JenkinsRule r) throws ApiClientException {
server = HttpServer.create()
Expand Down Expand Up @@ -627,7 +639,7 @@ void getTeamPermissionsTestWithErrors() throws IOException {
doThrow(new ConnectException("oops"))
.when(call).execute();

assertThatCode(() -> uut.getTeamPermissions())
assertThatCode(uut::getTeamPermissions)
.hasMessage(Messages.ApiClient_Error_Connection("", ""))
.hasCauseInstanceOf(ConnectException.class);
verify(httpClient, times(2)).newCall(any(okhttp3.Request.class));
Expand Down Expand Up @@ -664,7 +676,7 @@ void testGetVersionWithErrors() throws IOException {
doThrow(new ConnectException("oops"))
.when(call).execute();

assertThatCode(() -> uut.getVersion())
assertThatCode(uut::getVersion)
.hasMessage(Messages.ApiClient_Error_Connection("", ""))
.hasCauseInstanceOf(ConnectException.class);
verify(httpClient, times(2)).newCall(any(okhttp3.Request.class));
Expand All @@ -680,7 +692,7 @@ void testWithContextPath(JenkinsRule r) {

ApiClient uut = new ApiClient(String.format("http://%s:%d/ctx", server.host(), server.port()), API_KEY, logger, 1, 1);

assertThatCode(() -> uut.testConnection()).isInstanceOf(ApiClientException.class)
assertThatCode(uut::testConnection).isInstanceOf(ApiClientException.class)
.hasNoCause()
// if context path would be ignores, the server would return 404 instead of 500
.hasMessage(Messages.ApiClient_Error_Connection(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), HttpResponseStatus.INTERNAL_SERVER_ERROR.reasonPhrase()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ void getSeverityDistributionTrendPermissionTest(JenkinsRule j) throws IOExceptio
final User anonymous = User.getOrCreateByIdOrFullName(ACL.ANONYMOUS_USERNAME);
// without propper permissions
try (ACLContext ignored = ACL.as(anonymous)) {
assertThatThrownBy(() -> uut.getSeverityDistributionTrend()).isInstanceOf(AccessDeniedException3.class);
assertThatThrownBy(uut::getSeverityDistributionTrend).isInstanceOf(AccessDeniedException3.class);
}
// with propper permissions
try (ACLContext ignored = ACL.as(anonymous)) {
mockAuthorizationStrategy.grant(Job.READ).onItems(project).to(anonymous);
assertThatCode(() -> uut.getSeverityDistributionTrend()).doesNotThrowAnyException();
assertThatCode(uut::getSeverityDistributionTrend).doesNotThrowAnyException();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ void getFindingsJsonPermissionTest(JenkinsRule j) throws IOException {
final User anonymous = User.getOrCreateByIdOrFullName(ACL.ANONYMOUS_USERNAME);
// without propper permissions
try (ACLContext ignored = ACL.as(anonymous)) {
assertThatThrownBy(() -> uut.getFindingsJson()).isInstanceOf(AccessDeniedException3.class);
assertThatThrownBy(uut::getFindingsJson).isInstanceOf(AccessDeniedException3.class);
}
// with propper permissions
try (ACLContext ignored = ACL.as(anonymous)) {
mockAuthorizationStrategy.grant(Job.READ).onItems(project).to(anonymous);
assertThatCode(() -> uut.getFindingsJson()).doesNotThrowAnyException();
assertThatCode(uut::getFindingsJson).doesNotThrowAnyException();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ void getViolationsTrendPermissionTest(JenkinsRule j) throws IOException {
final var anonymous = User.getOrCreateByIdOrFullName(ACL.ANONYMOUS_USERNAME);
// without propper permissions
try (ACLContext ignored = ACL.as(anonymous)) {
assertThatThrownBy(() -> uut.getViolationsTrend()).isInstanceOf(AccessDeniedException3.class);
assertThatThrownBy(uut::getViolationsTrend).isInstanceOf(AccessDeniedException3.class);
}
// with propper permissions
try (ACLContext ignored = ACL.as(anonymous)) {
mockAuthorizationStrategy.grant(Job.READ).onItems(project).to(anonymous);
assertThatCode(() -> uut.getViolationsTrend()).doesNotThrowAnyException();
assertThatCode(uut::getViolationsTrend).doesNotThrowAnyException();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ void getViolationsJsonPermissionTest(JenkinsRule j) throws IOException {
final User anonymous = User.getOrCreateByIdOrFullName(ACL.ANONYMOUS_USERNAME);
// without propper permissions
try (ACLContext ignored = ACL.as(anonymous)) {
assertThatThrownBy(() -> uut.getViolationsJson()).isInstanceOf(AccessDeniedException3.class);
assertThatThrownBy(uut::getViolationsJson).isInstanceOf(AccessDeniedException3.class);
}
// with propper permissions
try (ACLContext ignored = ACL.as(anonymous)) {
mockAuthorizationStrategy.grant(Job.READ).onItems(project).to(anonymous);
assertThatCode(() -> uut.getViolationsJson()).doesNotThrowAnyException();
assertThatCode(uut::getViolationsJson).doesNotThrowAnyException();
}
}

Expand Down

0 comments on commit d36223d

Please sign in to comment.