Skip to content

Commit

Permalink
Refactor SOAPProxy to handle multiple services in WSDL
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
christiangoerdes committed Nov 14, 2024
1 parent c1e18e7 commit 4be1463
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
18 changes: 13 additions & 5 deletions core/src/main/java/com/predic8/membrane/core/rules/SOAPProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -122,15 +124,21 @@ void parseWSDL() throws Exception {
}
}

private static Service getService(Definitions definitions) {
private Service getService(Definitions definitions) {
List<Service> 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<String> getServiceNames(List<Service> services) {
return services.stream()
.map(WSDLElement::getName)
.toList();
}

private void handleException(Exception e) throws ResourceRetrievalException, UnknownHostException, ConnectException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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());
}

}

0 comments on commit 4be1463

Please sign in to comment.