Skip to content

Commit

Permalink
chore(citrus-http): review and code cleanup
Browse files Browse the repository at this point in the history
pr: #1224

`citrus-http` module.
  • Loading branch information
bbortt committed Oct 24, 2024
1 parent 53d09e0 commit 875c8c4
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.citrusframework.http.config.xml;

import jakarta.servlet.http.Cookie;
import org.citrusframework.http.message.HttpMessage;
import org.w3c.dom.Element;

import java.util.List;

import static java.lang.Boolean.parseBoolean;
import static java.lang.Integer.parseInt;

final class CookieUtils {

private CookieUtils() {
// Static utility class
}

static void setCookieElement(HttpMessage httpMessage, List<?> cookieElements) {
for (Object item : cookieElements) {
Element cookieElement = (Element) item;
Cookie cookie = new Cookie(cookieElement.getAttribute("name"), cookieElement.getAttribute("value"));

if (cookieElement.hasAttribute("path")) {
cookie.setPath(cookieElement.getAttribute("path"));
}

if (cookieElement.hasAttribute("domain")) {
cookie.setDomain(cookieElement.getAttribute("domain"));
}

if (cookieElement.hasAttribute("max-age")) {
cookie.setMaxAge(parseInt(cookieElement.getAttribute("max-age")));
}

if (cookieElement.hasAttribute("secure")) {
cookie.setSecure(parseBoolean(cookieElement.getAttribute("secure")));
}

httpMessage.cookie(cookie);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.citrusframework.http.config.xml;

import jakarta.servlet.http.Cookie;
import org.citrusframework.config.util.BeanDefinitionParserUtils;
import org.citrusframework.config.xml.DescriptionElementParser;
import org.citrusframework.config.xml.ReceiveMessageActionParser;
Expand All @@ -36,8 +35,8 @@
import java.util.List;

import static java.lang.Boolean.parseBoolean;
import static java.lang.Integer.parseInt;
import static org.citrusframework.config.xml.MessageSelectorParser.doParse;
import static org.citrusframework.http.config.xml.CookieUtils.setCookieElement;
import static org.springframework.util.xml.DomUtils.getChildElementByTagName;
import static org.springframework.util.xml.DomUtils.getChildElementsByTagName;

Expand Down Expand Up @@ -103,28 +102,7 @@ protected BeanDefinitionBuilder createBeanDefinitionBuilder(Element element,
}

List<?> cookieElements = getChildElementsByTagName(headers, "cookie");
for (Object item : cookieElements) {
Element cookieElement = (Element) item;
Cookie cookie = new Cookie(cookieElement.getAttribute("name"), cookieElement.getAttribute("value"));

if (cookieElement.hasAttribute("path")) {
cookie.setPath(cookieElement.getAttribute("path"));
}

if (cookieElement.hasAttribute("domain")) {
cookie.setDomain(cookieElement.getAttribute("domain"));
}

if (cookieElement.hasAttribute("max-age")) {
cookie.setMaxAge(parseInt(cookieElement.getAttribute("max-age")));
}

if (cookieElement.hasAttribute("secure")) {
cookie.setSecure(parseBoolean(cookieElement.getAttribute("secure")));
}

httpMessage.cookie(cookie);
}
setCookieElement(httpMessage, cookieElements);

boolean ignoreCase = !headers.hasAttribute("ignore-case") || parseBoolean(headers.getAttribute("ignore-case"));
validationContexts.stream()
Expand All @@ -135,8 +113,7 @@ protected BeanDefinitionBuilder createBeanDefinitionBuilder(Element element,

doParse(element, builder);

HttpMessageBuilder httpMessageBuilder = createMessageBuilder(
httpMessage);
HttpMessageBuilder httpMessageBuilder = createMessageBuilder(httpMessage);
DefaultMessageBuilder messageContentBuilder = constructMessageBuilder(body, builder);

httpMessageBuilder.setName(messageContentBuilder.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package org.citrusframework.http.config.xml;

import java.util.ArrayList;
import java.util.List;

import jakarta.servlet.http.Cookie;
import org.citrusframework.config.util.BeanDefinitionParserUtils;
import org.citrusframework.config.xml.DescriptionElementParser;
Expand All @@ -37,20 +34,21 @@
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.List;

/**
* @since 2.4
*/
public class HttpSendRequestActionParser extends SendMessageActionParser {

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = createBeanDefinitionBuilder(
element, parserContext);
BeanDefinitionBuilder builder = createBeanDefinitionBuilder(element, parserContext);
return builder.getBeanDefinition();
}

protected BeanDefinitionBuilder createBeanDefinitionBuilder(Element element,
ParserContext parserContext) {
protected BeanDefinitionBuilder createBeanDefinitionBuilder(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = parseComponent(element, parserContext);
builder.addPropertyValue("name", "http:" + element.getLocalName());

Expand Down Expand Up @@ -146,8 +144,7 @@ protected BeanDefinitionBuilder createBeanDefinitionBuilder(Element element,
}
}

HttpMessageBuilder httpMessageBuilder = createMessageBuilder(
httpMessage);
HttpMessageBuilder httpMessageBuilder = createMessageBuilder(httpMessage);
DefaultMessageBuilder messageContentBuilder = constructMessageBuilder(body, builder);

httpMessageBuilder.setName(messageContentBuilder.getName());
Expand All @@ -162,19 +159,20 @@ protected BeanDefinitionBuilder createBeanDefinitionBuilder(Element element,
if (!variableExtractors.isEmpty()) {
builder.addPropertyValue("variableExtractors", variableExtractors);
}

return builder;
}

protected Element getRequestElement(Element element) {
if (element.hasChildNodes()) {
return DomUtils.getChildElements(element).get(0);
}

throw new BeanCreationException("No request element specified for http send - invalid test action definition");
}

protected Element getHeadersElement(Element requestElement) {
Element headers = DomUtils.getChildElementByTagName(requestElement, "headers");
return headers;
return DomUtils.getChildElementByTagName(requestElement, "headers");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.citrusframework.http.config.xml;

import jakarta.servlet.http.Cookie;
import org.citrusframework.config.xml.SendMessageActionParser;
import org.citrusframework.http.message.HttpMessage;
import org.citrusframework.http.message.HttpMessageBuilder;
Expand All @@ -30,10 +29,9 @@

import java.util.List;

import static java.lang.Boolean.parseBoolean;
import static java.lang.Integer.parseInt;
import static org.citrusframework.config.util.BeanDefinitionParserUtils.setPropertyReference;
import static org.citrusframework.config.xml.DescriptionElementParser.doParse;
import static org.citrusframework.http.config.xml.CookieUtils.setCookieElement;
import static org.citrusframework.util.StringUtils.hasText;

/**
Expand Down Expand Up @@ -78,28 +76,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
}

List<?> cookieElements = DomUtils.getChildElementsByTagName(headers, "cookie");
for (Object item : cookieElements) {
Element cookieElement = (Element) item;
Cookie cookie = new Cookie(cookieElement.getAttribute("name"), cookieElement.getAttribute("value"));

if (cookieElement.hasAttribute("path")) {
cookie.setPath(cookieElement.getAttribute("path"));
}

if (cookieElement.hasAttribute("domain")) {
cookie.setDomain(cookieElement.getAttribute("domain"));
}

if (cookieElement.hasAttribute("max-age")) {
cookie.setMaxAge(parseInt(cookieElement.getAttribute("max-age")));
}

if (cookieElement.hasAttribute("secure")) {
cookie.setSecure(parseBoolean(cookieElement.getAttribute("secure")));
}

httpMessage.cookie(cookie);
}
setCookieElement(httpMessage, cookieElements);
}

Element body = DomUtils.getChildElementByTagName(element, "body");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ public void handleRequest(String request) {
logger.debug("Sending Http request message");
messageListener.onOutboundMessage(new RawMessage(request), contextFactory.getObject());
} else {
<<<<<<< Updated upstream
if (logger.isDebugEnabled()) {
logger.debug("Sending Http request message:" + NEWLINE + request);
logger.debug("Sending Http request message:{}{}", NEWLINE, request);
}
=======
logger.debug("Sending Http request message:{}{}", NEWLINE, request);
>>>>>>> Stashed changes
}
}

Expand All @@ -97,9 +101,13 @@ public void handleResponse(String response) {
logger.debug("Received Http response message");
messageListener.onInboundMessage(new RawMessage(response), contextFactory.getObject());
} else {
<<<<<<< Updated upstream
if (logger.isDebugEnabled()) {
logger.debug("Received Http response message:" + NEWLINE + response);
logger.debug("Received Http response message:{}{}", NEWLINE, response);
}
=======
logger.debug("Received Http response message:{}{}", NEWLINE, response);
>>>>>>> Stashed changes
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

import org.citrusframework.context.TestContextFactory;
import org.citrusframework.http.controller.HttpMessageController;
import org.citrusframework.message.RawMessage;
Expand All @@ -33,6 +30,9 @@
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Enumeration;

import static java.lang.System.lineSeparator;

/**
Expand Down Expand Up @@ -80,9 +80,13 @@ public void handleRequest(String request) {
logger.debug("Received Http request");
messageListener.onInboundMessage(new RawMessage(request), contextFactory.getObject());
} else {
<<<<<<< Updated upstream
if (logger.isDebugEnabled()) {
logger.debug("Received Http request:" + NEWLINE + request);
logger.debug("Received Http request:{}{}", NEWLINE, request);
}
=======
logger.debug("Received Http request:{}{}", NEWLINE, request);
>>>>>>> Stashed changes
}
}

Expand All @@ -95,9 +99,13 @@ public void handleResponse(String response) {
logger.debug("Sending Http response");
messageListener.onOutboundMessage(new RawMessage(response), contextFactory.getObject());
} else {
<<<<<<< Updated upstream
if (logger.isDebugEnabled()) {
logger.debug("Sending Http response:" + NEWLINE + response);
logger.debug("Sending Http response:{}{}", NEWLINE, response);
}
=======
logger.debug("Sending Http response:{}{}", NEWLINE, response);
>>>>>>> Stashed changes
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@

package org.citrusframework.http.message;

import static org.citrusframework.http.message.HttpMessageHeaders.HTTP_QUERY_PARAMS;
import static org.citrusframework.util.StringUtils.hasText;
import org.apache.commons.lang3.tuple.Pair;
import org.citrusframework.message.Message;
import org.citrusframework.message.MessageHeaders;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair;
import org.citrusframework.message.Message;
import org.citrusframework.message.MessageHeaders;

import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.citrusframework.http.message.HttpMessageHeaders.HTTP_QUERY_PARAMS;
import static org.citrusframework.util.StringUtils.hasText;

/**
* @since 2.7.5
Expand Down Expand Up @@ -95,9 +98,9 @@ public static Map<String, List<String>> getQueryParameterMap(HttpMessage httpMes
String value = keyAndValue.length > 1 ? keyAndValue[1] : "";
return Pair.of(key, value);
})
.collect(Collectors.groupingBy(
Pair::getLeft, Collectors.mapping(Pair::getRight, Collectors.toList())));
.collect(groupingBy(Pair::getLeft,mapping(Pair::getRight,toList())));
}
return Collections.emptyMap();

return emptyMap();
}
}
Loading

0 comments on commit 875c8c4

Please sign in to comment.