+/*
+ * Copyright (c) 2017-2020. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.snooker.request;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+/**
+ * Base Requester Test.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public abstract class AbstractRequesterTest extends Assert {
+
+ protected static final int CORRECT_ID = 1;
+ protected static final int INCORRECT_ID = 0;
+
+ protected static final String REQUEST_URL = "http://api.snooker.org/";
+
+ private static final String REQUEST_URL_METHOD_NAME = "getRequestUrl";
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ protected abstract Requester getRequester();
+
+ protected abstract RequestParams getCorrectRequestParams();
+
+ protected abstract RequestParams getIncorrectRequestParams();
+
+ protected abstract String getCorrectRequestUrl();
+
+ @Test
+ public void testRequestTypes() throws Exception {
+ for (RequestType requestType : RequestType.values()) {
+ assertNotNull(
+ requestType.requester()
+ );
+ }
+ }
+
+ @Test
+ public void testCorrectRequestById() throws Exception {
+ String requestUrl = invokeRequestUrl(CORRECT_ID);
+ assertEquals(
+ getCorrectRequestUrl(), requestUrl
+ );
+ }
+
+ @Test
+ public void testCorrectRequestByParams() throws Exception {
+ String requestUrl = invokeRequestUrl(
+ getCorrectRequestParams()
+ );
+ assertEquals(
+ getCorrectRequestUrl(), requestUrl
+ );
+ }
+
+ @Test
+ public void testIncorrectRequestById() throws Exception {
+ thrown.expect(InvocationTargetException.class);
+ invokeRequestUrl(INCORRECT_ID);
+ thrown = ExpectedException.none();
+ }
+
+ @Test
+ public void testIncorrectRequestByParams() throws Exception {
+ thrown.expect(InvocationTargetException.class);
+ invokeRequestUrl(
+ getIncorrectRequestParams()
+ );
+ thrown = ExpectedException.none();
+ }
+
+ private String invokeRequestUrl(int id) throws Exception {
+ Requester requester = getRequester();
+ Method requestMethod = requester.getClass().getDeclaredMethod(
+ REQUEST_URL_METHOD_NAME, int.class
+ );
+ assertEquals(
+ String.class, requestMethod.getReturnType()
+ );
+ requestMethod.setAccessible(true);
+ return (String) requestMethod.invoke(requester, id);
+ }
+
+ private String invokeRequestUrl(RequestParams params) throws Exception {
+ Requester requester = getRequester();
+ Method requestMethod = requester.getClass().getDeclaredMethod(
+ REQUEST_URL_METHOD_NAME, RequestParams.class
+ );
+ assertEquals(
+ String.class, requestMethod.getReturnType()
+ );
+ requestMethod.setAccessible(true);
+ return (String) requestMethod.invoke(requester, params);
+ }
+
+ private boolean isHostAvailable() {
+ try {
+ URL url = new URL(REQUEST_URL);
+ HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
+ httpConnection.setRequestMethod("HEAD");
+ return httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK;
+ } catch (Exception ignored) {
+ }
+ return false;
+ }
+
+}