Skip to content

Commit

Permalink
Add regression test for missing parent property in /v1/project/{uuid}…
Browse files Browse the repository at this point in the history
… response

Co-Authored-By: Niklas <[email protected]>
  • Loading branch information
sahibamittal and nscuro committed Sep 24, 2024
1 parent 839d8f1 commit 5945a6d
Showing 1 changed file with 154 additions and 17 deletions.
171 changes: 154 additions & 17 deletions src/test/java/org/dependencytrack/resources/v1/ProjectResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
import alpine.model.IConfigProperty.PropertyType;
import alpine.server.filters.ApiFilter;
import alpine.server.filters.AuthenticationFilter;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.datanucleus.store.types.wrappers.Date;
import org.dependencytrack.JerseyTestRule;
import org.dependencytrack.ResourceTest;
Expand Down Expand Up @@ -61,13 +68,6 @@
import org.junit.ClassRule;
import org.junit.Test;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -1264,19 +1264,63 @@ public void getProjectChildrenConciseWithLatestMetricsTest() {

@Test
public void getProjectByUuidTest() {
Project project = qm.createProject("ABC", null, "1.0", null, null, null, true, false);
final var parentProject = new Project();
parentProject.setName("acme-app-parent");
parentProject.setVersion("1.0.0");
qm.persist(parentProject);

final var project = new Project();
project.setName("acme-app");
project.setVersion("1.0.0");
project.setParent(parentProject);
qm.persist(project);

final var childProject = new Project();
childProject.setName("acme-app-child");
childProject.setVersion("1.0.0");
childProject.setParent(project);
qm.persist(childProject);

Response response = jersey.target(V1_PROJECT + "/" + project.getUuid())
.request()
.header(X_API_KEY, apiKey)
.get(Response.class);
Assert.assertEquals(200, response.getStatus(), 0);
Assert.assertNull(response.getHeaderString(TOTAL_COUNT_HEADER));
JsonObject json = parseJsonObject(response);
Assert.assertNotNull(json);
Assert.assertEquals("ABC", json.getString("name"));
Assert.assertEquals(1, json.getJsonArray("versions").size());
Assert.assertEquals(project.getUuid().toString(), json.getJsonArray("versions").getJsonObject(0).getJsonString("uuid").getString());
Assert.assertEquals("1.0", json.getJsonArray("versions").getJsonObject(0).getJsonString("version").getString());
.get();
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getHeaderString(TOTAL_COUNT_HEADER)).isNull();
assertThatJson(getPlainTextBody(response))
.withMatcher("projectUuid", equalTo(project.getUuid().toString()))
.withMatcher("parentUuid", equalTo(parentProject.getUuid().toString()))
.withMatcher("childUuid", equalTo(childProject.getUuid().toString()))
.isEqualTo("""
{
"name": "acme-app",
"version": "1.0.0",
"uuid": "${json-unit.matches:projectUuid}",
"parent": {
"name": "acme-app-parent",
"version": "1.0.0",
"uuid": "${json-unit.matches:parentUuid}"
},
"children": [
{
"name": "acme-app-child",
"version": "1.0.0",
"uuid": "${json-unit.matches:childUuid}",
"active": true
}
],
"properties": [],
"tags": [],
"active": true,
"versions": [
{
"uuid": "${json-unit.matches:projectUuid}",
"version": "1.0.0",
"active":true
}
]
}
""");
}

@Test
Expand Down Expand Up @@ -2257,4 +2301,97 @@ public void issue4048RegressionTest() {
.isNotEmpty();
}
}

@Test // https://github.com/DependencyTrack/dependency-track/issues/3883
public void issue3883RegressionTest() {
Response response = jersey.target(V1_PROJECT)
.request()
.header(X_API_KEY, apiKey)
.put(Entity.json("""
{
"name": "acme-app-parent",
"version": "1.0.0"
}
"""));
assertThat(response.getStatus()).isEqualTo(201);
final String parentProjectUuid = parseJsonObject(response).getString("uuid");

response = jersey.target(V1_PROJECT)
.request()
.header(X_API_KEY, apiKey)
.put(Entity.json("""
{
"name": "acme-app",
"version": "1.0.0",
"parent": {
"uuid": "%s"
}
}
""".formatted(parentProjectUuid)));
assertThat(response.getStatus()).isEqualTo(201);
final String childProjectUuid = parseJsonObject(response).getString("uuid");

response = jersey.target(V1_PROJECT + "/" + parentProjectUuid)
.request()
.header(X_API_KEY, apiKey)
.get();
assertThat(response.getStatus()).isEqualTo(200);
assertThatJson(getPlainTextBody(response)).isEqualTo("""
{
"name": "acme-app-parent",
"version": "1.0.0",
"classifier": "APPLICATION",
"uuid": "${json-unit.any-string}",
"children": [
{
"name": "acme-app",
"version": "1.0.0",
"classifier": "APPLICATION",
"uuid": "${json-unit.any-string}",
"active": true
}
],
"properties": [],
"tags": [],
"active": true,
"versions": [
{
"uuid": "${json-unit.any-string}",
"version": "1.0.0",
"active": true
}
]
}
""");

response = jersey.target(V1_PROJECT + "/" + childProjectUuid)
.request()
.header(X_API_KEY, apiKey)
.get();
assertThat(response.getStatus()).isEqualTo(200);
assertThatJson(getPlainTextBody(response)).isEqualTo("""
{
"name": "acme-app",
"version": "1.0.0",
"classifier": "APPLICATION",
"uuid": "${json-unit.any-string}",
"parent": {
"name": "acme-app-parent",
"version": "1.0.0",
"uuid": "${json-unit.any-string}"
},
"children": [],
"properties": [],
"tags": [],
"active": true,
"versions": [
{
"uuid": "${json-unit.any-string}",
"version": "1.0.0",
"active": true
}
]
}
""");
}
}

0 comments on commit 5945a6d

Please sign in to comment.