Skip to content

Commit

Permalink
Adjust several tests for new API
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar committed Mar 14, 2024
1 parent 759e1a0 commit 4bc883e
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 467 deletions.
25 changes: 14 additions & 11 deletions core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java
Expand Up @@ -350,24 +350,27 @@ private void debugModel(String source, OpenAPI model) {
return;
}
ApiLogging.logger.addingModel(source);
debugMap("callbacks", source, Optional.ofNullable(model.getComponents()).map(Components::getCallbacks));
debugMap("examples", source, Optional.ofNullable(model.getComponents()).map(Components::getExamples));
debugMap("headers", source, Optional.ofNullable(model.getComponents()).map(Components::getHeaders));
debugMap("links", source, Optional.ofNullable(model.getComponents()).map(Components::getLinks));
debugMap("parameters", source, Optional.ofNullable(model.getComponents()).map(Components::getParameters));
debugMap("request bodies", source, Optional.ofNullable(model.getComponents()).map(Components::getRequestBodies));
debugMap("responses", source, Optional.ofNullable(model.getComponents()).map(Components::getResponses));
debugMap("schemas", source, Optional.ofNullable(model.getComponents()).map(Components::getSchemas));
debugMap("security schemes", source,
Optional.ofNullable(model.getComponents()).map(Components::getSecuritySchemes));
debugMap("callbacks", source, getMap(model, Components::getCallbacks));
debugMap("examples", source, getMap(model, Components::getExamples));
debugMap("headers", source, getMap(model, Components::getHeaders));
debugMap("links", source, getMap(model, Components::getLinks));
debugMap("parameters", source, getMap(model, Components::getParameters));
debugMap("request bodies", source, getMap(model, Components::getRequestBodies));
debugMap("responses", source, getMap(model, Components::getResponses));
debugMap("schemas", source, getMap(model, Components::getSchemas));
debugMap("security schemes", source, getMap(model, Components::getSecuritySchemes));
debugList("servers", source, Optional.ofNullable(model.getServers()));
debugMap("path items", source, Optional.ofNullable(model.getPaths()).map(Paths::getPathItems));
debugList("security", source, Optional.ofNullable(model.getSecurity()));
debugList("tags", source, Optional.ofNullable(model.getTags()));
debugMap("extensions", source, Optional.ofNullable(model.getExtensions()));
}

private void debugMap(String name, String source, Optional<Map<?, ?>> collection) {
private Optional<Map<String, ?>> getMap(OpenAPI model, Function<Components, Map<String, ?>> extract) {
return Optional.ofNullable(model.getComponents()).map(extract);
}

private void debugMap(String name, String source, Optional<Map<String, ?>> collection) {
debugModel(name, source, collection.map(Map::size));
}

Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@Deprecated
class OpenApiProcessorTest {

ClassLoader loader;
Expand Down
Expand Up @@ -19,6 +19,7 @@

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.openapi.OASFactory;
import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.eclipse.microprofile.openapi.models.Operation;
import org.eclipse.microprofile.openapi.models.PathItem;
Expand All @@ -35,12 +36,9 @@
import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.openapi.api.OpenApiConfig;
import io.smallrye.openapi.api.models.ComponentsImpl;
import io.smallrye.openapi.api.models.OpenAPIImpl;
import io.smallrye.openapi.api.SmallRyeOpenAPI;
import io.smallrye.openapi.api.models.OperationImpl;
import io.smallrye.openapi.api.models.parameters.ParameterImpl;
import io.smallrye.openapi.runtime.io.Format;
import io.smallrye.openapi.runtime.io.OpenApiSerializer;

public class IndexScannerTestBase {

Expand Down Expand Up @@ -113,14 +111,27 @@ protected static DotName componentize(String className) {
return name;
}

public static void printToConsole(String entityName, Schema schema) throws IOException {
public static void printToConsole(String entityName, Schema schema) {
// Remember to set debug level logging.
LOG.debug(schemaToString(entityName, schema));
}

public static void printToConsole(OpenAPI oai) throws IOException {
public static void printToConsole(OpenAPI oai) {
// Remember to set debug level logging.
LOG.debug(OpenApiSerializer.serialize(oai, Format.JSON));
LOG.debug(toJSON(oai));
}

public static String toJSON(OpenAPI oai) {
return SmallRyeOpenAPI.builder()
.withConfig(config(Collections.emptyMap()))
.withInitialModel(oai)
.defaultRequiredProperties(false)
.enableModelReader(false)
.enableStandardStaticFiles(false)
.enableAnnotationScan(false)
.enableStandardFilter(false)
.build()
.toJSON();
}

public static void verifyMethodAndParamRefsPresent(OpenAPI oai) {
Expand Down Expand Up @@ -156,14 +167,10 @@ private static boolean isPathMatrixObject(Parameter parameter) {
&& parameter.getSchema() != null && parameter.getSchema().getType() == Schema.SchemaType.OBJECT;
}

public static String schemaToString(String entityName, Schema schema) throws IOException {
Map<String, Schema> map = new HashMap<>();
map.put(entityName, schema);
OpenAPIImpl oai = new OpenAPIImpl();
ComponentsImpl comp = new ComponentsImpl();
comp.setSchemas(map);
oai.setComponents(comp);
return OpenApiSerializer.serialize(oai, Format.JSON);
public static String schemaToString(String entityName, Schema schema) {
return toJSON(OASFactory.createOpenAPI()
.components(OASFactory.createComponents()
.addSchema(entityName, schema)));
}

public static void assertJsonEquals(String entityName, String expectedResource, Schema actual)
Expand All @@ -178,17 +185,42 @@ public static void assertJsonEquals(String expectedResource, OpenAPI actual) thr
}

public static void assertJsonEquals(URL expectedResourceUrl, OpenAPI actual) throws JSONException, IOException {
JSONAssert.assertEquals(loadResource(expectedResourceUrl), OpenApiSerializer.serialize(actual, Format.JSON),
true);
JSONAssert.assertEquals(loadResource(expectedResourceUrl), toJSON(actual), true);
}

public static OpenAPI scan(Class<?>... classes) {
return scan(config(Collections.emptyMap()), null, classes);
}

public static OpenAPI scan(InputStream customStaticFile, Class<?>... classes) {
return scan(config(Collections.emptyMap()), customStaticFile, classes);
}

public static OpenAPI scan(Config config, InputStream customStaticFile, Class<?>... classes) {
return scan(config, false, customStaticFile, classes);
}

public static OpenAPI scan(Config config, Class<?>... classes) {
return scan(config, false, null, classes);
}

public static void assertJsonEquals(String expectedResource, Class<?>... classes)
throws IOException, JSONException {
public static OpenAPI scan(Config config, boolean defaultRequiredProperties, InputStream customStaticFile, Class<?>... classes) {
Index index = indexOf(classes);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(dynamicConfig(new HashMap<String, String>()), index);
OpenAPI result = scanner.scan();

OpenAPI result = SmallRyeOpenAPI.builder()
.defaultRequiredProperties(defaultRequiredProperties)
.withCustomStaticFile(customStaticFile)
.withIndex(index)
.withConfig(config)
.build()
.model();

printToConsole(result);
assertJsonEquals(expectedResource, result);
return result;
}

public static void assertJsonEquals(String expectedResource, Class<?>... classes) throws IOException, JSONException {
assertJsonEquals(expectedResource, scan(config(Collections.emptyMap()), null, classes));
}

public static String loadResource(URL testResource) throws IOException {
Expand Down Expand Up @@ -223,10 +255,16 @@ public static OpenApiConfig dynamicConfig(Map<String, String> properties) {
return OpenApiConfig.fromConfig(config(properties));
}

public static Config config(String key, Object value) {
Map<String, String> config = new HashMap<>(1);
config.put(key, value.toString());
return config(config);
}

public static Config config(Map<String, String> properties) {
return new SmallRyeConfigBuilder()
.addDefaultSources()
.withSources(new PropertiesConfigSource(properties, "unit-test", ConfigSource.DEFAULT_ORDINAL))
.build();
}

}
Expand Up @@ -9,16 +9,15 @@
import java.util.Set;
import java.util.TreeSet;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.jboss.jandex.Index;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import com.fasterxml.jackson.databind.annotation.JsonNaming;

import io.smallrye.openapi.api.OpenApiConfig;
import io.smallrye.openapi.api.constants.JsonbConstants;
import io.smallrye.openapi.api.constants.OpenApiConstants;
import io.smallrye.openapi.runtime.OpenApiRuntimeException;
Expand All @@ -27,69 +26,47 @@ class PropertyNamingStrategyTest extends IndexScannerTestBase {

@Test
void testSnakeCase() throws Exception {
Index index = indexOf(NameStrategyBean1.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(
dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
OpenAPI result = scan(config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
"com.fasterxml.jackson.databind.PropertyNamingStrategies$SnakeCaseStrategy"),
index);

OpenAPI result = scanner.scan();

printToConsole(result);
NameStrategyBean1.class);
assertJsonEquals("components.schemas.name-strategy-snake.json", result);
}

@Test
void testJacksonNamingIgnoresConfig() throws Exception {
Index index = indexOf(NameStrategyBean2.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(
dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
OpenAPI result = scan(config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
"com.fasterxml.jackson.databind.PropertyNamingStrategies$SnakeCaseStrategy"),
index);

OpenAPI result = scanner.scan();

printToConsole(result);
NameStrategyBean2.class);
assertJsonEquals("components.schemas.name-strategy-ignored.json", result);
}

@Test
void testJacksonNamingOverridesConfig() throws Exception {
Index index = indexOf(NameStrategyKebab.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(
dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
OpenAPI result = scan(config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
"com.fasterxml.jackson.databind.PropertyNamingStrategies$SnakeCaseStrategy"),
index);

OpenAPI result = scanner.scan();

printToConsole(result);
NameStrategyKebab.class);
assertJsonEquals("components.schemas.name-strategy-kebab.json", result);
}

@Test
void testInvalidNamingStrategyClass() throws Exception {
Index index = indexOf(NameStrategyKebab.class);
OpenApiConfig config = dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
Config config = config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
"com.fasterxml.jackson.databind.PropertyNamingStrategies$InvalidStrategy");
assertThrows(OpenApiRuntimeException.class, () -> new OpenApiAnnotationScanner(config, index));
assertThrows(OpenApiRuntimeException.class, () -> scan(config, NameStrategyKebab.class));
}

@Test
void testNoValidTranslationMethods() throws Exception {
Index index = indexOf(NameStrategyKebab.class);
OpenApiConfig config = dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
NoValidTranslationMethods.class.getName());
assertThrows(OpenApiRuntimeException.class, () -> new OpenApiAnnotationScanner(config, index));
Config config = config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
NoValidTranslationMethods.class.getName());
assertThrows(OpenApiRuntimeException.class, () -> scan(config, NameStrategyKebab.class));
}

@Test
void testInvalidPropertyNameTranslationAttempt() throws Exception {
Index index = indexOf(NameStrategyBean3.class);
OpenApiConfig config = dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
Config config = config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY,
TranslationThrowsException.class.getName());
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(config, index);
assertThrows(OpenApiRuntimeException.class, () -> scanner.scan());
assertThrows(OpenApiRuntimeException.class, () -> scan(config, NameStrategyBean3.class));
}

@ParameterizedTest(name = "testJsonbConstantStrategy-{0}")
Expand All @@ -102,14 +79,7 @@ void testInvalidPropertyNameTranslationAttempt() throws Exception {
JsonbConstants.CASE_INSENSITIVE + ", simpleStringOne|anotherField|Y|z"
})
void testJsonbConstantStrategy(String strategy, String expectedNames) throws Exception {
Index index = indexOf(NameStrategyBean3.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(
dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, strategy),
index);

OpenAPI result = scanner.scan();

printToConsole(result);
OpenAPI result = scan(config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, strategy), NameStrategyBean3.class);
Set<String> expectedNameSet = new TreeSet<>(Arrays.asList(expectedNames.split("\\|")));
Map<String, org.eclipse.microprofile.openapi.models.media.Schema> schemas = result.getComponents().getSchemas();
org.eclipse.microprofile.openapi.models.media.Schema schema = schemas.get(NameStrategyBean3.class.getSimpleName());
Expand Down

0 comments on commit 4bc883e

Please sign in to comment.