-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into port-Fix-project-link-for-email-publisher
- Loading branch information
Showing
6 changed files
with
214 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/org/dependencytrack/resources/v1/exception/NotSortableExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) OWASP Foundation. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.resources.v1.exception; | ||
|
||
import alpine.persistence.NotSortableException; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.ExceptionMapper; | ||
import jakarta.ws.rs.ext.Provider; | ||
import org.dependencytrack.resources.v1.problems.ProblemDetails; | ||
|
||
/** | ||
* @since 4.12.0 | ||
*/ | ||
@Provider | ||
public class NotSortableExceptionMapper implements ExceptionMapper<NotSortableException> { | ||
|
||
@Override | ||
public Response toResponse(final NotSortableException exception) { | ||
final var problemDetails = new ProblemDetails(); | ||
problemDetails.setStatus(400); | ||
problemDetails.setTitle("Field not sortable"); | ||
problemDetails.setDetail(exception.getMessage()); | ||
|
||
return Response | ||
.status(Response.Status.BAD_REQUEST) | ||
.type(ProblemDetails.MEDIA_TYPE_JSON) | ||
.entity(problemDetails) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/test/java/org/dependencytrack/resources/v1/exception/NotSortableExceptionMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) OWASP Foundation. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.resources.v1.exception; | ||
|
||
import alpine.persistence.PaginatedResult; | ||
import alpine.server.auth.AuthenticationNotRequired; | ||
import alpine.server.filters.ApiFilter; | ||
import alpine.server.resources.AlpineResource; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import org.dependencytrack.JerseyTestRule; | ||
import org.dependencytrack.ResourceTest; | ||
import org.dependencytrack.persistence.QueryManager; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
|
||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class NotSortableExceptionMapperTest extends ResourceTest { | ||
|
||
@ClassRule | ||
public static JerseyTestRule jersey = new JerseyTestRule( | ||
new ResourceConfig(TestResource.class) | ||
.register(ApiFilter.class) | ||
.register(NotSortableExceptionMapper.class)); | ||
|
||
@Test | ||
public void testFieldDoesNotExist() { | ||
final Response response = jersey.target("/") | ||
.queryParam("sortName", "foo") | ||
.queryParam("sortOrder", "asc") | ||
.request() | ||
.get(); | ||
assertThat(response.getStatus()).isEqualTo(400); | ||
assertThat(response.getHeaderString("Content-Type")).isEqualTo("application/problem+json"); | ||
assertThatJson(getPlainTextBody(response)) | ||
.isEqualTo(""" | ||
{ | ||
"status": 400, | ||
"title": "Field not sortable", | ||
"detail": "Can not sort by Project#foo: The field does not exist" | ||
} | ||
"""); | ||
} | ||
|
||
@Test | ||
public void testTransientField() { | ||
final Response response = jersey.target("/") | ||
.queryParam("sortName", "bomRef") | ||
.queryParam("sortOrder", "asc") | ||
.request() | ||
.get(); | ||
assertThat(response.getStatus()).isEqualTo(400); | ||
assertThat(response.getHeaderString("Content-Type")).isEqualTo("application/problem+json"); | ||
assertThatJson(getPlainTextBody(response)) | ||
.isEqualTo(""" | ||
{ | ||
"status": 400, | ||
"title": "Field not sortable", | ||
"detail": "Can not sort by Project#bomRef: The field is computed and can not be queried or sorted by" | ||
} | ||
"""); | ||
} | ||
|
||
@Test | ||
public void testPersistentField() { | ||
final Response response = jersey.target("/") | ||
.queryParam("sortName", "name") | ||
.queryParam("sortOrder", "asc") | ||
.request() | ||
.get(); | ||
assertThat(response.getStatus()).isEqualTo(200); | ||
assertThat(response.getHeaderString("Content-Type")).isEqualTo("application/json"); | ||
assertThatJson(getPlainTextBody(response)) | ||
.isEqualTo("[]"); | ||
} | ||
|
||
@Path("/") | ||
public static class TestResource extends AlpineResource { | ||
|
||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@AuthenticationNotRequired | ||
public Response get() { | ||
try (final var qm = new QueryManager(getAlpineRequest())) { | ||
final PaginatedResult projects = qm.getProjects(); | ||
return Response | ||
.status(Response.Status.OK) | ||
.header("X-Total-Count", projects.getTotal()) | ||
.entity(projects.getObjects()) | ||
.build(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |