[LIB-10] Tests for Requesters
[snooker-score-api.git] / src / test / java / org / hedgecode / snooker / request / AbstractRequesterTest.java
1 /*
2  * Copyright (c) 2017-2020. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.snooker.request;
18
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.net.HttpURLConnection;
22 import java.net.URL;
23
24 import org.junit.Assert;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28
29 /**
30  * Base Requester Test.
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public abstract class AbstractRequesterTest extends Assert {
35
36     protected static final int CORRECT_ID = 1;
37     protected static final int INCORRECT_ID = 0;
38
39     protected static final String REQUEST_URL = "http://api.snooker.org/";
40
41     private static final String REQUEST_URL_METHOD_NAME = "getRequestUrl";
42
43     @Rule
44     public ExpectedException thrown = ExpectedException.none();
45
46     protected abstract Requester getRequester();
47
48     protected abstract RequestParams getCorrectRequestParams();
49
50     protected abstract RequestParams getIncorrectRequestParams();
51
52     protected abstract String getCorrectRequestUrl();
53
54     @Test
55     public void testRequestTypes() throws Exception {
56         for (RequestType requestType : RequestType.values()) {
57             assertNotNull(
58                     requestType.requester()
59             );
60         }
61     }
62
63     @Test
64     public void testCorrectRequestById() throws Exception {
65         String requestUrl = invokeRequestUrl(CORRECT_ID);
66         assertEquals(
67                 getCorrectRequestUrl(), requestUrl
68         );
69     }
70
71     @Test
72     public void testCorrectRequestByParams() throws Exception {
73         String requestUrl = invokeRequestUrl(
74                 getCorrectRequestParams()
75         );
76         assertEquals(
77                 getCorrectRequestUrl(), requestUrl
78         );
79     }
80
81     @Test
82     public void testIncorrectRequestById() throws Exception {
83         thrown.expect(InvocationTargetException.class);
84         invokeRequestUrl(INCORRECT_ID);
85         thrown = ExpectedException.none();
86     }
87
88     @Test
89     public void testIncorrectRequestByParams() throws Exception {
90         thrown.expect(InvocationTargetException.class);
91         invokeRequestUrl(
92                 getIncorrectRequestParams()
93         );
94         thrown = ExpectedException.none();
95     }
96
97     private String invokeRequestUrl(int id) throws Exception {
98         Requester requester = getRequester();
99         Method requestMethod = requester.getClass().getDeclaredMethod(
100                 REQUEST_URL_METHOD_NAME, int.class
101         );
102         assertEquals(
103                 String.class, requestMethod.getReturnType()
104         );
105         requestMethod.setAccessible(true);
106         return (String) requestMethod.invoke(requester, id);
107     }
108
109     private String invokeRequestUrl(RequestParams params) throws Exception {
110         Requester requester = getRequester();
111         Method requestMethod = requester.getClass().getDeclaredMethod(
112                 REQUEST_URL_METHOD_NAME, RequestParams.class
113         );
114         assertEquals(
115                 String.class, requestMethod.getReturnType()
116         );
117         requestMethod.setAccessible(true);
118         return (String) requestMethod.invoke(requester, params);
119     }
120
121     private boolean isHostAvailable() {
122         try {
123             URL url = new URL(REQUEST_URL);
124             HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
125             httpConnection.setRequestMethod("HEAD");
126             return httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK;
127         } catch (Exception ignored) {
128         }
129         return false;
130     }
131
132 }