Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies {
api("com.fasterxml.jackson.core:jackson-databind:2.20.0")
api("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.20.0")

api("com.github.erosb:json-sKema:0.27.0")
api("com.github.erosb:json-sKema:0.0.2-SNAPSHOT")

testImplementation("junit:junit:4.13.2")
testImplementation("org.skyscreamer:jsonassert:1.5.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.erosb.kappa.operation.validator.adapters.server.restassured;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.github.erosb.jsonsKema.JsonString;
import com.github.erosb.kappa.operation.validator.model.Request;
import io.restassured.authentication.NoAuthScheme;
import io.restassured.config.RestAssuredConfig;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void test()
Assert.assertNotNull(underTest.getHeaders());
Assert.assertTrue(underTest.getHeaders().containsKey(H_NAME));
Assert.assertEquals(H_VALUE, underTest.getHeaders().get(H_NAME).iterator().next());
Assert.assertEquals(JsonNodeFactory.instance.textNode("a body"), underTest.getBody().getContentAsNode(null, null, null));
Assert.assertEquals(new JsonString("a body"), underTest.getBody().getContentAsNode(null, null, null));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.erosb.kappa.operation.validator.adapters.server.restassured;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.github.erosb.jsonsKema.JsonString;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.response.Response;
Expand Down Expand Up @@ -36,7 +37,7 @@ public void test()
RestAssuredResponse underTest = new RestAssuredResponse(mockResponse);

Assert.assertEquals(200, underTest.getStatus());
Assert.assertEquals(JsonNodeFactory.instance.textNode("hello"), underTest.getBody().getContentAsNode(null, null, null));
Assert.assertEquals(new JsonString("hello"), underTest.getBody().getContentAsNode(null, null, null));

Map<String, Collection<String>> expectedHeaders = new HashMap<>();
expectedHeaders.put("Other-Header", Arrays.asList("value", "other value"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.erosb.kappa.operation.validator.adapters.server.servlet;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.github.erosb.jsonsKema.JsonString;
import com.github.erosb.kappa.operation.validator.model.Request;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -24,16 +25,6 @@ public class JakartaServletTest {
private HttpServletRequest servletRequest;
private Cookie cookie;

@Before
public void setUp()
throws IOException {
servletRequest = Mockito.mock(HttpServletRequest.class);
cookie = null;

Mockito.when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(URL));
Mockito.when(servletRequest.getQueryString()).thenReturn("id=2&name=foo");
}

private void mockCookies(boolean enable) {
if (enable) {
cookie = new Cookie("bis", "cuit");
Expand All @@ -44,6 +35,15 @@ private void mockCookies(boolean enable) {
}
}

@Before
public void setUp() {
servletRequest = Mockito.mock(HttpServletRequest.class);
cookie = null;

Mockito.when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(URL));
Mockito.when(servletRequest.getQueryString()).thenReturn("id=2&name=foo");
}

private void mockHeaders(boolean enable) {
if (enable) {
Vector<String> headerNames = new Vector<>();
Expand Down Expand Up @@ -103,7 +103,7 @@ public void postTest()
checkCommons(rq, true, true);

Assert.assertEquals(
JsonNodeFactory.instance.textNode("{}"),
new JsonString("{}"),
rq.getBody().getContentAsNode(null, null, null));

Assert.assertEquals("?queryString", rq.getQuery());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public class KappaContractTestingTest {
MockMvc mockMvc;

@Test
public void contractFailureInRequest()
throws Exception {
public void contractFailureInRequest() {
Throwable exc = assertThrows(AssertionError.class,
() -> mockMvc.perform(post("/users").contentType(MediaType.APPLICATION_JSON).content("""
{
Expand Down
3 changes: 2 additions & 1 deletion kappa-undertow-adapter/src/test/java/UndertowTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

import com.github.erosb.jsonsKema.JsonString;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -96,7 +97,7 @@ public void postTest() throws IOException {
Assert.assertNull(rq.getQuery());

Assert.assertEquals(
JsonNodeFactory.instance.textNode("a body"),
new JsonString("a body"),
rq.getBody().getContentAsNode(null, null, null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import com.github.erosb.jsonsKema.JsonString;
import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -124,8 +125,8 @@ public void mountRegexHandlerTest(TestContext context)
routerFactory.addOperationHandler("regex", rc -> {
RequestParameters rqParameters = rc.get("rqParameters");
context.assertNotNull(rqParameters);
context.assertEquals(JsonNodeFactory.instance.textNode("foo"), rqParameters.getPathParameter("dataset"));
context.assertEquals(JsonNodeFactory.instance.textNode("bar"), rqParameters.getPathParameter("version"));
context.assertEquals(new JsonString("foo"), rqParameters.getPathParameter("dataset"));
context.assertEquals(new JsonString("bar"), rqParameters.getPathParameter("version"));

rc
.response()
Expand Down
3 changes: 2 additions & 1 deletion kappa-vertx-adapter/src/test/java/VertxTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

import com.github.erosb.jsonsKema.JsonString;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void postTest() throws IOException {
assertNull(rq.getQuery());

assertEquals(
JsonNodeFactory.instance.textNode("a body"),
new JsonString("a body"),
rq.getBody().getContentAsNode(null, null, null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
import com.github.erosb.kappa.operation.validator.util.convert.ContentConverter;
import com.github.erosb.kappa.parser.model.v3.MediaType;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

Expand All @@ -27,7 +25,7 @@
public class Body {
private static final String BODY_REQUIRED_ERR_MSG = "Body content is required.";

private final JsonNode bodyNode;
private final IJsonValue bodyNode;
private final String bodyStr;
private final InputStream bodyIs;

Expand Down Expand Up @@ -122,7 +120,7 @@ public IJsonValue contentAsNode(final String rawContentType, final URI documentS
}

@Deprecated
public JsonNode getContentAsNode(final OAIContext context,
public IJsonValue getContentAsNode(final OAIContext context,
final MediaType mediaType,
final String rawContentType) throws IOException {
if (bodyNode != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,71 @@
package com.github.erosb.kappa.operation.validator.model.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.erosb.jsonsKema.IJsonValue;

import java.util.Map;

public class RequestParameters {
private final Map<String, JsonNode> pathParameters;
private final Map<String, JsonNode> queryParameters;
private final Map<String, JsonNode> headerParameters;
private final Map<String, JsonNode> cookieParameters;
private final Map<String, IJsonValue> pathParameters;
private final Map<String, IJsonValue> queryParameters;
private final Map<String, IJsonValue> headerParameters;
private final Map<String, IJsonValue> cookieParameters;

public RequestParameters(Map<String, JsonNode> pathParameters,
Map<String, JsonNode> queryParameters,
Map<String, JsonNode> headerParameters,
Map<String, JsonNode> cookieParameters) {
public RequestParameters(Map<String, IJsonValue> pathParameters,
Map<String, IJsonValue> queryParameters,
Map<String, IJsonValue> headerParameters,
Map<String, IJsonValue> cookieParameters) {

this.pathParameters = pathParameters;
this.queryParameters = queryParameters;
this.headerParameters = headerParameters;
this.cookieParameters = cookieParameters;
}

public Map<String, JsonNode> getPathParameters() {
public Map<String, IJsonValue> getPathParameters() {
return pathParameters;
}

public JsonNode getPathParameter(String name) {
if (pathParameters == null) return null;
public IJsonValue getPathParameter(String name) {
if (pathParameters == null) {
return null;
}

return pathParameters.get(name);
}

public Map<String, JsonNode> getQueryParameters() {
public Map<String, IJsonValue> getQueryParameters() {
return queryParameters;
}

public JsonNode getQueryParameter(String name) {
if (queryParameters == null) return null;
public IJsonValue getQueryParameter(String name) {
if (queryParameters == null) {
return null;
}

return queryParameters.get(name);
}

public Map<String, JsonNode> getHeaderParameters() {
public Map<String, IJsonValue> getHeaderParameters() {
return headerParameters;
}

public JsonNode getHeaderParameter(String name) {
if (headerParameters == null) return null;
public IJsonValue getHeaderParameter(String name) {
if (headerParameters == null) {
return null;
}

return headerParameters.get(name);
}

public Map<String, JsonNode> getCookieParameters() {
public Map<String, IJsonValue> getCookieParameters() {
return cookieParameters;
}

public JsonNode getCookieParameter(String name) {
if (cookieParameters == null) return null;
public IJsonValue getCookieParameter(String name) {
if (cookieParameters == null) {
return null;
}

return cookieParameters.get(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.github.erosb.jsonsKema.IJsonValue;
import com.github.erosb.jsonsKema.JsonParseException;
import com.github.erosb.jsonsKema.JsonParser;
import com.github.erosb.jsonsKema.JsonString;
import com.github.erosb.kappa.core.model.OAIContext;
import com.github.erosb.kappa.core.util.IOUtil;
import com.github.erosb.kappa.core.util.TreeUtil;
Expand All @@ -20,11 +24,11 @@ public final class ContentConverter {
private ContentConverter() {
}

public static JsonNode convert(final OAIContext context,
final MediaType mediaType,
final String rawContentType,
final InputStream is,
final String str) throws IOException {
public static IJsonValue convert(final OAIContext context,
final MediaType mediaType,
final String rawContentType,
final InputStream is,
final String str) throws IOException {

String contentType = ContentType.getTypeOnly(rawContentType);

Expand All @@ -47,47 +51,55 @@ public static JsonNode convert(final OAIContext context,
}
}

private static JsonNode formUrlEncodedToNode(final OAIContext context, final MediaType mediaType, final String rawContentType, final InputStream content) throws IOException {
private static IJsonValue formUrlEncodedToNode(final OAIContext context, final MediaType mediaType, final String rawContentType,
final InputStream content) throws IOException {
String encoding = ContentType.getCharSet(rawContentType);
return FormUrlConverter.instance().convert(context, mediaType, content, encoding);
}

private static JsonNode formUrlEncodedToNode(final OAIContext context, final MediaType mediaType, final String rawContentType, final String content) {
private static IJsonValue formUrlEncodedToNode(final OAIContext context, final MediaType mediaType, final String rawContentType,
final String content) {
String encoding = ContentType.getCharSet(rawContentType);
return FormUrlConverter.instance().convert(context, mediaType, content, encoding);
}

private static JsonNode multipartToNode(final OAIContext context, final MediaType mediaType, final String rawContentType, InputStream content) throws IOException {
private static IJsonValue multipartToNode(final OAIContext context, final MediaType mediaType, final String rawContentType,
InputStream content) throws IOException {
String encoding = ContentType.getCharSet(rawContentType);
return MultipartConverter.instance().convert(context, mediaType, content, rawContentType, encoding);
}

private static JsonNode multipartToNode(final OAIContext context, final MediaType mediaType, final String rawContentType, final String content) throws IOException {
private static IJsonValue multipartToNode(final OAIContext context, final MediaType mediaType, final String rawContentType,
final String content) throws IOException {
String encoding = ContentType.getCharSet(rawContentType);
return MultipartConverter.instance().convert(context, mediaType, content, rawContentType, encoding);
}

private static JsonNode jsonToNode(InputStream content) throws IOException {
return TreeUtil.json.readTree(content);
private static IJsonValue jsonToNode(InputStream content) throws IOException {
return new JsonParser(content).parse();
}

private static JsonNode jsonToNode(String content) throws IOException {
return TreeUtil.json.readTree(content);
private static IJsonValue jsonToNode(String content) throws IOException {
try {
return new JsonParser(content).parse();
} catch (JsonParseException e) {
return new JsonString(content);
}
}

private static JsonNode xmlToNode(final OAIContext context, final Schema schema, InputStream content) throws IOException {
private static IJsonValue xmlToNode(final OAIContext context, final Schema schema, InputStream content) throws IOException {
return XmlConverter.instance().convert(context, schema, IOUtil.toString(content, StandardCharsets.UTF_8.name()));
}

private static JsonNode xmlToNode(final OAIContext context, final Schema schema, String content) {
private static IJsonValue xmlToNode(final OAIContext context, final Schema schema, String content) {
return XmlConverter.instance().convert(context, schema, content);
}

private static JsonNode textToNode(InputStream content) throws IOException {
return JsonNodeFactory.instance.textNode(IOUtil.toString(content, StandardCharsets.UTF_8.name()));
private static IJsonValue textToNode(InputStream content) throws IOException {
return new JsonString(IOUtil.toString(content, StandardCharsets.UTF_8.name()));
}

private static JsonNode textToNode(String content) {
return JsonNodeFactory.instance.textNode(content);
private static IJsonValue textToNode(String content) {
return new JsonString(content);
}
}
Loading
Loading