RestManager.java

1
package pl.zankowski.iextrading4j.client.rest.manager;
2
3
import com.google.common.collect.Maps;
4
import javax.ws.rs.client.Entity;
5
import javax.ws.rs.client.Invocation;
6
import javax.ws.rs.core.MediaType;
7
import javax.ws.rs.core.Response;
8
import pl.zankowski.iextrading4j.api.exception.IEXTradingException;
9
import pl.zankowski.iextrading4j.client.IEXCloudToken;
10
11
import java.util.Map;
12
13
import static java.util.stream.Collectors.joining;
14
15
public class RestManager {
16
17
    private static final String TOKEN_QUERY_PARAM = "token";
18
    private static final int SUCCESS = 200;
19
    private static final int WRITE_SUCCESS = 201;
20
21
    private final RestClient restClient;
22
23
    public RestManager(final RestClient restClient) {
24
        this.restClient = restClient;
25
    }
26
27
    public <R> RestResponse<R> executeRequest(final RestRequest<R> restRequest) {
28
        final String url = createURL(restRequest, restClient.getRestClientMetadata().getToken(),
29
                restClient.getRestClientMetadata().getUrl());
30
31
        final Invocation.Builder target = restClient.getClient().target(url).request(MediaType.APPLICATION_JSON);
32
        Response response = null;
33
34
        try {
35
            switch (restRequest.getMethodType()) {
36
                case GET:
37
                    response = target.get();
38
                    break;
39
                case POST:
40
                    final PostEntity requestEntity = restRequest.getRequestEntity();
41 1 1. executeRequest : removed call to pl/zankowski/iextrading4j/client/rest/manager/PostEntity::setToken → NO_COVERAGE
                    requestEntity.setToken(resolveToken(restRequest, restClient.getRestClientMetadata().getToken()));
42
                    response = target.post(Entity.entity(requestEntity, MediaType.APPLICATION_JSON));
43
                    break;
44
                default:
45
                    throw new IllegalStateException("Method Type not supported.");
46
            }
47
48 1 1. executeRequest : negated conditional → KILLED
            if (!isSuccessful(response)) {
49
                final String errorMessage = response.readEntity(String.class);
50
                throw new IEXTradingException(errorMessage, response.getStatus());
51
            }
52
53 1 1. executeRequest : replaced return value with null for pl/zankowski/iextrading4j/client/rest/manager/RestManager::executeRequest → KILLED
            return new RestResponseBuilder<R>()
54
                    .withMessage(response.getStatusInfo().getReasonPhrase())
55
                    .withResponse(response.readEntity(restRequest.getResponseType()))
56
                    .build();
57
        } finally {
58 1 1. executeRequest : negated conditional → KILLED
            if (response != null) {
59 1 1. executeRequest : removed call to javax/ws/rs/core/Response::close → KILLED
                response.close();
60
            }
61
        }
62
    }
63
64
    private boolean isSuccessful(final Response response) {
65 3 1. isSuccessful : negated conditional → KILLED
2. isSuccessful : negated conditional → KILLED
3. isSuccessful : replaced boolean return with true for pl/zankowski/iextrading4j/client/rest/manager/RestManager::isSuccessful → KILLED
        return response.getStatus() == SUCCESS || response.getStatus() == WRITE_SUCCESS;
66
    }
67
68
    private <R> String createURL(final RestRequest<R> restRequest, final IEXCloudToken token, final String url) {
69 1 1. createURL : replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::createURL → KILLED
        return new StringBuilder()
70
                .append(url)
71
                .append(createPath(restRequest.getPath(), restRequest.getPathParams()))
72
                .append(createQueryParameters(restRequest.getQueryParams(), resolveUrlToken(restRequest, token)))
73
                .toString();
74
    }
75
76
    private <R> String resolveUrlToken(final RestRequest<R> restRequest, final IEXCloudToken token) {
77 2 1. resolveUrlToken : negated conditional → SURVIVED
2. resolveUrlToken : replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::resolveUrlToken → KILLED
        return restRequest.getMethodType() != MethodType.GET ? null : resolveToken(restRequest, token);
78
    }
79
80
    private <R> String resolveToken(final RestRequest<R> restRequest, final IEXCloudToken token) {
81 2 1. resolveToken : negated conditional → KILLED
2. resolveToken : replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::resolveToken → KILLED
        return token == null
82
                ? null
83 1 1. resolveToken : negated conditional → NO_COVERAGE
                : restRequest.getUseSecretToken()
84
                ? token.getSecretToken()
85
                : token.getPublishableToken();
86
    }
87
88
89
    private String createPath(final String originalPath, final Map<String, String> pathParams) {
90
        String path = originalPath;
91
        for (final Map.Entry<String, String> entry : pathParams.entrySet()) {
92
            path = path.replaceFirst("\\{" + entry.getKey() + "\\}", entry.getValue());
93
        }
94
95 1 1. createPath : replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::createPath → KILLED
        return path;
96
    }
97
98
    private String createQueryParameters(final Map<String, String> queryParams, final String publishableToken) {
99 2 1. createQueryParameters : negated conditional → SURVIVED
2. createQueryParameters : negated conditional → KILLED
        if (queryParams.isEmpty() && publishableToken == null) {
100
            return "";
101
        }
102
103
        final Map<String, String> paramsCopy = Maps.newHashMap(queryParams);
104 1 1. createQueryParameters : negated conditional → KILLED
        if (publishableToken != null) {
105
            paramsCopy.put(TOKEN_QUERY_PARAM, publishableToken);
106
        }
107
108 1 1. createQueryParameters : replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::createQueryParameters → KILLED
        return paramsCopy.entrySet().stream()
109
                .map(this::createQueryParam)
110
                .collect(joining("&", "?", ""));
111
    }
112
113
    private String createQueryParam(final Map.Entry<String, String> queryParam) {
114 1 1. createQueryParam : replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::createQueryParam → KILLED
        return queryParam.getKey() + "=" + queryParam.getValue();
115
    }
116
117
}

Mutations

41

1.1
Location : executeRequest
Killed by : none
removed call to pl/zankowski/iextrading4j/client/rest/manager/PostEntity::setToken → NO_COVERAGE

48

1.1
Location : executeRequest
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldThrowAnExceptionForNotSuccessfulResponse()]
negated conditional → KILLED

53

1.1
Location : executeRequest
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldSuccessfullyExecuteGetRequest()]
replaced return value with null for pl/zankowski/iextrading4j/client/rest/manager/RestManager::executeRequest → KILLED

58

1.1
Location : executeRequest
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldThrowAnExceptionIfMethodTypeIsNotSupported()]
negated conditional → KILLED

59

1.1
Location : executeRequest
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldSuccessfullyExecuteGetRequest()]
removed call to javax/ws/rs/core/Response::close → KILLED

65

1.1
Location : isSuccessful
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldThrowAnExceptionForNotSuccessfulResponse()]
negated conditional → KILLED

2.2
Location : isSuccessful
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldThrowAnExceptionForNotSuccessfulResponse()]
negated conditional → KILLED

3.3
Location : isSuccessful
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldThrowAnExceptionForNotSuccessfulResponse()]
replaced boolean return with true for pl/zankowski/iextrading4j/client/rest/manager/RestManager::isSuccessful → KILLED

69

1.1
Location : createURL
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldSuccessfullyExecuteGetRequest()]
replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::createURL → KILLED

77

1.1
Location : resolveUrlToken
Killed by : none
negated conditional → SURVIVED

2.2
Location : resolveUrlToken
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldSuccessfullyExecuteGetRequest()]
replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::resolveUrlToken → KILLED

81

1.1
Location : resolveToken
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldThrowAnExceptionForNotSuccessfulResponse()]
negated conditional → KILLED

2.2
Location : resolveToken
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldSuccessfullyExecuteGetRequest()]
replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::resolveToken → KILLED

83

1.1
Location : resolveToken
Killed by : none
negated conditional → NO_COVERAGE

95

1.1
Location : createPath
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldSuccessfullyExecuteGetRequest()]
replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::createPath → KILLED

99

1.1
Location : createQueryParameters
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldSuccessfullyExecuteGetRequest()]
negated conditional → KILLED

2.2
Location : createQueryParameters
Killed by : none
negated conditional → SURVIVED

104

1.1
Location : createQueryParameters
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldSuccessfullyExecuteGetRequest()]
negated conditional → KILLED

108

1.1
Location : createQueryParameters
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldSuccessfullyExecuteGetRequest()]
replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::createQueryParameters → KILLED

114

1.1
Location : createQueryParam
Killed by : pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.rest.manager.RestManagerTest]/[method:shouldSuccessfullyExecuteGetRequest()]
replaced return value with "" for pl/zankowski/iextrading4j/client/rest/manager/RestManager::createQueryParam → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.1