package com.goree.api.service;

import com.goree.api.domain.Group;
import com.goree.api.mapper.MemberMapper;
import com.goree.api.util.TestWithDBUnit;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;

import java.io.*;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

public class GroupServiceUpdateImageTest extends TestWithDBUnit {
	@Autowired
	private GroupService groupService;

	@Autowired
	private MemberMapper memberMapper;

    @Value("${file.upload.path}")
	private String fileUploadDir;

	private final String testImagePath = "src/test/resources/static/Image_upload_test.jpg";
	private File testImageFile;

	private final String multiFileName = "test.jpg";
	private MockMultipartFile multipartFile;

	@Override
	public String getDatasetFilePath() {
		return "src/test/resources/testdataset/group_test_setup.xml";
	}

	@Override
    @Before
	public void setUp() {
		super.setUp();

		testImageFile = new File(testImagePath);

		try (InputStream testImageStream = new FileInputStream(testImageFile)) {
			multipartFile = new MockMultipartFile(multiFileName, multiFileName, null, testImageStream);
		} catch (FileNotFoundException e) {
			throw new RuntimeException("test image ("+ testImagePath +") is not found.", e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Test
	public void updateImage_normal() throws IOException {
		// when
        updateAndAssert();
	}

    @Test
	public void updateImage_destDirNotExists() throws IOException {
		// remove destination directory
		File fileUploadDirObj = new File(fileUploadDir);
		if (fileUploadDirObj.exists() && fileUploadDirObj.isDirectory())
			FileUtils.forceDelete(fileUploadDirObj);

		// when
        updateAndAssert();
	}

    private void updateAndAssert() throws IOException {
        Group resultGroup = groupService.updateImage(multipartFile, 1L);
        assertThat(resultGroup.getImagePath(), is(not(nullValue())));

        String resultImagePath = FilenameUtils.concat(fileUploadDir, resultGroup.getImagePath());
        File resultFile = new File(resultImagePath);

        // then
        assertThat(resultGroup.getImagePath(), is(endsWith(multiFileName)));
        assertTrue(resultFile.exists());
        assertTrue(resultFile.isFile());
        assertTrue(FileUtils.contentEquals(resultFile, testImageFile));
    }
}

@@ -64,50 +64,8 @@
 up;%0A
-import com.goree.api.mapper.MemberMapper;%0A
 impo
@@ -178,32 +178,56 @@
 .FilenameUtils;%0A
+import org.junit.After;%0A
 import org.junit
@@ -652,57 +652,8 @@
 e;%0A%0A
-%09@Autowired%0A%09private MemberMapper memberMapper;%0A%0A
     
@@ -925,18 +925,56 @@
 rtFile;%0A
-%0A%09
+    private File fileUploadDirObj;%0A%0A    
 @Overrid
@@ -1546,16 +1546,69 @@
 (e);%0A%09%09%7D
+%0A%0A        fileUploadDirObj = new File(fileUploadDir);
 %0A%09%7D%0A%0A%09@T
@@ -1662,24 +1662,165 @@
 Exception %7B%0A
+        // ensure destination directory exists.%0A        if (!fileUploadDirObj.exists())%0A            FileUtils.forceMkdir(fileUploadDirObj);%0A%0A
 %09%09// when%0A  
@@ -1808,32 +1808,37 @@
 Obj);%0A%0A%09%09// when
+ then
 %0A        updateA
@@ -1966,59 +1966,8 @@
 ory%0A
-%09%09File fileUploadDirObj = new File(fileUploadDir);%0A
 %09%09if
@@ -2083,16 +2083,21 @@
 %09// when
+ then
 %0A       
@@ -2168,32 +2168,48 @@
 s IOException %7B%0A
+        // when%0A
         Group re
@@ -2740,11 +2740,253 @@
 );%0A    %7D
+%0A%0A    @Override%0A    @After%0A    public void tearDown() %7B%0A        super.tearDown();%0A%0A        try %7B%0A            FileUtils.forceDelete(fileUploadDirObj);%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D
 %0A%7D%0A

refactor test code