com.intershop.oms.test.servicehandler.RESTServiceHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of iom-test-framework Show documentation
Show all versions of iom-test-framework Show documentation
An "SDK"-style library improving test automation for Intershop Order Management.
package com.intershop.oms.test.servicehandler;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.intershop.oms.rest.shared.ApiClient;
import com.intershop.oms.rest.shared.auth.HttpBasicAuth;
import com.intershop.oms.rest.shared.auth.HttpBearerAuth;
import com.intershop.oms.test.configuration.ConfigBuilder;
import com.intershop.oms.test.configuration.ServiceConfiguration;
import com.intershop.oms.test.configuration.ServiceEndpoint;
import com.intershop.oms.test.util.AuthTokenUtil;
public abstract class RESTServiceHandler implements OMSServiceHandler
{
private static final Logger log = LoggerFactory.getLogger(RESTServiceHandler.class);
private static final String BEARER_AUTH = "bearerAuth";
private static final String BASIC_AUTH = "basicAuth";
protected String defaultProtocol = null;
protected String defaultHost = null;
protected Integer defaultPort = null;
protected ApiClient apiClient;
protected String defaultBasePath = null;
protected static final String HTTP_HEADER_LOCATION = "Location";
/**
* @param logger
* NOTE: this is not being used yet - the intention is to
* pass this on to the ApiClient for request/response logging -
* still to be implemented
*/
protected RESTServiceHandler(ServiceConfiguration serviceConfig, String basePath, Logger logger)
{
Optional defaultEndpoint = ConfigBuilder.getDefault().defaultEndpoint();
ServiceConfiguration credentialsConfig = serviceConfig.bearerAuthToken().isPresent()
|| serviceConfig.username().isPresent() ? serviceConfig
: defaultEndpoint.orElseThrow(() -> new RuntimeException(
"no credentials are set, default-endpoint not configured"));
if (credentialsConfig.bearerAuthToken().isPresent())
{
this.apiClient = getBearerAuthApiClient(credentialsConfig.bearerAuthToken().get());
}
else
{
this.apiClient = getBasicAuthApiClient(credentialsConfig.username().get(),
credentialsConfig.password().get());
}
ServiceEndpoint endpoint = serviceConfig.serviceEndpoint().orElseGet(
() -> defaultEndpoint.isPresent() && defaultEndpoint.get().serviceEndpoint().isPresent()
? defaultEndpoint.get().serviceEndpoint().get()
: null);
if (endpoint == null)
{
throw new RuntimeException("neither service config nor default-endpoint are configured");
}
defaultProtocol = endpoint.protocol().orElse("http");
defaultHost = endpoint.host();
defaultPort = endpoint.port().orElse(80);
defaultBasePath = basePath;
apiClient.setBasePath(defaultProtocol + "://" + defaultHost + ":" + defaultPort + defaultBasePath);
log.debug("RESTServiceHandler instantiated for {} => {}", basePath, serviceConfig.getVersion());
}
@Override
public ApiClient getApiClient()
{
return apiClient;
}
@Override
public void setBasicAuth(String user, String password)
{
HttpBasicAuth basicAuth = (HttpBasicAuth)apiClient.getAuthentication(BASIC_AUTH);
basicAuth.setUsername(user);
basicAuth.setPassword(password);
HttpBearerAuth tokenAuth = (HttpBearerAuth)apiClient.getAuthentication(BEARER_AUTH);
tokenAuth.setBearerToken(null);
}
@Override
public void setTokenAuth(String token)
{
HttpBasicAuth basicAuth = (HttpBasicAuth)apiClient.getAuthentication(BASIC_AUTH);
basicAuth.setUsername(null);
basicAuth.setPassword(null);
HttpBearerAuth tokenAuth = (HttpBearerAuth)apiClient.getAuthentication(BEARER_AUTH);
tokenAuth.setBearerToken(token);
}
@Override
public void setTokenAuthForUser(String username)
{
setTokenAuth(AuthTokenUtil.getDefaultJWTForUser(username));
}
protected abstract Collection