Skip to content
This repository was archived by the owner on Feb 3, 2026. It is now read-only.

Commit 0aab103

Browse files
Mikhail Yakushinrultor
authored andcommitted
#233 - Added WallPostsRandomQuote tests.
1 parent 82854f7 commit 0aab103

File tree

7 files changed

+685
-12
lines changed

7 files changed

+685
-12
lines changed

pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<dependency>
3333
<groupId>com.github.driver733</groupId>
3434
<artifactId>vk-java-sdk</artifactId>
35-
<version>8f0787b2ea</version>
35+
<version>1741c9e583</version>
3636
</dependency>
3737
<dependency>
3838
<groupId>org.apache.commons</groupId>
@@ -163,7 +163,6 @@
163163
<excludes>
164164
<exclude>checkstyle:/src/test/resources/.*</exclude>
165165
<exclude>xml:/src/main/resources/.*</exclude>
166-
<exclude>duplicatefinder:.*</exclude>
167166
<exclude>cobertura:.*</exclude>
168167
<exclude>dependencies:.*</exclude>
169168

src/main/java/com/driver733/vkmusicuploader/media/MediaRandom.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public Path file() throws IOException {
8080
ThreadLocalRandom.current()
8181
.nextInt(
8282
0,
83-
files.size() + 1
83+
files.size()
8484
)
8585
);
8686
}

src/main/java/com/driver733/vkmusicuploader/wallpost/attachment/upload/TransportClientFake.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,9 @@ public ClientResponse post(
9494
public boolean isCached() {
9595
return true;
9696
}
97+
98+
@Override
99+
public boolean isTest() {
100+
return true;
101+
}
97102
}

src/main/java/com/driver733/vkmusicuploader/wallpost/wallpost/wallposts/WallPostsRandomQuote.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.driver733.vkmusicuploader.media.audio.MediaEmpty;
3030
import com.driver733.vkmusicuploader.media.photo.MediaPhotosBasic;
3131
import com.driver733.vkmusicuploader.post.UploadServers;
32+
import com.driver733.vkmusicuploader.wallpost.attachment.upload.TransportClientFake;
3233
import com.driver733.vkmusicuploader.wallpost.wallpost.WallPostWithRandomQuote;
3334
import com.jcabi.aspects.Immutable;
3435
import com.vk.api.sdk.client.VkApiClient;
@@ -38,6 +39,7 @@
3839
import java.nio.file.Path;
3940
import java.util.ArrayList;
4041
import java.util.List;
42+
import java.util.concurrent.TimeUnit;
4143

4244
/**
4345
* Creates {@link com.driver733.vkmusicuploader.wallpost.wallpost.WallPost}s
@@ -116,24 +118,30 @@ public final class WallPostsRandomQuote implements WallPosts {
116118
* Ctor.
117119
* @param client The {@link VkApiClient} for all requests.
118120
* @param actor UserActor on behalf of which all requests will be sent.
119-
* @param servers Upload servers that provide upload URLs
120-
* for attachmentsFields.
121121
* @param group Group ID.
122122
* @param count Number of posts to create.
123123
* @checkstyle ParameterNumberCheck (200 lines)
124124
*/
125125
public WallPostsRandomQuote(
126126
final VkApiClient client,
127127
final UserActor actor,
128-
final UploadServers servers,
129128
final int group,
130129
final int count
131130
) {
132131
this.client = client;
133132
this.actor = actor;
133+
this.servers = new UploadServers(
134+
new VkApiClient(
135+
new TransportClientFake()
136+
),
137+
new UserActor(
138+
0,
139+
""
140+
),
141+
0
142+
);
134143
this.photos = new MediaEmpty();
135144
this.audios = new MediaEmpty();
136-
this.servers = servers;
137145
this.group = group;
138146
this.count = count;
139147
}
@@ -167,6 +175,35 @@ public WallPostsRandomQuote(
167175
this.audios = new MediaEmpty();
168176
}
169177

178+
/**
179+
* Ctor.
180+
* @param client The {@link VkApiClient} for all requests.
181+
* @param actor UserActor on behalf of which all requests will be sent.
182+
* @param servers Upload servers that provide upload URLs
183+
* for attachmentsFields.
184+
* @param group Group ID.
185+
* @param count Number of posts to create.
186+
* @param audios Directory with audios.
187+
*/
188+
public WallPostsRandomQuote(
189+
final VkApiClient client,
190+
final UserActor actor,
191+
final UploadServers servers,
192+
final int group,
193+
final Path audios,
194+
final int count
195+
) {
196+
this.client = client;
197+
this.actor = actor;
198+
this.servers = servers;
199+
this.group = group;
200+
this.count = count;
201+
this.photos = new MediaEmpty();
202+
this.audios = new MediaAudiosBasic(
203+
audios
204+
);
205+
}
206+
170207
/**
171208
* Ctor.
172209
* @param client The {@link VkApiClient} for all requests.
@@ -297,7 +334,7 @@ private List<ExecuteBatchQuery> posts(
297334
final int cost,
298335
final WallPostWithRandomQuote wallpost
299336
) throws Exception {
300-
int left = this.count * cost;
337+
int left = this.count;
301338
final List<ExecuteBatchQuery> result = new ArrayList<>(
302339
(int) Math.ceil(
303340
(double) this.count / WallPostsRandomQuote.BATCH_MAX_REQ * cost
@@ -309,15 +346,17 @@ private List<ExecuteBatchQuery> posts(
309346
);
310347
for (
311348
int iter = 0;
312-
iter < WallPostsRandomQuote.BATCH_MAX_REQ;
349+
iter < this.count;
313350
iter += 1
314351
) {
315352
queries.add(
316353
wallpost.construct()
317354
);
355+
TimeUnit.SECONDS.sleep(2);
318356
}
319357
result.add(
320-
this.client.execute().batch(
358+
new ExecuteBatchQuery(
359+
this.client,
321360
this.actor,
322361
queries.toArray(
323362
new WallPostQuery[0]
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2018 Mikhail Yakushin
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.driver733.vkmusicuploader.wallpost.wallpost;
25+
26+
import com.google.gson.Gson;
27+
import com.google.gson.JsonObject;
28+
import com.jcabi.aspects.Immutable;
29+
import java.util.regex.Matcher;
30+
import java.util.regex.Pattern;
31+
32+
/**
33+
* Retrieves JSON from VK Execute API call.
34+
*
35+
* @since 0.1
36+
*/
37+
@Immutable
38+
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
39+
public final class JsonPattern {
40+
41+
/**
42+
* Exec String for VK Execute API call.
43+
*/
44+
private final String exec;
45+
46+
/**
47+
* Regex pattern.
48+
*/
49+
private final Pattern pattern;
50+
51+
/**
52+
* Ctor.
53+
* @param exec Exec String for VK Execute API call.
54+
* @param pattern Regex pattern.
55+
*/
56+
public JsonPattern(
57+
final String exec,
58+
final Pattern pattern
59+
) {
60+
this.exec = exec;
61+
this.pattern = pattern;
62+
}
63+
64+
/**
65+
* Creates a JSON object from the provided string.
66+
* @return Created {@link JsonObject}.
67+
* @throws Exception If the provided patter was not found.
68+
*/
69+
public JsonObject json() throws Exception {
70+
final Matcher matcher = this.pattern.matcher(
71+
this.exec
72+
);
73+
if (matcher.find()) {
74+
String match = matcher.group();
75+
match = match.substring(
76+
0,
77+
match.length() - 2
78+
);
79+
return new Gson()
80+
.fromJson(
81+
match,
82+
JsonObject.class
83+
);
84+
}
85+
throw new Exception(
86+
String.format(
87+
String.join(
88+
"No occurrence of the provided pattern \"%s\"",
89+
" found in the Json String \"%s\""
90+
),
91+
this.pattern.toString(),
92+
this.exec
93+
)
94+
);
95+
}
96+
97+
}

src/test/java/com/driver733/vkmusicuploader/wallpost/wallpost/WallPostPhotoAlbumTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
/**
4545
* Test for {@link WallPostPhotoAlbum}.
4646
*
47-
*
48-
*
4947
* @since 0.2
5048
* @checkstyle AnonInnerLengthCheck (500 lines)
5149
* @checkstyle JavadocMethodCheck (500 lines)

0 commit comments

Comments
 (0)