Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#1156): provide test api generator #1160

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
/**
* Interface for providing TestCaseRunner.
*
* @author Thorsten Schlathoelter
* @since 4.0
*/
public interface TestCaseRunnerProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,38 @@ public static String prettyPrintJson(String payload) {
}
return sb.toString();
}

/**
* Normalizes the given text by replacing all whitespace characters (identified by {@link Character#isWhitespace) by a single space
* and replacing windows style line endings with unix style line endings.
*/
public static String normalizeWhitespace(String text, boolean normalizeWhitespace, boolean normalizeLineEndingsToUnix) {
if (text == null || text.isEmpty()) {
return text;
}

if (normalizeWhitespace) {
StringBuilder result = new StringBuilder();
boolean lastWasSpace = true;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isWhitespace(c)) {
if (!lastWasSpace) {
result.append(' ');
}
lastWasSpace = true;
} else {
result.append(c);
lastWasSpace = false;
}
}
return result.toString().trim();
}

if (normalizeLineEndingsToUnix) {
return text.replaceAll("\\r(\\n)?", "\n");
}

return text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.citrusframework.testapi;

import java.util.Map;

/**
* Interface representing a generated API from an OpenAPI specification.
* Provides methods to retrieve metadata about the API such as title, version,
* prefix, and information extensions.
*/
public interface GeneratedApi {
tschlat marked this conversation as resolved.
Show resolved Hide resolved

/**
* Retrieves the title of the OpenAPI specification, as specified in the info section of the API.
*
* @return the title of the OpenAPI specification
*/
String getApiTitle();

/**
* Retrieves the version of the OpenAPI specification, as specified in the info section of the API.
*
* @return the version of the OpenAPI specification
*/
String getApiVersion();

/**
* Retrieves the prefix used for the API, as specified in the API generation configuration.
*
* @return the prefix used for the API
*/
String getApiPrefix();

/**
* Retrieves the specification extensions of the OpenAPI defined in the "info" section.
* <p>
* Specification extensions, also known as vendor extensions, are custom key-value pairs used to describe extra
* functionality not covered by the standard OpenAPI Specification. These properties start with "x-".
* This method collects only the extensions defined in the "info" section of the API.
* </p>
*
* @return a map containing the specification extensions defined in the "info" section of the API,
* where keys are extension names and values are extension values
*/
Map<String, String> getApiInfoExtensions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.citrusframework.testapi;

/**
* Interface representing a generated API request corresponding to an operation in an OpenAPI specification.
* Provides methods to retrieve metadata about the request such as operation name, HTTP method, and path.
*/
public interface GeneratedApiRequest {

/**
* Retrieves the name of the OpenAPI operation associated with the request.
*
* @return the name of the OpenAPI operation
*/
String getOperationName();

/**
* Retrieves the HTTP method used for the request.
*
* @return the HTTP method used for the request (e.g., GET, POST)
*/
String getMethod();

/**
* Retrieves the path used for the request.
*
* @return the path used for the request
*/
String getPath();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

/**
* Class extracting values of segments of VariableExpressions.
*
* @author Thorsten Schlathoelter
*/
public interface SegmentVariableExtractor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
* the extractors managed by this registry in order to access variable content from the TestContext expressed by variable expressions.
* <p>
* Registry provides all known {@link SegmentVariableExtractor}s.
*
* @author Thorsten Schlathoelter
*/
public class SegmentVariableExtractorRegistry {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
* <li>the third element of the <b>persons</b> property of the variable retrieved in the previous step</li>
* <li>the first element of the <b>firstnames</b> property of the property retrieved in the previous step</li>
* </ol>
*
* @author Thorsten Schlathoelter
*/
public class VariableExpressionIterator implements Iterator<VariableSegment> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,79 @@

package org.citrusframework.message;

import static org.testng.Assert.assertEquals;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class MessagePayloadUtilsTest {

@Test
public void shouldPrettyPrintJson() {
Assert.assertEquals(MessagePayloadUtils.prettyPrint(""), "");
Assert.assertEquals(MessagePayloadUtils.prettyPrint("{}"), "{}");
Assert.assertEquals(MessagePayloadUtils.prettyPrint("[]"), "[]");
Assert.assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\"}"),
assertEquals(MessagePayloadUtils.prettyPrint(""), "");
assertEquals(MessagePayloadUtils.prettyPrint("{}"), "{}");
assertEquals(MessagePayloadUtils.prettyPrint("[]"), "[]");
assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\"}"),
String.format("{%n \"user\": \"citrus\"%n}"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("{\"text\":\"<?;,{}' '[]:>\"}"),
assertEquals(MessagePayloadUtils.prettyPrint("{\"text\":\"<?;,{}' '[]:>\"}"),
String.format("{%n \"text\": \"<?;,{}' '[]:>\"%n}"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint(String.format("%n%n { \"user\":%n%n \"citrus\" }")),
assertEquals(MessagePayloadUtils.prettyPrint(String.format("%n%n { \"user\":%n%n \"citrus\" }")),
String.format("{%n \"user\": \"citrus\"%n}"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\",\"age\": 32}"),
assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\",\"age\": 32}"),
String.format("{%n \"user\": \"citrus\",%n \"age\": 32%n}"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("[22, 32]"),
assertEquals(MessagePayloadUtils.prettyPrint("[22, 32]"),
String.format("[%n22,%n32%n]"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("[{\"user\":\"citrus\",\"age\": 32}]"),
assertEquals(MessagePayloadUtils.prettyPrint("[{\"user\":\"citrus\",\"age\": 32}]"),
String.format("[%n {%n \"user\": \"citrus\",%n \"age\": 32%n }%n]"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("[{\"user\":\"citrus\",\"age\": 32}, {\"user\":\"foo\",\"age\": 99}]"),
assertEquals(MessagePayloadUtils.prettyPrint("[{\"user\":\"citrus\",\"age\": 32}, {\"user\":\"foo\",\"age\": 99}]"),
String.format("[%n {%n \"user\": \"citrus\",%n \"age\": 32%n },%n {%n \"user\": \"foo\",%n \"age\": 99%n }%n]"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\",\"age\": 32,\"pet\":{\"name\": \"fluffy\", \"age\": 4}}"),
assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\",\"age\": 32,\"pet\":{\"name\": \"fluffy\", \"age\": 4}}"),
String.format("{%n \"user\": \"citrus\",%n \"age\": 32,%n \"pet\": {%n \"name\": \"fluffy\",%n \"age\": 4%n }%n}"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\",\"age\": 32,\"pets\":[\"fluffy\",\"hasso\"]}"),
assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\",\"age\": 32,\"pets\":[\"fluffy\",\"hasso\"]}"),
String.format("{%n \"user\": \"citrus\",%n \"age\": 32,%n \"pets\": [%n \"fluffy\",%n \"hasso\"%n ]%n}"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\",\"pets\":[\"fluffy\",\"hasso\"],\"age\": 32}"),
assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\",\"pets\":[\"fluffy\",\"hasso\"],\"age\": 32}"),
String.format("{%n \"user\": \"citrus\",%n \"pets\": [%n \"fluffy\",%n \"hasso\"%n ],%n \"age\": 32%n}"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\",\"pets\":[{\"name\": \"fluffy\", \"age\": 4},{\"name\": \"hasso\", \"age\": 2}],\"age\": 32}"),
assertEquals(MessagePayloadUtils.prettyPrint("{\"user\":\"citrus\",\"pets\":[{\"name\": \"fluffy\", \"age\": 4},{\"name\": \"hasso\", \"age\": 2}],\"age\": 32}"),
String.format("{%n \"user\": \"citrus\",%n \"pets\": [%n {%n \"name\": \"fluffy\",%n \"age\": 4%n },%n {%n \"name\": \"hasso\",%n \"age\": 2%n }%n ],%n \"age\": 32%n}"));
}

@Test
public void shouldPrettyPrintXml() {
Assert.assertEquals(MessagePayloadUtils.prettyPrint(""), "");
Assert.assertEquals(MessagePayloadUtils.prettyPrint("<root></root>"),
assertEquals(MessagePayloadUtils.prettyPrint(""), "");
assertEquals(MessagePayloadUtils.prettyPrint("<root></root>"),
String.format("<root>%n</root>%n"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("<root><text>Citrus rocks!</text></root>"),
assertEquals(MessagePayloadUtils.prettyPrint("<root><text>Citrus rocks!</text></root>"),
String.format("<root>%n <text>Citrus rocks!</text>%n</root>%n"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><text>Citrus rocks!</text></root>"),
assertEquals(MessagePayloadUtils.prettyPrint("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><text>Citrus rocks!</text></root>"),
String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n<root>%n <text>Citrus rocks!</text>%n</root>%n"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint(String.format("<root>%n<text>%nCitrus rocks!%n</text>%n</root>")),
assertEquals(MessagePayloadUtils.prettyPrint(String.format("<root>%n<text>%nCitrus rocks!%n</text>%n</root>")),
String.format("<root>%n <text>Citrus rocks!</text>%n</root>%n"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint(String.format("<root>%n <text language=\"eng\">%nCitrus rocks!%n </text>%n</root>")),
assertEquals(MessagePayloadUtils.prettyPrint(String.format("<root>%n <text language=\"eng\">%nCitrus rocks!%n </text>%n</root>")),
String.format("<root>%n <text language=\"eng\">Citrus rocks!</text>%n</root>%n"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint(String.format("%n%n <root><text language=\"eng\"><![CDATA[Citrus rocks!]]></text></root>")),
assertEquals(MessagePayloadUtils.prettyPrint(String.format("%n%n <root><text language=\"eng\"><![CDATA[Citrus rocks!]]></text></root>")),
String.format("<root>%n <text language=\"eng\">%n <![CDATA[Citrus rocks!]]>%n </text>%n</root>%n"));
Assert.assertEquals(MessagePayloadUtils.prettyPrint(String.format("<root><text language=\"eng\" important=\"true\"><![CDATA[%n Citrus rocks!%n ]]></text></root>")),
assertEquals(MessagePayloadUtils.prettyPrint(String.format("<root><text language=\"eng\" important=\"true\"><![CDATA[%n Citrus rocks!%n ]]></text></root>")),
String.format("<root>%n <text language=\"eng\" important=\"true\">%n <![CDATA[%n Citrus rocks!%n ]]>%n </text>%n</root>%n"));
}

@DataProvider
public Object[][] normalizeWhitespaceProvider() {
return new Object[][] {
// Test data: payload, ignoreWhitespace, ignoreNewLineType, expected result
{"Hello \t\r\nWorld\r\n", true, true, "Hello World"},
{"Hello \t\r\nWorld\r\n", true, false, "Hello World"},
{"Hello \t\r\nWorld\r\n", false, true, "Hello \t\nWorld\n"},
{"Hello \t\r\nWorld\r\n", false, false, "Hello \t\r\nWorld\r\n"},
{"", true, true, ""},
{"", false, false, ""},
{null, true, true, null},
{null, false, false, null}
};
}

@Test(dataProvider = "normalizeWhitespaceProvider")
public void normalizeWhitespace(String text, boolean normalizeWhitespace, boolean normalizeLineEndingsToUnix, String expected) {
assertEquals(MessagePayloadUtils.normalizeWhitespace(text, normalizeWhitespace, normalizeLineEndingsToUnix), expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package org.citrusframework.spi.mocks;

/**
* @author Thorsten Schlathoelter
*/
public class FooWithParams {

public FooWithParams(int intParam, short shortParam, double doubleParam, float floatParam,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package org.citrusframework.spi.mocks;

/**
* @author Thorsten Schlathoelter
*/
public class SingletonFoo {

public static final SingletonFoo INSTANCE = new SingletonFoo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
* The specified file must define the type of {@link TestCaseRunnerProvider} responsible for
* delivering the custom test case runner.
*
* @author Thorsten Schlathoelter
* @since 4.0
* @see TestCaseRunnerProvider
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public String getEndpointUri() {
/**
* Action builder.
*/
public static final class Builder extends SendMessageActionBuilder<SendMessageAction, SendMessageActionBuilderSupport, Builder> {
public static class Builder extends SendMessageActionBuilder<SendMessageAction, SendMessageActionBuilderSupport, Builder> {

/**
* Fluent API action building entry method used in Java DSL.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.citrusframework.testapi;

import org.citrusframework.TestAction;
import org.citrusframework.actions.SendMessageAction.SendMessageActionBuilder;
import org.citrusframework.context.TestContext;

/**
* Implementors of this interface are used to customize the SendMessageActionBuilder with application specific information. E.g. cookies
* or transactionIds.
*/
public interface ApiActionBuilderCustomizerService {
<T extends SendMessageActionBuilder<?,?,?>> T build(GeneratedApi generatedApi, TestAction action, TestContext context, T builder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
* loading SpringXmlTest.
*
*
* @author Thorsten Schlathoelter
* @since 4.0
* @see SpringXmlTestLoader
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
* This configuration is necessary only when using Spring without Spring Boot. Spring Boot includes a standard conversion service
* that automatically detects and uses relevant converters by default.
*
* @author Thorsten Schlathoelter
* @since 4.0
*/
@Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* Namespace handler registers bean definition parser
* for Citrus testcase schema elements.
*
* @author Christoph Deppisch, Thorsten Schlathoelter
* @author Christoph Deppisch
* @since 2007
*/
public class CitrusTestCaseNamespaceHandler extends NamespaceHandlerSupport {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
*
* @param <T>
*
* @author Thorsten Schlathoelter
*/
public abstract class BaseTestCaseMetaInfoParser<T extends TestCaseMetaInfo> implements BeanDefinitionParser {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*
* @param <T>
*
* @author Christop Deppisch, Thorsten Schlathoelter
* @author Christop Deppisch
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class BaseTestCaseParser<T extends TestCase> implements BeanDefinitionParser {
Expand Down
Loading