Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Feat: support yaml payloads #210

Merged
merged 11 commits into from
Apr 24, 2023
Merged
1 change: 1 addition & 0 deletions platform-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
// REST related stuff
implementation 'io.quarkus:quarkus-resteasy-reactive'
implementation 'io.quarkus:quarkus-resteasy-reactive-jackson'
implementation 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-yaml-provider'

// SmallRye for Rest and messaging, micrometer for monitoring
implementation 'io.quarkus:quarkus-smallrye-openapi'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.datacater.core.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.jaxrs.yaml.YAMLMediaTypes;
import io.datacater.core.exceptions.ConfigNotFoundException;
import io.quarkus.security.Authenticated;
import io.smallrye.mutiny.Uni;
Expand All @@ -16,7 +17,7 @@

@Path("/configs")
@Authenticated
@Produces(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
@SecurityRequirement(name = "apiToken")
public class ConfigEndpoint {
private static final String UUID_NOT_FOUND_ERROR_MESSAGE = "No config found for uuid %s";
Expand Down Expand Up @@ -51,7 +52,7 @@ public Uni<ConfigEntity> getConfig(@PathParam("uuid") UUID uuid) {

@POST
@RequestBody
@Consumes(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
public Uni<ConfigEntity> createConfig(Config config) throws JsonProcessingException {
ConfigEntity configEntity =
ConfigEntity.from(config.name(), config.kind(), config.metadata(), config.spec());
Expand All @@ -63,7 +64,7 @@ public Uni<ConfigEntity> createConfig(Config config) throws JsonProcessingExcept
@PUT
@Path("{uuid}")
@RequestBody
@Consumes(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
public Uni<ConfigEntity> updateConfig(@PathParam("uuid") UUID uuid, Config config) {
return sf.withTransaction(
((session, transaction) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.yaml.YAMLMediaTypes;
import io.datacater.core.authentication.DataCaterSessionFactory;
import io.datacater.core.config.ConfigEntity;
import io.datacater.core.config.ConfigUtilities;
Expand Down Expand Up @@ -40,7 +41,7 @@

@Path("/deployments")
@Authenticated
@Produces(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
@SecurityRequirement(name = "apiToken")
@RequestScoped
public class DeploymentEndpoint {
Expand Down Expand Up @@ -166,7 +167,7 @@ public Uni<List<DeploymentEntity>> getDeployments() {
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
public Uni<DeploymentEntity> createDeployment(DeploymentSpec spec) {
DeploymentEntity de = new DeploymentEntity(spec);

Expand Down Expand Up @@ -252,6 +253,7 @@ public Uni<Response> deleteDeployment(@PathParam("uuid") UUID deploymentId) {

@PUT
@Path("{uuid}")
@Consumes({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
public Uni<DeploymentEntity> updateDeployment(
@PathParam("uuid") UUID deploymentUuid, DeploymentSpec spec) {
return dsf.withTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ protected void setSpec(JsonNode spec) {
this.spec = spec;
}

protected UUID getId() {
public UUID getId() {
return this.id;
}

protected JsonNode getSpec() {
public JsonNode getSpec() {
return this.spec;
}

protected JsonNode getConfigSelector() {
public JsonNode getConfigSelector() {
return configSelector;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.datacater.core.info;

import com.fasterxml.jackson.jaxrs.yaml.YAMLMediaTypes;
import io.smallrye.mutiny.Uni;
import java.net.URI;
import javax.ws.rs.GET;
Expand All @@ -10,7 +11,7 @@
import javax.ws.rs.core.UriInfo;

@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
public class InfoEndpoint {

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.jaxrs.yaml.YAMLMediaTypes;
import io.datacater.core.authentication.DataCaterSessionFactory;
import io.datacater.core.exceptions.DatacaterException;
import io.datacater.core.exceptions.PipelineNotFoundException;
Expand Down Expand Up @@ -42,7 +43,7 @@

@Path("/pipelines")
@Authenticated
@Produces(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
@SecurityRequirement(name = "apiToken")
public class PipelineEndpoint {

Expand Down Expand Up @@ -75,7 +76,7 @@ public Uni<PipelineEntity> getPipeline(@PathParam("uuid") UUID uuid) {

@POST
@RequestBody
@Consumes(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
public Uni<PipelineEntity> createPipeline(Pipeline pipeline) throws JsonProcessingException {
PipelineEntity pe =
PipelineEntity.from(
Expand All @@ -88,7 +89,7 @@ public Uni<PipelineEntity> createPipeline(Pipeline pipeline) throws JsonProcessi
@PUT
@Path("{uuid}")
@RequestBody
@Consumes(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
public Uni<PipelineEntity> updatePipeline(@PathParam("uuid") UUID uuid, Pipeline pipeline) {
return dsf.withTransaction(
((session, transaction) ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.datacater.core.stream;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.jaxrs.yaml.YAMLMediaTypes;
import io.datacater.core.authentication.DataCaterSessionFactory;
import io.datacater.core.config.ConfigEntity;
import io.datacater.core.config.ConfigUtilities;
import io.datacater.core.exceptions.*;
import io.quarkus.security.Authenticated;
import io.smallrye.mutiny.Uni;
import java.util.List;
import java.util.UUID;
Expand All @@ -16,13 +16,10 @@
import javax.ws.rs.core.Response;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement;
import org.jboss.logging.Logger;

@Path("/streams")
@Authenticated
@Produces(MediaType.APPLICATION_JSON)
@SecurityRequirement(name = "apiToken")
@Produces({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
public class StreamEndpoint {
private static final Logger LOGGER = Logger.getLogger(StreamEndpoint.class);
private static final Integer KAFKA_API_TIMEOUT_MS =
Expand Down Expand Up @@ -58,7 +55,7 @@ public Uni<List<StreamEntity>> getStreams() {

@POST
@RequestBody
@Consumes(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
public Uni<Response> createStream(Stream stream) throws JsonProcessingException {
StreamEntity se = new StreamEntity(stream.name(), stream.spec(), stream.configSelector());
return dsf.withTransaction(
Expand All @@ -84,7 +81,7 @@ public Uni<Response> createStream(Stream stream) throws JsonProcessingException
@PUT
@Path("{uuid}")
@RequestBody
@Consumes(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_JSON, YAMLMediaTypes.APPLICATION_JACKSON_YAML})
public Uni<StreamEntity> updateStream(@PathParam("uuid") UUID uuid, Stream stream) {

return dsf.withTransaction(
Expand Down
4 changes: 4 additions & 0 deletions platform-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
---
quarkus :
index-dependency:
yaml:
group-id: com.fasterxml.jackson.jaxrs
artifact-id: jackson-jaxrs-yaml-provider
application:
name: datacater
version: '2023.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import io.datacater.core.yamlTests.Utilities;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
Expand All @@ -30,6 +32,17 @@ void testGetInfo() throws JsonProcessingException {
Assertions.assertEquals(expectedResponseCode, response.getStatusCode());
}

@Test
void testGetInfoAsYaml() throws JsonProcessingException {
final int expectedResponseCode = 200;
ObjectMapper mapper = new YAMLMapper();
response = given().header(Utilities.ACCEPT_YAML).get();
Info infoFromYaml = mapper.readValue(response.body().asString(), Info.class);

Assertions.assertEquals(expectedResponseCode, response.getStatusCode());
Assertions.assertNotNull(infoFromYaml);
}

@Test
void testStreamResourceInfo() {
Assertions.assertEquals(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.datacater.core.yamlTests;

import static io.restassured.config.EncoderConfig.encoderConfig;

import com.fasterxml.jackson.jaxrs.yaml.YAMLMediaTypes;
import io.restassured.RestAssured;
import io.restassured.config.RestAssuredConfig;
import io.restassured.http.ContentType;
import io.restassured.http.Header;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.ws.rs.core.MediaType;

public class Utilities {

public static final Header ACCEPT_YAML =
new Header("Accept", YAMLMediaTypes.APPLICATION_JACKSON_YAML);
public static final Header CONTENT_YAML =
new Header("Content-Type", YAMLMediaTypes.APPLICATION_JACKSON_YAML);

public static final Header ACCEPT_JSON = new Header("Accept", MediaType.APPLICATION_JSON);
public static final Header CONTENT_JSON = new Header("Content-Type", MediaType.APPLICATION_JSON);

public static final RestAssuredConfig restAssuredConfig =
RestAssured.config()
.encoderConfig(
encoderConfig()
.encodeContentTypeAs(YAMLMediaTypes.APPLICATION_JACKSON_YAML, ContentType.TEXT));

static String getStringFromFile(String testResourcePath) throws IOException, URISyntaxException {
URL streamJsonURL = ClassLoader.getSystemClassLoader().getResource(testResourcePath);

return Files.readString(Paths.get(streamJsonURL.toURI()));
}
}
Loading