/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.lofidewanto.demo.server.service.content;

import java.net.MalformedURLException;
import java.net.URI;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;

import com.lofidewanto.demo.shared.DemoGwtServiceEndpoint;

import static org.springframework.test.util.AssertionErrors.assertEquals;

@RunWith(MockitoJUnitRunner.class)
public class ConfluenceContentServiceImplTest {

	@InjectMocks
	private ConfluenceContentServiceImpl confluenceContentService = new ConfluenceContentServiceImpl();

	@Mock(answer = Answers.RETURNS_DEEP_STUBS)
	private RestTemplate restTemplate;

	private String confluenceUrl = "http://confluence:8080";

	private String confluencePageId = "1345151";

	@Before
	public void setUp() throws Exception {
		ReflectionTestUtils.setField(confluenceContentService, "confluenceUrl", confluenceUrl);
		ReflectionTestUtils.setField(confluenceContentService, "confluencePageId", confluencePageId);
	}

	@Test
	public void testReplacePageId() throws MalformedURLException {
		String confluenceAttachmentList = DemoGwtServiceEndpoint.CONFLUENCE_ATTACHMENT_LIST;
		String url = confluenceUrl.concat(confluenceAttachmentList);

		URI uri = confluenceContentService.replacePageId(url);

		final String expected = confluenceUrl + "/rest/api/content/" + confluencePageId
						+ "/child/attachment";
		assertEquals("URI", expected, uri.toURL().toString());
	}


}

@@ -913,16 +913,39 @@
 net.URI;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -1023,36 +1023,8 @@
 th;%0A
-import org.mockito.Answers;%0A
 impo
@@ -1051,16 +1051,45 @@
 tMocks;%0A
+import org.mockito.Matchers;%0A
 import o
@@ -1183,151 +1183,568 @@
 ork.
-test.util.ReflectionTestUtils;%0Aimport org.springframework.web.client.RestTemplate;%0A%0Aimport com.lofidewanto.demo.shared.DemoGwtServiceEndpoint;%0A
+http.HttpEntity;%0Aimport org.springframework.http.HttpMethod;%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;%0Aimport org.springframework.test.util.ReflectionTestUtils;%0Aimport org.springframework.web.client.RestTemplate;%0A%0Aimport com.lofidewanto.demo.server.domain.Attachment;%0Aimport com.lofidewanto.demo.server.domain.AttachmentImpl;%0Aimport com.lofidewanto.demo.shared.DemoGwtServiceEndpoint;%0A%0Aimport static org.mockito.Matchers.anyString;%0Aimport static org.mockito.Matchers.eq;%0Aimport static org.mockito.Mockito.when;
 %0Aimp
@@ -1979,91 +1979,17 @@
 vice
- = new ConfluenceContentServiceImpl();%0A%0A%09@Mock(answer = Answers.RETURNS_DEEP_STUBS)
+;%0A%0A%09@Mock
 %0A%09pr
@@ -2438,16 +2438,29 @@
 ption %7B%0A
+%09%09// Prepare%0A
 %09%09String
@@ -2602,16 +2602,25 @@
 List);%0A%0A
+%09%09// CUT%0A
 %09%09URI ur
@@ -2669,16 +2669,28 @@
 (url);%0A%0A
+%09%09// Verify%0A
 %09%09final 
@@ -2853,11 +2853,769 @@
 ));%0A%09%7D%0A%0A
+%09@Test%0A%09public void testGetAllAttachments() %7B%0A%09%09// Prepare%0A%09%09Attachment%5B%5D attachments = new Attachment%5B3%5D;%0A%09%09Attachment attach1 = new AttachmentImpl();%0A%09%09Attachment attach2 = new AttachmentImpl();%0A%09%09Attachment attach3 = new AttachmentImpl();%0A%09%09attachments%5B0%5D = attach1;%0A%09%09attachments%5B1%5D = attach2;%0A%09%09attachments%5B2%5D = attach3;%0A%0A%09%09ResponseEntity attachmentsWithResponseEntity =%0A%09%09%09%09new ResponseEntity%3CAttachment%5B%5D%3E(HttpStatus.ACCEPTED);%0A%0A%09%09when(restTemplate.exchange(anyString(), eq(HttpMethod.GET), Matchers.%3CHttpEntity%3C?%3E%3Eany(),%0A%09%09%09%09Matchers.%3CClass%3CAttachment%5B%5D%3E%3Eany(), Matchers.%3CObject%3EanyVararg())).%0A%09%09%09%09thenReturn(attachmentsWithResponseEntity);%0A%0A%09%09// CUT%0A%09%09List%3CAttachment%3E allAttachments = confluenceContentService.getAllAttachments();%0A%0A%09%09// Verify%0A%0A%09%7D%0A
 %0A%7D%0A

add test