package com.commafeed.backend.service;

import org.apache.http.HttpHeaders;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockserver.client.MockServerClient;
import org.mockserver.junit.MockServerRule;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.model.MediaType;

import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.feed.FeedQueues;
import com.commafeed.backend.model.Feed;

@RunWith(MockitoJUnitRunner.class)
public class PubSubServiceTest {

	private PubSubService underTest;

	@Rule
	private final MockServerRule mockServerRule = new MockServerRule(this, 22441);

	private MockServerClient mockServerClient;

	@Mock(answer = Answers.RETURNS_DEEP_STUBS)
	private CommaFeedConfiguration config;

	@Mock
	private FeedQueues queues;

	@Mock
	private Feed feed;

	@Before
	public void init() {
		underTest = new PubSubService(config, queues);

		// setup feed
		feed = Mockito.mock(Feed.class);
		Mockito.when(feed.getPushHub()).thenReturn("http://localhost:22441/hub");
		Mockito.when(feed.getPushTopic()).thenReturn("foo");

		// setup config
		Mockito.when(config.getApplicationSettings().getPublicUrl()).thenReturn("http://localhost:22441/hub");
	}

	@Test
	public void subscribe200() {
		// Arrange
		mockServerClient.when(HttpRequest.request().withMethod("POST")).respond(HttpResponse.response().withStatusCode(200));

		// Act
		underTest.subscribe(feed);

		// Assert
		mockServerClient.verify(HttpRequest.request()
				.withContentType(MediaType.APPLICATION_FORM_URLENCODED)
				.withHeader(HttpHeaders.USER_AGENT, "CommaFeed")
				.withMethod("POST")
				.withPath("/hub"));
		Mockito.verify(feed, Mockito.never()).setPushTopic(Mockito.anyString());
		Mockito.verifyZeroInteractions(queues);
	}

	@Test
	public void subscribe400WithPushpressError() {
		// Arrange
		mockServerClient.when(HttpRequest.request().withMethod("POST"))
				.respond(HttpResponse.response().withStatusCode(400).withBody(" is value is not allowed.  You may only subscribe to"));

		// Act
		underTest.subscribe(feed);

		// Assert
		Mockito.verify(feed).setPushTopic(Mockito.anyString());
		Mockito.verify(queues).giveBack(feed);
	}

	@Test
	public void subscribe400WithoutPushpressError() {
		// Arrange
		mockServerClient.when(HttpRequest.request().withMethod("POST")).respond(HttpResponse.response().withStatusCode(400));

		// Act
		underTest.subscribe(feed);

		// Assert
		Mockito.verify(feed, Mockito.never()).setPushTopic(Mockito.anyString());
		Mockito.verifyZeroInteractions(queues);
	}

}
@@ -743,45 +743,8 @@
 %0D%0A%0D%0A
-%09private PubSubService underTest;%0D%0A%0D%0A
 %09@Ru
@@ -749,22 +749,21 @@
 Rule%0D%0A%09p
-rivate
+ublic
  final M
@@ -829,16 +829,51 @@
 41);%0D%0A%0D%0A
+%09private PubSubService underTest;%0D%0A
 %09private

fix test