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

Configurable SoapMarshalling #229

Merged
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- the SoapMarshalling is now configurable regarding packages to scan for JAXB classes and the schemas for validation
- the collected data is now flushed after each precondition
- moved test case specific parameter into separate file test_parameter.toml
- sdc-ri version to 6.2.0-SNAPSHOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
import java.util.List;
import org.somda.sdc.common.guice.AbstractConfigurationModule;

/**
Expand All @@ -27,11 +29,26 @@ private MarshallingUtil() {}
* @return injector configured for marshalling
*/
public static Injector createMarshallingTestInjector(final boolean validateMessages) {
return createMarshallingTestInjector(validateMessages, SoapMarshalling.PACKAGES, SoapMarshalling.SCHEMAS);
}

/**
* Create an injector which supports soap marshalling.
*
* @param validateMessages enable schema validation
* @param packages packages to scan for JAXB classes
* @param schemas schemas to validate against
* @return injector configured for marshalling
*/
public static Injector createMarshallingTestInjector(
final boolean validateMessages, final List<String> packages, final List<String> schemas) {
final var testInjector = Guice.createInjector(
new AbstractConfigurationModule() {
@Override
protected void defaultConfigure() {
bind(MarshallingConfig.VALIDATE_SOAP_MESSAGES, Boolean.class, validateMessages);
bind(MarshallingConfig.PACKAGES, new TypeLiteral<>() {}, packages);
bind(MarshallingConfig.SCHEMAS, new TypeLiteral<>() {}, schemas);
}
},
new AbstractModule() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* Utility for marshalling classes to XML.
*/
public class SoapMarshalling {

private static final Logger LOG = LogManager.getLogger(SoapMarshalling.class);

private static final String PKG_BASE = "com.draeger.medical.";
Expand Down Expand Up @@ -70,44 +71,40 @@ public class SoapMarshalling {
private static final String PKG_DELIM = ":";
private static final String SCHEMA_DELIM = ":";

public static final List<String> PACKAGES =
List.of(PKG_EXT, PKG_PM, PKG_MSG, PKG_SOAP, PKG_DPWS, PKG_WSA, PKG_WSD, PKG_WSE, PKG_WST, PKG_MEX);
public static final List<String> SCHEMAS = List.of(
SCHEMA_SOAP, SCHEMA_WSA, SCHEMA_WSD, SCHEMA_WSE, SCHEMA_MEX, SCHEMA_WST, SCHEMA_DPWS, SCHEMA_BICEPS);

private final JAXBContext jaxbContext;
private final Schema schema;

/**
* Create a SoapMarshalling instance for use in unit tests.
*
* @param validateMessages enable schema validation for messages
* @param packages packages to scan for JAXB classes
* @param schemas schemas to validate against
* @throws ParserConfigurationException on error when preparing schema
* @throws SAXException on error when preparing schema
* @throws IOException on error when preparing schema
*/
@Inject
public SoapMarshalling(@Named(MarshallingConfig.VALIDATE_SOAP_MESSAGES) final boolean validateMessages)
public SoapMarshalling(
@Named(MarshallingConfig.VALIDATE_SOAP_MESSAGES) final boolean validateMessages,
@Named(MarshallingConfig.PACKAGES) final List<String> packages,
@Named(MarshallingConfig.SCHEMAS) final List<String> schemas)
throws ParserConfigurationException, SAXException, IOException {
final var packageList =
List.of(PKG_EXT, PKG_PM, PKG_MSG, PKG_SOAP, PKG_DPWS, PKG_WSA, PKG_WSD, PKG_WSE, PKG_WST, PKG_MEX);
final String contextPackages = String.join(PKG_DELIM, packageList);

try {
jaxbContext = JAXBContext.newInstance(contextPackages);
jaxbContext = JAXBContext.newInstance(String.join(PKG_DELIM, packages));
} catch (final JAXBException e) {
LOG.error("JAXB context for SOAP model(s) could not be created", e);
throw new RuntimeException("JAXB context for SOAP model(s) could not be created");
}

if (validateMessages) {
final var schemaList = List.of(
SCHEMA_SOAP,
SCHEMA_WSA,
SCHEMA_WSD,
SCHEMA_WSE,
SCHEMA_MEX,
SCHEMA_WST,
SCHEMA_DPWS,
SCHEMA_BICEPS);
final var schemas = String.join(SCHEMA_DELIM, schemaList);

schema = generateTopLevelSchema(schemas);
schema = generateTopLevelSchema(String.join(SCHEMA_DELIM, schemas));
} else {
schema = null;
}
Expand All @@ -131,7 +128,7 @@ public void marshal(final JAXBElement<Envelope> envelope, final OutputStream out
/**
* Takes an InputStream and unmarshals it.
*
* @param inputStream the inputStream to unmarshal
* @param inputStream the inputStream to unmarshal
* @return an Envelope created from the inputstream
* @throws JAXBException if marshalling fails
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ public final class MarshallingConfig {
*/
public static final String VALIDATE_SOAP_MESSAGES = "ValidateSoapMessages";

public static final String PACKAGES = "Packages";
public static final String SCHEMAS = "Schemas";

private MarshallingConfig() {}
}
Loading