/* * Copyright 2012 GitHub Inc. * * 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 com.github.mobile.gauges.core; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.doReturn; import com.github.kevinsawicki.http.HttpRequest; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; /** * Unit tests of {@link GaugesService} */ @RunWith(MockitoJUnitRunner.class) public class GaugesServiceTest { /** * Create reader for string * * @param value * @return input stream reader * @throws IOException */ private static BufferedReader createReader(String value) throws IOException { return new BufferedReader(new InputStreamReader(new ByteArrayInputStream( value.getBytes(HttpRequest.CHARSET_UTF8)))); } @Mock private HttpRequest request; private GaugesService service; /** * Set up default mocks * * @throws IOException */ @Before public void before() throws IOException { service = new GaugesService("key") { protected HttpRequest execute(HttpRequest request) throws IOException { return GaugesServiceTest.this.request; } }; doReturn(true).when(request).ok(); } /** * Verify getting gauges with an empty response * * @throws IOException */ @Test public void getGaugesEmptyResponse() throws IOException { doReturn(createReader("")).when(request).bufferedReader(); List<Gauge> gauges = service.getGauges(); assertNotNull(gauges); assertTrue(gauges.isEmpty()); } /** * Verify getting page content with an empty response * * @throws IOException */ @Test public void getContentEmptyResponse() throws IOException { doReturn(createReader("")).when(request).bufferedReader(); List<PageContent> content = service.getContent("id"); assertNotNull(content); assertTrue(content.isEmpty()); } /** * Verify getting referrers with an empty response * * @throws IOException */ @Test public void getReferrersEmptyResponse() throws IOException { doReturn(createReader("")).when(request).bufferedReader(); List<Referrer> referrers = service.getReferrers("id"); assertNotNull(referrers); assertTrue(referrers.isEmpty()); } }