Skip to content

Commit

Permalink
Use ADR object when getting latest revision (finos#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
grahampacker-ms committed Jan 14, 2025
1 parent ddb57af commit ff9855c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bson.Document;
import org.eclipse.microprofile.config.ConfigProvider;
import org.finos.calm.domain.Adr;
import org.finos.calm.domain.AdrBuilder;
import org.finos.calm.domain.AdrContent;
import org.finos.calm.domain.AdrContentBuilder;
import org.finos.calm.domain.AdrDecision;
Expand Down Expand Up @@ -133,22 +134,27 @@ void end_to_end_verify_get_adr_revision() throws JsonProcessingException {
.extract()
.body()
.as(AdrContent.class);

assertEquals(adrContent, result);
}

@Test
@Order(4)
void end_to_end_verify_get_adr() {
AdrContent result = given()
Adr result = given()
.when().get("/calm/namespaces/finos/adrs/1")
.then()
.statusCode(200)
.extract()
.body()
.as(AdrContent.class);

assertEquals(adrContent, result);
.as(Adr.class);

Adr expectedAdr = AdrBuilder.builder()
.namespace("finos")
.id(1)
.revision(1)
.adrContent(adrContent)
.build();
assertEquals(expectedAdr, result);
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions calm-hub/src/main/java/org/finos/calm/domain/AdrContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ public record AdrContent(
AdrStatus status,
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss:SSS")
LocalDateTime creationDateTime,
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss:SSS")
LocalDateTime updateDateTime,
String contextAndProblemStatement,
List<String> decisionDrivers,
Expand Down
23 changes: 22 additions & 1 deletion calm-hub/src/main/java/org/finos/calm/resources/AdrResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ public Response getAdrsForNamespace(@PathParam("namespace") String namespace) {
}
}


/**
* Create a new ADR in the DRAFT state
* @param namespace the namespace to create the ADR in
* @param newAdr the new ADR to be created
* @return created response with Location header
* @throws URISyntaxException cannot produce location URL
*/
@POST
@Path("{namespace}/adrs")
@Consumes(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -95,6 +101,14 @@ public Response createAdrForNamespace(@PathParam("namespace") String namespace,
}
}

/**
* Update an existing ADRs contents
* @param namespace the namespace the ADR is in
* @param adrId the ID of the ADR
* @param newAdrRevision the new ADR content
* @return created with a Location header
* @throws URISyntaxException cannot produce Location header
*/
@POST
@Path("{namespace}/adrs/{adrId}")
@Consumes(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -131,6 +145,7 @@ public Response updateAdrForNamespace(@PathParam("namespace") String namespace,
}
}


@GET
@Path("{namespace}/adrs/{adrId}")
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -155,6 +170,8 @@ public Response getAdr(@PathParam("namespace") String namespace, @PathParam("adr
} catch (AdrRevisionNotFoundException e) {
logger.error("Could not get latest revision of ADR [{}]", adrId, e);
return invalidLatestRevisionResponse(adrId);
} catch(JsonProcessingException e) {
return invalidGetAdrResponse(adrId);
}
}

Expand Down Expand Up @@ -222,6 +239,10 @@ private Response invalidAdrJsonResponse(String adrJson) {
return Response.status(Response.Status.BAD_REQUEST).entity("The ADR JSON could not be parsed: " + adrJson).build();
}

private Response invalidGetAdrResponse(int adrId) {
return Response.status(Response.Status.NOT_FOUND).entity("Could not process ADR when getting it: " + adrId).build();
}

private Response invalidAdrResponse(int adrId) {
return Response.status(Response.Status.NOT_FOUND).entity("Invalid adrId provided: " + adrId).build();
}
Expand Down
2 changes: 1 addition & 1 deletion calm-hub/src/main/java/org/finos/calm/store/AdrStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface AdrStore {

List<Integer> getAdrsForNamespace(String namespace) throws NamespaceNotFoundException;
Adr createAdrForNamespace(Adr adr) throws NamespaceNotFoundException, JsonProcessingException;
String getAdr(Adr adr) throws NamespaceNotFoundException, AdrNotFoundException, AdrRevisionNotFoundException;
Adr getAdr(Adr adr) throws NamespaceNotFoundException, AdrNotFoundException, AdrRevisionNotFoundException, JsonProcessingException;
List<Integer> getAdrRevisions(Adr adr) throws NamespaceNotFoundException, AdrNotFoundException;
String getAdrRevision(Adr adr) throws NamespaceNotFoundException, AdrNotFoundException, AdrRevisionNotFoundException;
Adr updateAdrForNamespace(Adr adr) throws NamespaceNotFoundException, AdrNotFoundException, AdrRevisionNotFoundException, JsonProcessingException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Adr createAdrForNamespace(Adr adr) throws NamespaceNotFoundException, Jso
}

@Override
public String getAdr(Adr adr) throws NamespaceNotFoundException, AdrNotFoundException, AdrRevisionNotFoundException {
public Adr getAdr(Adr adr) throws NamespaceNotFoundException, AdrNotFoundException, AdrRevisionNotFoundException, JsonProcessingException {
Document result = retrieveAdrRevisions(adr);
List<Document> adrs = (List<Document>) result.get("adrs");
for (Document adrDoc : adrs) {
Expand All @@ -101,7 +101,12 @@ public String getAdr(Adr adr) throws NamespaceNotFoundException, AdrNotFoundExce
// Return the ADR JSON blob for the specified revision
Document revisionDoc = (Document) revisions.get(String.valueOf(latestRevision));
log.info("RevisionDoc: [{}], Revision: [{}]", adrDoc.get("revisions"), latestRevision);
return revisionDoc.toJson();
return AdrBuilder.builder()
.namespace(adr.namespace())
.id(adr.id())
.revision(latestRevision)
.adrContent(objectMapper.readValue(revisionDoc.toJson(), AdrContent.class))
.build();
}
}
throw new AdrNotFoundException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static Stream<Arguments> provideParametersForCreateAdrTests() {

@ParameterizedTest
@MethodSource("provideParametersForCreateAdrTests")
void respond_correctly_when_creating_adr(String namespace, Throwable exceptionToThrow, int expectedStatusCode, Matcher locationHeader) throws NamespaceNotFoundException, JsonProcessingException {
void respond_correctly_when_creating_adr(String namespace, Throwable exceptionToThrow, int expectedStatusCode, Matcher<?> locationHeader) throws NamespaceNotFoundException, JsonProcessingException {
if (exceptionToThrow != null) {
when(mockAdrStore.createAdrForNamespace(any(Adr.class))).thenThrow(exceptionToThrow);
} else {
Expand Down Expand Up @@ -265,21 +265,30 @@ void respond_correctly_to_get_adr_revision(String namespace, Throwable exception

@ParameterizedTest
@MethodSource("provideParametersForGetAdrRevisionTests")
void respond_correctly_to_get_adr(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws NamespaceNotFoundException, AdrNotFoundException, AdrRevisionNotFoundException {
void respond_correctly_to_get_adr(String namespace, Throwable exceptionToThrow, int expectedStatusCode) throws NamespaceNotFoundException, AdrNotFoundException, AdrRevisionNotFoundException, JsonProcessingException {
Adr adr = AdrBuilder.builder()
.namespace(namespace)
.id(12)
.revision(2)
.adrContent(AdrContentBuilder.builder().title("My title").build())
.build();

if (exceptionToThrow != null) {
when(mockAdrStore.getAdr(any(Adr.class))).thenThrow(exceptionToThrow);
} else {
String adr = "{ \"test\": \"json\" }";
when(mockAdrStore.getAdr(any(Adr.class))).thenReturn(adr);
}

if (expectedStatusCode == 200) {
given()
Adr actualAdr = given()
.when()
.get("/calm/namespaces/" + namespace + "/adrs/12")
.then()
.statusCode(expectedStatusCode)
.body(equalTo("{ \"test\": \"json\" }"));
.extract()
.body()
.as(Adr.class);
assertEquals(adr, actualAdr);
} else {
given()
.when()
Expand All @@ -301,7 +310,7 @@ private void verifyExpectedGetAdrRevision(String namespace) throws NamespaceNotF
verify(mockAdrStore, times(1)).getAdrRevision(expectedAdrToRetrieve);
}

private void verifyExpectedGetAdr(String namespace) throws NamespaceNotFoundException, AdrNotFoundException, AdrRevisionNotFoundException {
private void verifyExpectedGetAdr(String namespace) throws NamespaceNotFoundException, AdrNotFoundException, AdrRevisionNotFoundException, JsonProcessingException {
Adr expectedAdrToRetrieve = AdrBuilder.builder()
.namespace(namespace)
.id(12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -359,10 +360,11 @@ void return_the_latest_adr_revision() throws NamespaceNotFoundException, AdrNotF
mockSetupAdrDocumentWithRevisions();

Adr adr = AdrBuilder.builder().namespace(NAMESPACE)
.id(42).revision(1).build();
.id(42).build();

String adrRevision = mongoAdrStore.getAdr(adr);
assertThat(adrRevision.replaceAll("\\s+", ""), is(objectMapper.writeValueAsString(simpleAdr.adrContent()).replaceAll("\\s+", "")));
Adr latestAdr = mongoAdrStore.getAdr(adr);
Adr expectedAdr = AdrBuilder.builder(simpleAdr).revision(1).build();
assertEquals(expectedAdr, latestAdr);
}

@Test
Expand Down

0 comments on commit ff9855c

Please sign in to comment.