2121import static org .junit .jupiter .api .Assertions .assertThrows ;
2222import static org .junit .jupiter .api .Assertions .assertTrue ;
2323
24+ import java .io .ByteArrayOutputStream ;
25+ import java .io .FileNotFoundException ;
2426import java .io .IOException ;
25- import java .nio . file . Files ;
26- import java .nio .file . Paths ;
27+ import java .io . InputStream ;
28+ import java .nio .charset . StandardCharsets ;
2729import java .util .List ;
2830import java .util .Map ;
2931
@@ -63,20 +65,18 @@ class JsoupFunPayParserTest {
6365 private MockWebServer mockWebServer ;
6466 private JsoupFunPayParser parser ;
6567
66- private static final String PARSE_LOT_HTML_RESPONSE_PATH =
67- "src/test/resources/html/client/parseLotResponse.html" ;
68+ private static final String PARSE_LOT_HTML_RESPONSE_PATH = "html/client/getLotResponse.html" ;
6869 private static final String PARSE_PROMO_GAMES_JSON_RESPONSE_PATH =
69- "src/test/resources/ json/client/parsePromoGamesResponse .json" ;
70+ "json/client/getPromoGamesResponse .json" ;
7071 private static final String PARSE_OFFER_HTML_RESPONSE_PATH =
71- "src/test/resources/html/client/parseOfferResponse.html" ;
72- private static final String PARSE_USER_HTML_RESPONSE_PATH =
73- "src/test/resources/html/client/parseUserResponse.html" ;
72+ "html/client/getOfferResponse.html" ;
73+ private static final String PARSE_USER_HTML_RESPONSE_PATH = "html/client/getUserResponse.html" ;
7474 private static final String PARSE_SELLER_REVIEWS_HTML_RESPONSE_PATH =
75- "src/test/resources/ html/client/parseSellerReviewsResponse .html" ;
75+ "html/client/getSellerReviewsResponse .html" ;
7676 private static final String PARSE_TRANSACTIONS_HTML_RESPONSE_PATH =
77- "src/test/resources/ html/client/parseTransactionsResponse .html" ;
77+ "html/client/getTransactionsResponse .html" ;
7878 private static final String PARSE_ORDER_HTML_RESPONSE_PATH =
79- "src/test/resources/ html/client/parseOrderResponse .html" ;
79+ "html/client/getOrderResponse .html" ;
8080 private static final String BASE_URL = "/" ;
8181
8282 @ BeforeEach
@@ -95,8 +95,7 @@ void tearDown() throws IOException {
9595
9696 @ Test
9797 void testParseLot () throws Exception {
98- String htmlContent =
99- new String (Files .readAllBytes (Paths .get (PARSE_LOT_HTML_RESPONSE_PATH )));
98+ String htmlContent = readResource (PARSE_LOT_HTML_RESPONSE_PATH );
10099 mockWebServer .enqueue (new MockResponse ().setBody (htmlContent ).setResponseCode (200 ));
101100
102101 long lotId = 149L ;
@@ -136,8 +135,7 @@ void testParseLotNotFound() throws Exception {
136135
137136 @ Test
138137 void testParsePromoGames () throws Exception {
139- String jsonContent =
140- new String (Files .readAllBytes (Paths .get (PARSE_PROMO_GAMES_JSON_RESPONSE_PATH )));
138+ String jsonContent = readResource (PARSE_PROMO_GAMES_JSON_RESPONSE_PATH );
141139 mockWebServer .enqueue (new MockResponse ().setBody (jsonContent ).setResponseCode (200 ));
142140
143141 String query = "dota" ;
@@ -158,8 +156,7 @@ void testParsePromoGames() throws Exception {
158156
159157 @ Test
160158 void testParseOffer () throws Exception {
161- String htmlContent =
162- new String (Files .readAllBytes (Paths .get (PARSE_OFFER_HTML_RESPONSE_PATH )));
159+ String htmlContent = readResource (PARSE_OFFER_HTML_RESPONSE_PATH );
163160 mockWebServer .enqueue (new MockResponse ().setBody (htmlContent ).setResponseCode (200 ));
164161
165162 long offerId = 33502824L ;
@@ -198,8 +195,7 @@ void testParseOfferNotFound() throws Exception {
198195
199196 @ Test
200197 void testParseUserWithoutGoldenKey () throws Exception {
201- String htmlContent =
202- new String (Files .readAllBytes (Paths .get (PARSE_USER_HTML_RESPONSE_PATH )));
198+ String htmlContent = readResource (PARSE_USER_HTML_RESPONSE_PATH );
203199 mockWebServer .enqueue (new MockResponse ().setBody (htmlContent ).setResponseCode (200 ));
204200
205201 long userId = 2L ;
@@ -232,8 +228,7 @@ void testParseUserWithoutGoldenKey() throws Exception {
232228
233229 @ Test
234230 void testParseUserWithGoldenKey () throws Exception {
235- String htmlContent =
236- new String (Files .readAllBytes (Paths .get (PARSE_USER_HTML_RESPONSE_PATH )));
231+ String htmlContent = readResource (PARSE_USER_HTML_RESPONSE_PATH );
237232 mockWebServer .enqueue (new MockResponse ().setBody (htmlContent ).setResponseCode (200 ));
238233
239234 String goldenKey = "some_golden_key" ;
@@ -267,8 +262,7 @@ void testParseUserNotFound() throws Exception {
267262
268263 @ Test
269264 void testParseSellerReviews () throws Exception {
270- String htmlContent =
271- new String (Files .readAllBytes (Paths .get (PARSE_SELLER_REVIEWS_HTML_RESPONSE_PATH )));
265+ String htmlContent = readResource (PARSE_SELLER_REVIEWS_HTML_RESPONSE_PATH );
272266 mockWebServer .enqueue (new MockResponse ().setBody (htmlContent ).setResponseCode (200 ));
273267
274268 long userId = 2L ;
@@ -305,8 +299,7 @@ void testParseSellerReviewsUserNotFound() throws Exception {
305299
306300 @ Test
307301 void testParseTransactions () throws Exception {
308- String htmlContent =
309- new String (Files .readAllBytes (Paths .get (PARSE_TRANSACTIONS_HTML_RESPONSE_PATH )));
302+ String htmlContent = readResource (PARSE_TRANSACTIONS_HTML_RESPONSE_PATH );
310303 mockWebServer .enqueue (new MockResponse ().setBody (htmlContent ).setResponseCode (200 ));
311304
312305 String goldenKey = "test-golden-key" ;
@@ -341,8 +334,7 @@ void testParseTransactionsUserNotFound() throws Exception {
341334
342335 @ Test
343336 void testParseOrder () throws Exception {
344- String htmlContent =
345- new String (Files .readAllBytes (Paths .get (PARSE_ORDER_HTML_RESPONSE_PATH )));
337+ String htmlContent = readResource (PARSE_ORDER_HTML_RESPONSE_PATH );
346338 mockWebServer .enqueue (new MockResponse ().setBody (htmlContent ).setResponseCode (200 ));
347339
348340 String goldenKey = "some_golden_key" ;
@@ -400,4 +392,20 @@ void testParseCsrfTokenAndPHPSESSID() throws Exception {
400392 assertEquals (csrfToken , result .getCsrfToken ());
401393 assertEquals (phpSessId , result .getPHPSESSID ());
402394 }
395+
396+ private static String readResource (String resourcePath ) throws IOException {
397+ try (InputStream is =
398+ JsoupFunPayParser .class .getClassLoader ().getResourceAsStream (resourcePath )) {
399+ if (is == null ) {
400+ throw new FileNotFoundException ("Resource not found: " + resourcePath );
401+ }
402+ ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
403+ byte [] data = new byte [4096 ];
404+ int nRead ;
405+ while ((nRead = is .read (data , 0 , data .length )) != -1 ) {
406+ buffer .write (data , 0 , nRead );
407+ }
408+ return new String (buffer .toByteArray (), StandardCharsets .UTF_8 );
409+ }
410+ }
403411}
0 commit comments