SocketManager.java

1
package pl.zankowski.iextrading4j.client.socket.manager;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import com.google.common.collect.Maps;
6
import io.socket.client.Socket;
7
import pl.zankowski.iextrading4j.client.mapper.IEXTradingMapperContextResolver;
8
9
import java.io.IOException;
10
import java.util.Arrays;
11
import java.util.Map;
12
import java.util.function.Consumer;
13
14
public class SocketManager {
15
16
    private final Map<SocketRequest, Socket> socketStore = Maps.newHashMap();
17
18
    private final SocketWrapper socketWrapper;
19
    private final String baseUrl;
20
    private final ObjectMapper objectMapper;
21
22
    public SocketManager(final SocketWrapper socketWrapper, final String baseUrl) {
23
        this.socketWrapper = socketWrapper;
24
        this.baseUrl = baseUrl;
25
        this.objectMapper = new IEXTradingMapperContextResolver().getContext(SocketManager.class);
26
    }
27
28
    public <T> void subscribe(final SocketRequest<T> request, final Consumer<T> consumer) {
29
        final String url = createURL(request);
30
31
        try {
32 1 1. subscribe : negated conditional → KILLED
            if (socketStore.containsKey(request)) {
33
                return;
34
            }
35
36
            final Socket socket = socketWrapper.socket(url, true).connect();
37
            socket.emit("subscribe", mapParam(request.getParam()))
38 1 1. lambda$subscribe$0 : removed call to pl/zankowski/iextrading4j/client/socket/manager/SocketManager::processResponse → KILLED
                    .on("message", args -> processResponse(args, request, consumer));
39
40
            socketStore.put(request, socket);
41
        } catch (Exception e) {
42
            throw new IllegalStateException(e.getMessage(), e);
43
        }
44
    }
45
46
    private String mapParam(final Object param) throws JsonProcessingException {
47 1 1. mapParam : negated conditional → KILLED
        if (param instanceof String) {
48 1 1. mapParam : replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::mapParam → NO_COVERAGE
            return String.valueOf(param);
49
        }
50 1 1. mapParam : replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::mapParam → KILLED
        return objectMapper.writeValueAsString(param);
51
    }
52
53
    public <T> void unsubscribe(final SocketRequest<T> request) {
54
        final Socket socket = socketStore.remove(request);
55 1 1. unsubscribe : negated conditional → KILLED
        if (socket == null) {
56
            return;
57
        }
58
59
        socket.disconnect();
60
    }
61
62
    private <T> void processResponse(final Object[] args, final SocketRequest<T> request, final Consumer<T> consumer) {
63 1 1. processResponse : removed call to java/util/stream/Stream::forEach → KILLED
        Arrays.stream(args).forEach(arg -> {
64
            try {
65 1 1. lambda$processResponse$1 : removed call to java/util/function/Consumer::accept → KILLED
                consumer.accept(mapObject(arg, request));
66
            } catch (IOException e) {
67
                throw new IllegalStateException(e.getMessage(), e);
68
            }
69
        });
70
    }
71
72
    private <T> T mapObject(final Object arg, final SocketRequest<T> request) throws IOException {
73 1 1. mapObject : replaced return value with null for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::mapObject → KILLED
        return objectMapper.readValue(String.valueOf(arg), request.getResponseType());
74
    }
75
76
    private <R> String createURL(final SocketRequest<R> restRequest) {
77 1 1. createURL : replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::createURL → KILLED
        return new StringBuilder()
78
                .append(getServicePath())
79
                .append(restRequest.getPath())
80
                .toString();
81
    }
82
83
    private String getServicePath() {
84 1 1. getServicePath : replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::getServicePath → KILLED
        return baseUrl;
85
    }
86
87
}

Mutations

32

1.1
Location : subscribe
Killed by : pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest]/[method:shouldThrowIllegalStateExceptionIfFailsOnConnection()]
negated conditional → KILLED

38

1.1
Location : lambda$subscribe$0
Killed by : pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest]/[method:shouldNotOverwriteSubscription()]
removed call to pl/zankowski/iextrading4j/client/socket/manager/SocketManager::processResponse → KILLED

47

1.1
Location : mapParam
Killed by : pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest]/[method:shouldConnectAndSubscribeAndProcessResponse()]
negated conditional → KILLED

48

1.1
Location : mapParam
Killed by : none
replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::mapParam → NO_COVERAGE

50

1.1
Location : mapParam
Killed by : pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest]/[method:shouldConnectAndSubscribeAndProcessResponse()]
replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::mapParam → KILLED

55

1.1
Location : unsubscribe
Killed by : pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest]/[method:shouldNotThrowExceptionWhenThereIsNoSubscription()]
negated conditional → KILLED

63

1.1
Location : processResponse
Killed by : pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest]/[method:shouldNotOverwriteSubscription()]
removed call to java/util/stream/Stream::forEach → KILLED

65

1.1
Location : lambda$processResponse$1
Killed by : pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest]/[method:shouldNotOverwriteSubscription()]
removed call to java/util/function/Consumer::accept → KILLED

73

1.1
Location : mapObject
Killed by : pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest]/[method:shouldNotOverwriteSubscription()]
replaced return value with null for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::mapObject → KILLED

77

1.1
Location : createURL
Killed by : pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest]/[method:verifyCreatedSocketPath()]
replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::createURL → KILLED

84

1.1
Location : getServicePath
Killed by : pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest.[engine:junit-jupiter]/[class:pl.zankowski.iextrading4j.client.socket.manager.SocketManagerTest]/[method:verifyCreatedSocketPath()]
replaced return value with "" for pl/zankowski/iextrading4j/client/socket/manager/SocketManager::getServicePath → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.1