2 * Copyright (c) 2017-2020. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.snooker.request;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.net.HttpURLConnection;
24 import org.junit.Assert;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
30 * Base Requester Test.
32 * @author Dmitry Samoshin aka gotty
34 public abstract class AbstractRequesterTest extends Assert {
36 protected static final int CORRECT_ID = 1;
37 protected static final int INCORRECT_ID = 0;
39 protected static final String REQUEST_URL = "http://api.snooker.org/";
41 private static final String REQUEST_URL_METHOD_NAME = "getRequestUrl";
44 public ExpectedException thrown = ExpectedException.none();
46 protected abstract Requester getRequester();
48 protected abstract RequestParams getCorrectRequestParams();
50 protected abstract RequestParams getIncorrectRequestParams();
52 protected abstract String getCorrectRequestUrl();
55 public void testRequestTypes() throws Exception {
56 for (RequestType requestType : RequestType.values()) {
58 requestType.requester()
64 public void testCorrectRequestById() throws Exception {
65 String requestUrl = invokeRequestUrl(CORRECT_ID);
67 getCorrectRequestUrl(), requestUrl
72 public void testCorrectRequestByParams() throws Exception {
73 String requestUrl = invokeRequestUrl(
74 getCorrectRequestParams()
77 getCorrectRequestUrl(), requestUrl
82 public void testIncorrectRequestById() throws Exception {
83 thrown.expect(InvocationTargetException.class);
84 invokeRequestUrl(INCORRECT_ID);
85 thrown = ExpectedException.none();
89 public void testIncorrectRequestByParams() throws Exception {
90 thrown.expect(InvocationTargetException.class);
92 getIncorrectRequestParams()
94 thrown = ExpectedException.none();
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
103 String.class, requestMethod.getReturnType()
105 requestMethod.setAccessible(true);
106 return (String) requestMethod.invoke(requester, id);
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
115 String.class, requestMethod.getReturnType()
117 requestMethod.setAccessible(true);
118 return (String) requestMethod.invoke(requester, params);
121 private boolean isHostAvailable() {
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) {