Skip to content

Commit 3e555a5

Browse files
committed
- add comments next to magic numbers
1 parent 36475c2 commit 3e555a5

File tree

8 files changed

+118
-13
lines changed

8 files changed

+118
-13
lines changed

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected void validate() {
9999

100100
//TODO 250 Magic number
101101
if (clientSettings.getPingInterval() < 250)
102-
throw new TikTokLiveException("Minimum allowed ping interval is 250 millseconds");
102+
throw new TikTokLiveException("Minimum allowed ping interval is 250 milliseconds");
103103

104104
var httpSettings = clientSettings.getHttpSettings();
105105
httpSettings.getParams().put("app_language", clientSettings.getClientLanguage());
@@ -176,12 +176,10 @@ public LiveClient build() {
176176
dependance.registerSingleton(LiveClient.class, TikTokLiveClient.class);
177177

178178
onCustomDependencies.forEach(action -> action.accept(dependance));
179-
180179
var container = dependance.build();
181180

182181
var listenerManager = container.find(ListenersManager.class);
183182
listeners.forEach(listenerManager::addListener);
184-
185183
return container.find(LiveClient.class);
186184
}
187185

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public LiveUserData.Response getLiveUserData(LiveUserData.Request request) {
136136
var url = TIKTOK_URL_WEB + "api-live/user/room";
137137
var result = httpFactory.client(url)
138138
.withParam("uniqueId", request.getUserName())
139-
.withParam("sourceType", "54")
139+
.withParam("sourceType", "54") //MAGIC NUMBER, WHAT 54 means?
140140
.build()
141141
.toJsonResponse();
142142

@@ -218,7 +218,7 @@ private ActionResult<HttpResponse<byte[]>> getStartingPayload(LiveConnectionData
218218
private ActionResult<HttpResponse<byte[]>> getByteResponse(String room_id) {
219219
HttpClientBuilder builder = httpFactory.client(TIKTOK_SIGN_API)
220220
.withParam("client", "ttlive-java")
221-
.withParam("uuc", "1")
221+
.withParam("uuc", "1") //MAGIC NUMBER!
222222
.withParam("room_id", room_id);
223223

224224
if (clientSettings.getApiKey() != null)

Client/src/main/java/io/github/jwdeveloper/tiktok/listener/TikTokListenersManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private EventConsumer createAction(Object listener, Method method, Class tiktokE
129129
var methodContainer = dependanceContainer.createChildContainer()
130130
.configure(configuration ->
131131
{
132-
//Modfying container, so it returns TikTokEvent object instance,
132+
//Modifying container, so it returns TikTokEvent object instance,
133133
//when TikTokEvent type is encountered in the methods parameters
134134
configuration.onInjection(injectionEvent ->
135135
{

Client/src/main/java/io/github/jwdeveloper/tiktok/mappers/TikTokGenericEventMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private record TypePair(Class<?> a, Class<?> b) {
5353

5454
private final Map<Class<?>, Method> methodCache;
5555
private final Map<TypePair, Constructor<?>> constructorCache;
56-
56+
private static final String PARSE_FIELD = "parseFrom";
5757
public TikTokGenericEventMapper() {
5858
this.methodCache = new HashMap<>();
5959
this.constructorCache = new HashMap<>();
@@ -75,7 +75,7 @@ public TikTokEvent mapToEvent(Class<?> inputClazz, Class<?> outputClass, byte[]
7575
public Method getParsingMethod(Class<?> input) throws RuntimeException {
7676
return methodCache.computeIfAbsent(input, aClass -> {
7777
try {
78-
return aClass.getDeclaredMethod("parseFrom", byte[].class);
78+
return aClass.getDeclaredMethod(PARSE_FIELD, byte[].class);
7979
} catch (NoSuchMethodException e) {
8080
throw new RuntimeException(e);
8181
}

Client/src/main/java/io/github/jwdeveloper/tiktok/mappers/TikTokLiveMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ public class TikTokLiveMapper implements LiveMapper {
3434
private final Map<String, TikTokLiveMapperModel> mappers;
3535
private final LiveMapperHelper mapperUtils;
3636
private final TikTokLiveMapperModel globalMapperModel;
37+
private static final String GLOBAL_MESSAGE = "GLOBAL MESSAGE";
3738

3839
public TikTokLiveMapper(LiveMapperHelper mapperUtils) {
3940
this.mappers = new HashMap<>();
4041
this.mapperUtils = mapperUtils;
41-
this.globalMapperModel = new TikTokLiveMapperModel("any message");
42+
this.globalMapperModel = new TikTokLiveMapperModel(GLOBAL_MESSAGE);
4243
}
4344

4445
@Override

Client/src/main/java/io/github/jwdeveloper/tiktok/mappers/handlers/TikTokGiftEventHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
public class TikTokGiftEventHandler {
3939
private final Map<Long, WebcastGiftMessage> giftsMessages;
4040
private final TikTokRoomInfo tikTokRoomInfo;
41-
4241
private final GiftsManager giftsManager;
4342

4443
public TikTokGiftEventHandler(GiftsManager giftsManager, TikTokRoomInfo tikTokRoomInfo) {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package io.github.jwdeveloper.tiktok;
2+
3+
import io.github.jwdeveloper.tiktok.data.requests.LiveData;
4+
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
5+
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
6+
import io.github.jwdeveloper.tiktok.http.LiveHttpClient;
7+
import io.github.jwdeveloper.tiktok.listener.ListenersManager;
8+
import io.github.jwdeveloper.tiktok.live.GiftsManager;
9+
import io.github.jwdeveloper.tiktok.live.LiveClient;
10+
import io.github.jwdeveloper.tiktok.live.LiveEventsHandler;
11+
import io.github.jwdeveloper.tiktok.live.LiveMessagesHandler;
12+
import io.github.jwdeveloper.tiktok.models.ConnectionState;
13+
import io.github.jwdeveloper.tiktok.websocket.LiveSocketClient;
14+
import org.junit.Assert;
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
import org.mockito.Mockito;
18+
19+
import java.util.logging.Logger;
20+
21+
public class TikTokLiveClientTests {
22+
23+
private LiveClient sut;
24+
LiveMessagesHandler messageHandler;
25+
GiftsManager giftsManager;
26+
TikTokRoomInfo tikTokLiveMeta;
27+
LiveHttpClient tiktokHttpClient;
28+
LiveSocketClient webSocketClient;
29+
LiveEventsHandler tikTokEventHandler;
30+
LiveClientSettings clientSettings;
31+
ListenersManager listenersManager;
32+
Logger logger;
33+
34+
@Before
35+
public void onBefore() {
36+
messageHandler = Mockito.mock(LiveMessagesHandler.class);
37+
giftsManager = Mockito.mock(GiftsManager.class);
38+
tikTokLiveMeta = Mockito.mock(TikTokRoomInfo.class);
39+
tiktokHttpClient = Mockito.mock(LiveHttpClient.class);
40+
webSocketClient = Mockito.mock(LiveSocketClient.class);
41+
tikTokEventHandler = Mockito.mock(LiveEventsHandler.class);
42+
clientSettings = Mockito.mock(LiveClientSettings.class);
43+
listenersManager = Mockito.mock(ListenersManager.class);
44+
logger = Mockito.mock(Logger.class);
45+
46+
sut = new TikTokLiveClient(messageHandler,
47+
giftsManager,
48+
tikTokLiveMeta,
49+
tiktokHttpClient,
50+
webSocketClient,
51+
tikTokEventHandler,
52+
clientSettings,
53+
listenersManager,
54+
logger);
55+
}
56+
57+
58+
@Test
59+
public void shouldThrownWhenAlreadyConnected() {
60+
tikTokLiveMeta.setConnectionState(ConnectionState.CONNECTED);
61+
Assert.assertThrows(TikTokLiveException.class, () ->
62+
{
63+
sut.connect();
64+
});
65+
}
66+
67+
@Test
68+
public void shouldThrowWhenUserIsOffline() {
69+
70+
var request = new LiveData.Request("X");
71+
var response = new LiveData.Response();
72+
response.setLiveStatus(LiveData.LiveStatus.HostOffline);
73+
Mockito.when(tiktokHttpClient.fetchLiveData(request)).thenReturn(response);
74+
Assert.assertThrows(TikTokLiveException.class, () ->
75+
{
76+
sut.connect();
77+
});
78+
}
79+
80+
@Test
81+
public void shouldThrowWhenUserNotFound()
82+
{
83+
var request = new LiveData.Request("X");
84+
var response = new LiveData.Response();
85+
response.setLiveStatus(LiveData.LiveStatus.HostNotFound);
86+
Mockito.when(tiktokHttpClient.fetchLiveData(request)).thenReturn(response);
87+
Assert.assertThrows(TikTokLiveException.class, () ->
88+
{
89+
sut.connect();
90+
});
91+
}
92+
93+
@Test
94+
public void shouldThrowWhenAgeRestricted()
95+
{
96+
Mockito.when(tiktokHttpClient.fetchLiveData(new LiveData.Request("X")))
97+
.thenReturn(new LiveData.Response());
98+
Assert.assertThrows(TikTokLiveException.class, () ->
99+
{
100+
sut.connect();
101+
});
102+
}
103+
104+
105+
@Test
106+
public void shouldConnect() {
107+
// sut.connect();
108+
}
109+
110+
}

tools-readme/src/main/resources/template.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ Do you prefer other programming languages?
8888

8989
{{if item is 2}}
9090

91-
my name is {{item.name}}
92-
9391
{{else}}
9492

9593
{{end}}
@@ -98,7 +96,6 @@ my name is {{item.name}}
9896
<br>
9997

10098
## Listeners
101-
10299
```java
103100
{{listener-content}}
104101
```

0 commit comments

Comments
 (0)