From 4be1463cb5559ebaba50e0336e9c4454e683567e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6rdes?= Date: Thu, 14 Nov 2024 15:45:41 +0100 Subject: [PATCH] Refactor SOAPProxy to handle multiple services in WSDL - Add method to retrieve a service by name from definitions. - Implement detailed error handling for missing services. - Introduce utility method to list service names. - Update tests to cover scenarios for multiple WSDL services. --- .../membrane/core/rules/SOAPProxy.java | 18 +++++++---- .../membrane/core/rules/SOAPProxyTest.java | 30 +++++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/com/predic8/membrane/core/rules/SOAPProxy.java b/core/src/main/java/com/predic8/membrane/core/rules/SOAPProxy.java index 4f0184cf3..7bb6893d1 100644 --- a/core/src/main/java/com/predic8/membrane/core/rules/SOAPProxy.java +++ b/core/src/main/java/com/predic8/membrane/core/rules/SOAPProxy.java @@ -26,12 +26,14 @@ import com.predic8.membrane.core.util.*; import com.predic8.wsdl.*; import org.apache.commons.lang3.*; +import org.jetbrains.annotations.NotNull; import org.slf4j.*; import javax.xml.namespace.*; import java.net.*; import java.util.*; import java.util.regex.*; +import java.util.stream.Collectors; import static com.predic8.membrane.core.Constants.*; @@ -122,15 +124,21 @@ void parseWSDL() throws Exception { } } - private static Service getService(Definitions definitions) { + private Service getService(Definitions definitions) { List services = definitions.getServices(); - - - if (services.size() == 1) return services.get(0); - throw new IllegalArgumentException("There are " + services.size() + " services defined in the WSDL, but exactly 1 is required for soapProxy."); + return services.stream() + .filter(s -> s.getName().equals(serviceName)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("No Service with the name %s found. Available Services: %s".formatted(serviceName, getServiceNames(services)))); + } + + private static @NotNull List getServiceNames(List services) { + return services.stream() + .map(WSDLElement::getName) + .toList(); } private void handleException(Exception e) throws ResourceRetrievalException, UnknownHostException, ConnectException { diff --git a/core/src/test/java/com/predic8/membrane/core/rules/SOAPProxyTest.java b/core/src/test/java/com/predic8/membrane/core/rules/SOAPProxyTest.java index c827a693b..870026321 100644 --- a/core/src/test/java/com/predic8/membrane/core/rules/SOAPProxyTest.java +++ b/core/src/test/java/com/predic8/membrane/core/rules/SOAPProxyTest.java @@ -2,9 +2,16 @@ import com.predic8.membrane.core.*; import com.predic8.membrane.core.exchangestore.*; +import com.predic8.membrane.core.openapi.util.TestUtils; import com.predic8.membrane.core.transport.http.*; +import io.restassured.response.ResponseBody; import org.junit.jupiter.api.*; +import static com.predic8.membrane.core.http.MimeType.TEXT_XML; +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertThrows; + public class SOAPProxyTest { Router router; @@ -15,12 +22,18 @@ public class SOAPProxyTest { void setUp() throws Exception { proxy = new SOAPProxy(); + proxy.setPort(2000); router = new Router(); router.setTransport(new HttpTransport()); router.setExchangeStore(new ForgetfulExchangeStore()); router.add(proxy); } + @AfterEach + void shutDown() { + router.stop(); + } + @Test void parseWSDL() throws Exception { proxy.setWsdl("classpath:/ws/cities.wsdl"); @@ -36,15 +49,28 @@ void parseWSDLWithMultiplePortsPerService() throws Exception { @Test void parseWSDLWithMultipleServices() { proxy.setWsdl("classpath:/ws/cities-2-services.wsdl"); - Assertions.assertThrows(IllegalArgumentException.class, () -> router.init()); + assertThrows(IllegalArgumentException.class, () -> router.init()); } @Test - void parseWSDLWithMultipleServicesForAGivenService() throws Exception { + void parseWSDLWithMultipleServicesForAGivenServiceA() throws Exception { proxy.setServiceName("CityServiceA"); proxy.setWsdl("classpath:/ws/cities-2-services.wsdl"); router.init(); } + @Test + void parseWSDLWithMultipleServicesForAGivenServiceB() throws Exception { + proxy.setServiceName("CityServiceB"); + proxy.setWsdl("classpath:/ws/cities-2-services.wsdl"); + router.init(); + } + + @Test + void parseWSDLWithMultipleServicesForAWrongService() { + proxy.setServiceName("WrongService"); + proxy.setWsdl("classpath:/ws/cities-2-services.wsdl"); + assertThrows(IllegalArgumentException.class, () ->router.init()); + } } \ No newline at end of file