| 1 | package com.fwmotion.threescale.cms; | |
| 2 | ||
| 3 | import com.fwmotion.threescale.cms.exception.ThreescaleCmsNonApiException; | |
| 4 | import com.fwmotion.threescale.cms.support.ApiClientBuilder; | |
| 5 | import com.redhat.threescale.rest.cms.ApiClient; | |
| 6 | import com.redhat.threescale.rest.cms.auth.ApiKeyAuth; | |
| 7 | import com.redhat.threescale.rest.cms.auth.Authentication; | |
| 8 | import jakarta.annotation.Nonnull; | |
| 9 | import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | |
| 10 | import org.apache.hc.client5.http.impl.classic.HttpClients; | |
| 11 | import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; | |
| 12 | import org.apache.hc.client5.http.io.HttpClientConnectionManager; | |
| 13 | import org.apache.hc.client5.http.ssl.NoopHostnameVerifier; | |
| 14 | import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; | |
| 15 | import org.apache.hc.client5.http.ssl.TrustAllStrategy; | |
| 16 | import org.apache.hc.core5.ssl.SSLContexts; | |
| 17 | ||
| 18 | import java.io.IOException; | |
| 19 | import java.security.KeyManagementException; | |
| 20 | import java.security.KeyStoreException; | |
| 21 | import java.security.NoSuchAlgorithmException; | |
| 22 | ||
| 23 | public class ThreescaleCmsClientFactory implements AutoCloseable { | |
| 24 | ||
| 25 | private String baseUrl; | |
| 26 | private boolean useInsecureConnections; | |
| 27 | private String providerKey; | |
| 28 | private String accessToken; | |
| 29 | ||
| 30 | private HttpClientConnectionManager httpClientConnectionManager; | |
| 31 | private CloseableHttpClient httpClient; | |
| 32 | ||
| 33 | private void tryCloseHttpClient() { | |
| 34 |
2
1. tryCloseHttpClient : removed conditional - replaced equality check with true → NO_COVERAGE 2. tryCloseHttpClient : removed conditional - replaced equality check with false → NO_COVERAGE |
if (httpClient != null) { |
| 35 | try { | |
| 36 |
1
1. tryCloseHttpClient : removed call to org/apache/hc/client5/http/impl/classic/CloseableHttpClient::close → NO_COVERAGE |
httpClient.close(); |
| 37 | } catch (IOException e) { | |
| 38 | throw new ThreescaleCmsNonApiException("Couldn't close old HTTP client", e); | |
| 39 | } | |
| 40 | } | |
| 41 | httpClient = null; | |
| 42 | } | |
| 43 | ||
| 44 | private CloseableHttpClient getHttpClient() { | |
| 45 |
2
1. getHttpClient : removed conditional - replaced equality check with true → SURVIVED 2. getHttpClient : removed conditional - replaced equality check with false → KILLED |
if (httpClient == null) { |
| 46 |
2
1. getHttpClient : removed conditional - replaced equality check with false → SURVIVED 2. getHttpClient : removed conditional - replaced equality check with true → SURVIVED |
if (isUseInsecureConnections()) { |
| 47 | try { | |
| 48 | SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(SSLContexts.custom() | |
| 49 | .loadTrustMaterial(null, new TrustAllStrategy()) | |
| 50 | .build(), | |
| 51 | NoopHostnameVerifier.INSTANCE); | |
| 52 | ||
| 53 | httpClientConnectionManager = PoolingHttpClientConnectionManagerBuilder.create() | |
| 54 | .setSSLSocketFactory(connectionSocketFactory) | |
| 55 | .build(); | |
| 56 | httpClient = HttpClients.custom() | |
| 57 | .setConnectionManager(httpClientConnectionManager) | |
| 58 | .build(); | |
| 59 | } catch (NoSuchAlgorithmException | KeyManagementException | | |
| 60 | KeyStoreException e) { | |
| 61 | throw new ThreescaleCmsNonApiException("Unable to create insecure HttpClient", e); | |
| 62 | } | |
| 63 | } else { | |
| 64 | httpClient = HttpClients.createDefault(); | |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 |
1
1. getHttpClient : replaced return value with null for com/fwmotion/threescale/cms/ThreescaleCmsClientFactory::getHttpClient → KILLED |
return httpClient; |
| 69 | } | |
| 70 | ||
| 71 | private ApiClient newApiClient() { | |
| 72 | ApiClient apiClient = ApiClientBuilder.buildApiClient(getHttpClient()); | |
| 73 | ||
| 74 | apiClient.setBasePath(baseUrl); | |
| 75 | ||
| 76 |
2
1. newApiClient : removed conditional - replaced equality check with false → SURVIVED 2. newApiClient : removed conditional - replaced equality check with true → KILLED |
if (providerKey != null) { |
| 77 | Authentication providerKeyAuth = apiClient.getAuthentication("provider_key"); | |
| 78 |
1
1. newApiClient : removed call to com/redhat/threescale/rest/cms/auth/ApiKeyAuth::setApiKey → NO_COVERAGE |
((ApiKeyAuth) providerKeyAuth).setApiKey(apiClient.escapeString(providerKey)); |
| 79 |
2
1. newApiClient : removed conditional - replaced equality check with true → SURVIVED 2. newApiClient : removed conditional - replaced equality check with false → KILLED |
} else if (accessToken != null) { |
| 80 | Authentication accessTokenAuth = apiClient.getAuthentication("access_token"); | |
| 81 |
1
1. newApiClient : removed call to com/redhat/threescale/rest/cms/auth/ApiKeyAuth::setApiKey → KILLED |
((ApiKeyAuth) accessTokenAuth).setApiKey(apiClient.escapeString(accessToken)); |
| 82 | } else { | |
| 83 | throw new ThreescaleCmsNonApiException("Authentication not set for 3scale CMS client; must provide one of: providerKey, accessToken"); | |
| 84 | } | |
| 85 | ||
| 86 |
1
1. newApiClient : replaced return value with null for com/fwmotion/threescale/cms/ThreescaleCmsClientFactory::newApiClient → KILLED |
return apiClient; |
| 87 | } | |
| 88 | ||
| 89 | public String getBaseUrl() { | |
| 90 |
1
1. getBaseUrl : replaced return value with "" for com/fwmotion/threescale/cms/ThreescaleCmsClientFactory::getBaseUrl → NO_COVERAGE |
return baseUrl; |
| 91 | } | |
| 92 | ||
| 93 | public void setBaseUrl(String baseUrl) { | |
| 94 | this.baseUrl = baseUrl; | |
| 95 | } | |
| 96 | ||
| 97 | public boolean isUseInsecureConnections() { | |
| 98 |
2
1. isUseInsecureConnections : replaced boolean return with true for com/fwmotion/threescale/cms/ThreescaleCmsClientFactory::isUseInsecureConnections → SURVIVED 2. isUseInsecureConnections : replaced boolean return with false for com/fwmotion/threescale/cms/ThreescaleCmsClientFactory::isUseInsecureConnections → SURVIVED |
return useInsecureConnections; |
| 99 | } | |
| 100 | ||
| 101 | public void setUseInsecureConnections(boolean useInsecureConnections) { | |
| 102 |
2
1. setUseInsecureConnections : removed conditional - replaced equality check with false → NO_COVERAGE 2. setUseInsecureConnections : removed conditional - replaced equality check with true → NO_COVERAGE |
if (this.useInsecureConnections != useInsecureConnections) { |
| 103 |
1
1. setUseInsecureConnections : removed call to com/fwmotion/threescale/cms/ThreescaleCmsClientFactory::tryCloseHttpClient → NO_COVERAGE |
tryCloseHttpClient(); |
| 104 | } | |
| 105 | this.useInsecureConnections = useInsecureConnections; | |
| 106 | } | |
| 107 | ||
| 108 | public String getProviderKey() { | |
| 109 |
1
1. getProviderKey : replaced return value with "" for com/fwmotion/threescale/cms/ThreescaleCmsClientFactory::getProviderKey → NO_COVERAGE |
return providerKey; |
| 110 | } | |
| 111 | ||
| 112 | public void setProviderKey(String providerKey) { | |
| 113 | this.providerKey = providerKey; | |
| 114 | this.accessToken = null; | |
| 115 | } | |
| 116 | ||
| 117 | public String getAccessToken() { | |
| 118 |
1
1. getAccessToken : replaced return value with "" for com/fwmotion/threescale/cms/ThreescaleCmsClientFactory::getAccessToken → NO_COVERAGE |
return accessToken; |
| 119 | } | |
| 120 | ||
| 121 | public void setAccessToken(String accessToken) { | |
| 122 | this.accessToken = accessToken; | |
| 123 | this.providerKey = null; | |
| 124 | } | |
| 125 | ||
| 126 | @Nonnull | |
| 127 | public ThreescaleCmsClient getThreescaleCmsClient() { | |
| 128 |
1
1. getThreescaleCmsClient : replaced return value with null for com/fwmotion/threescale/cms/ThreescaleCmsClientFactory::getThreescaleCmsClient → KILLED |
return new ThreescaleCmsClientImpl(newApiClient()); |
| 129 | } | |
| 130 | ||
| 131 | @Override | |
| 132 | public void close() throws Exception { | |
| 133 |
2
1. close : removed conditional - replaced equality check with false → SURVIVED 2. close : removed conditional - replaced equality check with true → SURVIVED |
if (httpClientConnectionManager != null) { |
| 134 |
1
1. close : removed call to org/apache/hc/client5/http/io/HttpClientConnectionManager::close → NO_COVERAGE |
httpClientConnectionManager.close(); |
| 135 | } | |
| 136 |
2
1. close : removed conditional - replaced equality check with true → SURVIVED 2. close : removed conditional - replaced equality check with false → SURVIVED |
if (httpClient != null) { |
| 137 |
1
1. close : removed call to org/apache/hc/client5/http/impl/classic/CloseableHttpClient::close → SURVIVED |
httpClient.close(); |
| 138 | } | |
| 139 | } | |
| 140 | } | |
Mutations | ||
| 34 |
1.1 2.2 |
|
| 36 |
1.1 |
|
| 45 |
1.1 2.2 |
|
| 46 |
1.1 2.2 |
|
| 68 |
1.1 |
|
| 76 |
1.1 2.2 |
|
| 78 |
1.1 |
|
| 79 |
1.1 2.2 |
|
| 81 |
1.1 |
|
| 86 |
1.1 |
|
| 90 |
1.1 |
|
| 98 |
1.1 2.2 |
|
| 102 |
1.1 2.2 |
|
| 103 |
1.1 |
|
| 109 |
1.1 |
|
| 118 |
1.1 |
|
| 128 |
1.1 |
|
| 133 |
1.1 2.2 |
|
| 134 |
1.1 |
|
| 136 |
1.1 2.2 |
|
| 137 |
1.1 |