public class MockUtil extends Object
Upon construction, this class automatically loads test systems and credentials from resource files
TEST_SYSTEMS_RESOURCE_FILE
and CREDENTIALS_RESOURCE_FILE
(appending the file extensions defined by
CONFIG_FILE_EXTENSIONS
). For examples of such files, refer to loadTestSystems(File)
and
loadCredentials(File)
.
In addition, test systems and credentials are read from system properties PROPERTY_TEST_SYSTEMS
and
PROPERTY_CREDENTIALS
. Both properties can be used to specify either the path to or the direct content of a
configuration file. For example, these can be specified via
mvn clean install -Dtest.systems="..." -Dtest.credentials="..."
where "..."
corresponds to
either the path to a configuration file or the content of such a file.
When specifying paths to test systems and credentials files, the file extension can be omitted (e.g.,
test/resources/systems
). In this case, this class will scan for the existence of the following files:
test/resources/systems.json
test/resources/systems.yaml
test/resources/systems.yml
Constructor and Description |
---|
MockUtil()
Instantiates a new instance of
MockUtil , invalidates caches, and resets Hystrix. |
Modifier and Type | Method and Description |
---|---|
void |
addCredentials(String systemAlias,
Credentials credentials)
Registers the given
Credentials to enable retrieval by its alias. |
void |
addDefaultErpSystem(ErpSystem erpSystem)
Adds and sets the default ERP system.
|
void |
addTestSystem(TestSystem<?> testSystem)
Registers the given
TestSystem to enable retrieval by its alias. |
void |
clearCredentials()
Clears all
Credentials . |
void |
clearDestinations()
Clears all previously mocked
Destination s. |
void |
clearKeyStores()
Clears all previously mocked
KeyStore s. |
void |
clearSecretStores()
Clears all previously mocked
SecretStore s. |
void |
clearTenants()
Clears all previously mocked
Tenant s. |
void |
clearTestSystems()
Clears all
TestSystem s. |
void |
clearUsers()
Clears all previously mocked
User s. |
Credentials |
getCredentials(String systemAlias)
Returns the credentials for the given alias.
|
Credentials |
getCredentials(TestSystem<?> testSystem)
Returns the credentials for the given
TestSystem . |
ErpSystem |
getErpSystem() |
ErpSystem |
getErpSystem(String systemAlias)
Returns an
ErpSystem for the given alias. |
TestSystem<?> |
getTestSystem(String systemAlias)
Returns the
TestSystem for the given alias. |
void |
loadCredentials(File file)
Loads credential configuration from the resource file with the given name.
|
void |
loadCredentials(String resourceFileName)
Delegates to
loadCredentials(File) and loads credential configuration from the resource file with the
given name. |
void |
loadTestSystems(File file)
Loads test system configuration from the given file.
|
void |
loadTestSystems(String resourceFileName)
Delegates to
loadTestSystems(File) and loads test system configuration from the resource file with the
given name. |
AuditLog |
mockAuditLog() |
CloudPlatform |
mockCurrentCloudPlatform() |
CloudPlatform |
mockCurrentCloudPlatform(String applicationName) |
void |
mockCurrentLocales() |
void |
mockCurrentLocales(Locale locale,
Locale... additionalLocales) |
Tenant |
mockCurrentTenant() |
Tenant |
mockCurrentTenant(String tenantId) |
User |
mockCurrentUser() |
User |
mockCurrentUser(String userName) |
User |
mockCurrentUser(String userName,
Locale locale,
Collection<? extends Authorization> authorizations,
Map<String,? extends UserAttribute> attributes) |
void |
mockDefaults()
Mocks typical defaults for a test, for example, the respective facades that are used as Cloud platform
abstractions.
|
Destination |
mockDestination(String name,
String systemAlias)
Mocks a destination with a given name to the given system with the registered credentials.
|
Destination |
mockDestination(String name,
TestSystem<?> testSystem)
Mocks a destination with a given name to the given
TestSystem with the registered credentials. |
Destination |
mockDestination(String name,
URI uri)
Mocks a destination with a given name to redirect to the given
URI with no authentication. |
Destination |
mockDestination(String name,
URI uri,
AuthenticationType authenticationType,
ProxyType proxyType,
ProxyConfiguration proxyConfiguration,
List<Header> headers,
KeyStore trustStore,
Boolean isTrustingAllCertificates,
KeyStore keyStore,
String keyStorePassword,
Map<String,String> propertiesByName)
Mocks a destination with the given authentication type and name to redirect to the given
URI . |
Destination |
mockDestination(String name,
URI uri,
Credentials credentials)
Mocks a destination with a given name to redirect to the given
URI with the given Credentials . |
Destination |
mockErpDestination()
Mocks an ERP destination by redirecting to an actual ERP system for destination name
ErpDestination.getDefaultName() . |
Destination |
mockErpDestination(ErpSystem erpSystem)
Mocks an ERP destination by redirecting to an actual ERP system for destination name
ErpDestination.getDefaultName() . |
Destination |
mockErpDestination(ErpSystem erpSystem,
Credentials credentials)
Mocks an ERP destination for the given
URI and SapClient for destination name
ErpDestination.getDefaultName() . |
Destination |
mockErpDestination(String systemAlias)
Mocks an ERP destination by redirecting to an actual ERP system for destination name
ErpDestination.getDefaultName() . |
Destination |
mockErpDestination(String destinationName,
ErpSystem erpSystem)
Mocks an ERP destination by redirecting to an actual ERP system for the given destination name.
|
Destination |
mockErpDestination(String destinationName,
ErpSystem erpSystem,
Credentials credentials)
|
Destination |
mockErpDestination(String destinationName,
ErpSystem erpSystem,
Credentials credentials,
AuthenticationType authenticationType,
ProxyType proxyType,
ProxyConfiguration proxyConfiguration,
List<Header> additionalHeaders,
KeyStore trustStore,
Boolean isTrustingAllCertificates,
KeyStore keyStore,
String keyStorePassword,
Map<String,String> propertiesByName)
Mocks an ERP destination for the given parameters.
|
Destination |
mockErpDestination(String destinationName,
String systemAlias)
Mocks an ERP destination by redirecting to an actual ERP system for the given destination name.
|
org.mockserver.integration.ClientAndServer |
mockErpServer(SapClient sapClient)
Mocks an ERP destination with the given
SapClient and starts a mock server pointing all calls against the
ERP destination towards the mock server instance. |
org.mockserver.integration.ClientAndServer |
mockErpServer(SapClient sapClient,
String relativePath)
Mocks an ERP destination with the given
SapClient and starts a mock server pointing all calls against the
ERP destination towards the mock server instance. |
<T> T |
mockJndiLookup(Class<T> cls,
String name)
Mocks a JNDI lookup.
|
void |
mockJndiLookup(Object obj,
String name)
Mocks a JNDI lookup.
|
void |
mockKeyStore(String name,
SecretStore password,
KeyStore keyStore) |
void |
mockKeyStore(String name,
SecretStore password,
String keyStoreFileName,
String keyStoreType) |
void |
mockKeyStore(String name,
String password,
KeyStore keyStore) |
void |
mockKeyStore(String name,
String password,
String keyStoreFileName,
String keyStoreType) |
void |
mockSecretStore(String name,
SecretStore secretStore) |
void |
mockSecretStore(String name,
String password) |
org.mockserver.integration.ClientAndServer |
mockServer(String destinationName)
Mocks a destination and starts a mock server pointing all calls against the destination towards the mock server
instance.
|
org.mockserver.integration.ClientAndServer |
mockServer(String destinationName,
String relativePath)
Mocks a destination and starts a mock server pointing all calls against the destination towards the mock server
instance.
|
Tenant |
mockTenant(String tenantId) |
User |
mockUser(String userName) |
User |
mockUser(String userName,
Locale locale,
Collection<? extends Authorization> authorizations,
Map<String,? extends UserAttribute> attributes) |
void |
removeCredentials(String systemAlias)
Unregisters the
Credentials for the given alias. |
void |
removeTestSystem(String systemAlias)
Unregisters the
TestSystem for the given alias. |
void |
removeTestSystem(TestSystem<?> testSystem)
Unregisters the given
TestSystem . |
RequestContextExecutor |
requestContextExecutor() |
void |
setCurrentTenant(String tenantId)
Sets the current
Tenant . |
void |
setCurrentUser(String userName)
Sets the current
User . |
MockUtil |
withProxy(ProxyConfiguration proxyConfiguration) |
public MockUtil()
MockUtil
, invalidates caches, and resets Hystrix.
Note: To avoid potential side effects, only one instance of MockUtil should exist within a test class. It is
therefore recommended to maintain an instance of this class as a static member of the test class. Example usage:
private static final MockUtil mockUtil = new MockUtil();
@BeforeClass
public static void beforeClass()
{
mockUtil.mockDefaults();
}
public MockUtil withProxy(@Nullable ProxyConfiguration proxyConfiguration)
public void mockDefaults()
@BeforeClass
.public void clearDestinations()
Destination
s.public void clearTestSystems()
TestSystem
s.public void loadTestSystems(String resourceFileName)
loadTestSystems(File)
and loads test system configuration from the resource file with the
given name. The file is assumed to be in JSON format and expected to be located in src/test/resources/
.resourceFileName
- The name of the file to be loaded from the resources folder.public void loadTestSystems(@Nullable File file)
"alias"
field must be unique.
Note: Configuration specified as system properties always overrides configuration loaded from files.
Example (YAML):
---
systems:
- alias: "ANY_SYSTEM"
uri: "https://any-system.com"
erp:
default: "ERP_001"
systems:
- alias: "ERP_001"
uri: "https://my-erp.com"
systemId: "ERP"
sapClient: "001"
erpEdition: "cloud" # edition is optional
Example (JSON):
{
"systems": [
{
"alias": "ANY_SYSTEM",
"uri": "https://any-system.com"
}
],
"erp": {
"default": "ERP_001",
"systems": [
{
"alias": "ERP_001",
"systemId": "ERP",
"uri": "https://my-erp.com",
"sapClient": "001"
"erpEdition": "cloud"
}
]
}
}
file
- The file to be loaded.public void addTestSystem(TestSystem<?> testSystem)
TestSystem
to enable retrieval by its alias. Replaces TestSystem
s that may
already exist for the given alias.public void removeTestSystem(TestSystem<?> testSystem) throws IllegalArgumentException
TestSystem
.IllegalArgumentException
public void removeTestSystem(String systemAlias) throws IllegalArgumentException
TestSystem
for the given alias.IllegalArgumentException
public TestSystem<?> getTestSystem(String systemAlias) throws IllegalArgumentException
TestSystem
for the given alias. By default, TestSystem
s are loaded from the system
property PROPERTY_TEST_SYSTEMS
and the test resource file TEST_SYSTEMS_RESOURCE_FILE
(with
extension .json/.yml for JSON/YAML format). For registering additional TestSystem
s, use
loadTestSystems(String)
or addTestSystem(TestSystem)
.IllegalArgumentException
- If a TestSystem
with the given alias cannot be found.public void addDefaultErpSystem(ErpSystem erpSystem)
public ErpSystem getErpSystem()
public ErpSystem getErpSystem(String systemAlias) throws IllegalArgumentException
ErpSystem
for the given alias. Delegates to getTestSystem(String)
.IllegalArgumentException
- If a ErpSystem
with the given alias cannot be found.public Credentials getCredentials(TestSystem<?> testSystem) throws IllegalArgumentException
TestSystem
. Delegates to getCredentials(String)
.IllegalArgumentException
- If Credentials
cannot be found for the TestSystem
.public Credentials getCredentials(String systemAlias) throws IllegalArgumentException
Credentials
are loaded from the system property
PROPERTY_CREDENTIALS
and the test resource file CREDENTIALS_RESOURCE_FILE
. For registering
additional Credentials
, use loadCredentials(String)
or
addCredentials(String, Credentials)
.IllegalArgumentException
- If Credentials
for the given alias cannot be found.public void clearCredentials()
Credentials
.public void loadCredentials(String resourceFileName)
loadCredentials(File)
and loads credential configuration from the resource file with the
given name. The file is assumed to be in JSON format and expected to be located in /src/test/resources/
.resourceFileName
- The name of the file to be loaded from the resources folder.public void loadCredentials(@Nullable File file)
Note: Configuration specified as system properties always overrides configuration loaded from files.
Example (YAML):
---
credentials:
- alias: "ABC_001"
username: "(username)"
password: "(password)"
Example (JSON):
{
"credentials": [
{
"alias": "ABC_001",
"username": "(username)",
"password": "(password)"
}
]
}
file
- The file to be loaded.public void addCredentials(String systemAlias, Credentials credentials)
Credentials
to enable retrieval by its alias. Replaces Credentials
that may
already exist for the given alias.public void removeCredentials(String systemAlias)
Credentials
for the given alias.public void clearSecretStores()
SecretStore
s.public void clearKeyStores()
KeyStore
s.public void mockJndiLookup(Object obj, String name)
obj
- Object to be returned by the lookup.name
- Name that is used for the lookup.public <T> T mockJndiLookup(Class<T> cls, String name)
cls
- Class to be mocked.name
- Name that is used for the lookup.public CloudPlatform mockCurrentCloudPlatform()
public CloudPlatform mockCurrentCloudPlatform(String applicationName)
public void mockCurrentLocales()
public Tenant mockCurrentTenant()
public void setCurrentTenant(@Nullable String tenantId) throws IllegalArgumentException
IllegalArgumentException
- If no Tenant
has been mocked for the given identifier.public void clearTenants()
Tenant
s.public User mockUser(String userName, @Nullable Locale locale, @Nullable Collection<? extends Authorization> authorizations, @Nullable Map<String,? extends UserAttribute> attributes)
public User mockCurrentUser()
public User mockCurrentUser(String userName, @Nullable Locale locale, @Nullable Collection<? extends Authorization> authorizations, @Nullable Map<String,? extends UserAttribute> attributes)
public void setCurrentUser(@Nullable String userName) throws IllegalArgumentException
IllegalArgumentException
- If no User
has been mocked for the given name.public void clearUsers()
User
s.public AuditLog mockAuditLog()
public void mockSecretStore(String name, SecretStore secretStore)
public void mockKeyStore(String name, SecretStore password, KeyStore keyStore)
public void mockKeyStore(String name, SecretStore password, String keyStoreFileName, String keyStoreType)
public void mockKeyStore(String name, String password, String keyStoreFileName, String keyStoreType)
public Destination mockDestination(String name, String systemAlias)
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
public Destination mockDestination(String name, TestSystem<?> testSystem)
TestSystem
with the registered credentials.
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
public Destination mockDestination(String name, URI uri)
URI
with no authentication.
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
public Destination mockDestination(String name, URI uri, @Nullable Credentials credentials) throws IllegalArgumentException
URI
with the given Credentials
.
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
IllegalArgumentException
- If the given Credentials
are not supported.public Destination mockDestination(@NonNull String name, @NonNull URI uri, @Nullable AuthenticationType authenticationType, @Nullable ProxyType proxyType, @Nullable ProxyConfiguration proxyConfiguration, @Nullable List<Header> headers, @Nullable KeyStore trustStore, @Nullable Boolean isTrustingAllCertificates, @Nullable KeyStore keyStore, @Nullable String keyStorePassword, @Nullable Map<String,String> propertiesByName)
URI
.
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
public Destination mockErpDestination()
ErpDestination.getDefaultName()
.
The system that is used is either defined as property PROPERTY_ERP_ALIAS
or in the system property
PROPERTY_TEST_SYSTEMS
and the test resource file TEST_SYSTEMS_RESOURCE_FILE
(with extension
.json/.yml for JSON/YAML format).
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
public Destination mockErpDestination(String systemAlias)
ErpDestination.getDefaultName()
.
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
systemAlias
- The alias of the ErpSystem
that should be used.public Destination mockErpDestination(ErpSystem erpSystem)
ErpDestination.getDefaultName()
.
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
erpSystem
- The ErpSystem
that should be used.public Destination mockErpDestination(ErpSystem erpSystem, @Nullable Credentials credentials)
URI
and SapClient
for destination name
ErpDestination.getDefaultName()
.
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
public Destination mockErpDestination(String destinationName, String systemAlias)
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
destinationName
- The name of the destination to be mocked.systemAlias
- The alias of the ErpSystem
that should be used.public Destination mockErpDestination(String destinationName, ErpSystem erpSystem)
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
destinationName
- The name of the destination to be mocked.erpSystem
- The ErpSystem
that should be used.public Destination mockErpDestination(String destinationName, ErpSystem erpSystem, @Nullable Credentials credentials)
URI
and SapClient
.
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
public Destination mockErpDestination(String destinationName, ErpSystem erpSystem, @Nullable Credentials credentials, @Nullable AuthenticationType authenticationType, @Nullable ProxyType proxyType, @Nullable ProxyConfiguration proxyConfiguration, @Nullable List<Header> additionalHeaders, @Nullable KeyStore trustStore, @Nullable Boolean isTrustingAllCertificates, @Nullable KeyStore keyStore, @Nullable String keyStorePassword, @Nullable Map<String,String> propertiesByName) throws IllegalArgumentException
Note: This invalidates all caches to avoid stale entries within the
DestinationsRequestContextListener
.
destinationName
- The name of the mocked destination.erpSystem
- The ErpSystem
to use for mocking.credentials
- The Credentials
to be used for accessing the system. If null
, they are resolved from
configuration files or explicit registration via addCredentials(String, Credentials)
.authenticationType
- The AuthenticationType
to be used.proxyType
- The ProxyType
to be used. If not null
, overrides the ProxyType
inferred from
the ErpSystem
.proxyConfiguration
- The ProxyConfiguration
to be used.additionalHeaders
- Header
s that are added in addition to authentication-related headers.trustStore
- The trust store to be used.isTrustingAllCertificates
- Decides whether all certificates are trusted.keyStore
- The KeyStore
to be used.keyStorePassword
- The KeyStore
password to be used.propertiesByName
- Properties of the Destination
by their name. If a property with the respective name is
provided, it will override pre-configured properties such as
ErpConfigContext.DEFAULT_SAP_CLIENT_PROPERTY
and
ErpConfigContext.DEFAULT_LOCALE_PROPERTY
.IllegalArgumentException
- If the given Credentials
are not supported.public org.mockserver.integration.ClientAndServer mockServer(String destinationName, @Nullable String relativePath) throws IllegalArgumentException
IllegalArgumentException
public org.mockserver.integration.ClientAndServer mockServer(String destinationName) throws IllegalArgumentException
IllegalArgumentException
public org.mockserver.integration.ClientAndServer mockErpServer(SapClient sapClient, @Nullable String relativePath) throws IllegalArgumentException
SapClient
and starts a mock server pointing all calls against the
ERP destination towards the mock server instance.IllegalArgumentException
public org.mockserver.integration.ClientAndServer mockErpServer(SapClient sapClient) throws IllegalArgumentException
SapClient
and starts a mock server pointing all calls against the
ERP destination towards the mock server instance.IllegalArgumentException
public RequestContextExecutor requestContextExecutor()
RequestContextExecutor
that is pre-configured for testing.Copyright © 2017 SAP SE. All rights reserved.