/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is Web Questionnaires 2
 *
 * The Initial Owner of the Original Code is European Environment
 * Agency. Portions created by TripleDev are Copyright
 * (C) European Environment Agency.  All Rights Reserved.
 *
 * Contributor(s):
 *        Anton Dmitrijev
 */
package eionet.webq.web.controller;

import eionet.webq.dto.ProjectFile;
import eionet.webq.dto.UserFile;
import eionet.webq.service.ProjectFileService;
import eionet.webq.service.UserFileService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 */
public class PublicPageControllerTest {

    public static final int WEB_FORM_ID = 1;
    private PublicPageController publicPageController = new PublicPageController();
    private ProjectFileService projectFileService = Mockito.mock(ProjectFileService.class);
    private UserFileService userFileService = Mockito.mock(UserFileService.class);

    @Before
    public void setUp() throws Exception {
        publicPageController.projectFileService = projectFileService;
        publicPageController.userFileService = userFileService;
    }

    @Test
    public void savesNewUserFileToStorageAndRedirectsToWebForm() throws Exception {
        ProjectFile projectFile = new ProjectFile();
        projectFile.setNewXmlFileName("test_file.xml");
        projectFile.setXmlSchema("xml-schema");
        UserFile userToSave = saveFileAndGetParameterFromServiceCall(projectFile);

        assertThat(userToSave.getXmlSchema(), equalTo(projectFile.getXmlSchema()));
        assertThat(userToSave.getName(), equalTo(projectFile.getNewXmlFileName()));
    }

    @Test
    public void ifNewFileNameNotSetUseDefault() throws Exception {
        UserFile userFile = saveFileAndGetParameterFromServiceCall(new ProjectFile());
        assertThat(userFile.getName(), equalTo("new_form.xml"));
    }

    @Test
    public void writesWebFormContentForResponseWithRightHeaders() throws Exception {
        ProjectFile projectFile = new ProjectFile();
        byte[] testContent = "test-content".getBytes();
        projectFile.setFileContent(testContent);
        when(projectFileService.getById(WEB_FORM_ID)).thenReturn(projectFile);

        MockHttpServletResponse response = new MockHttpServletResponse();
        publicPageController.startWebFormWriteFormToResponse(WEB_FORM_ID, response);

        assertThat(response.getContentType(), equalTo("application/xhtml+html"));
        assertThat(response.getContentLength(), equalTo(testContent.length));
        assertThat(response.getContentAsByteArray(), equalTo(testContent));
    }

    private UserFile saveFileAndGetParameterFromServiceCall(ProjectFile projectFile) {
        when(projectFileService.getById(WEB_FORM_ID)).thenReturn(projectFile);

        publicPageController.startWebFormSaveFile(WEB_FORM_ID, new MockHttpServletRequest());

        ArgumentCaptor<UserFile> userFileArgumentCaptor = ArgumentCaptor.forClass(UserFile.class);
        verify(userFileService).save(userFileArgumentCaptor.capture());
        return userFileArgumentCaptor.getValue();
    }
}

@@ -1060,23 +1060,91 @@
 mockito.
-Mockito
+InjectMocks;%0Aimport org.mockito.Mock;%0Aimport org.mockito.MockitoAnnotations
 ;%0Aimport
@@ -1536,87 +1536,13 @@
     
-private PublicPageController publicPageController = new PublicPageController();
+@Mock
 %0A   
@@ -1591,50 +1591,19 @@
 vice
- = Mockito.mock(ProjectFileService.class);
+;%0A    @Mock
 %0A   
@@ -1646,236 +1646,209 @@
 vice
- = Mockito.mock(UserFileService.class);%0A%0A    @Before%0A    public void setUp() throws Exception %7B%0A        publicPageController.projectFileService = projectFileService;%0A        publicPageController.userFileService = userFileService
+;%0A    @InjectMocks%0A    private PublicPageController publicPageController = new PublicPageController();%0A%0A    @Before%0A    public void setUp() throws Exception %7B%0A        MockitoAnnotations.initMocks(this)
 ;%0A  

use mockito annotations.