package com.addicks.sendash.admin.service.test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.addicks.sendash.admin.Application;
import com.addicks.sendash.admin.domain.properties.RepositoryProperties;
import com.addicks.sendash.admin.service.GitService;


@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Application.class)
@Profile("test")
public class GitServiceTest {

	@Autowired
	private GitService gitService;

	@Autowired
	private RepositoryProperties properties;

	
	private static final Logger log = LoggerFactory.getLogger(GitService.class);
	
	@Test
	public void shouldCreateLocalRepo() throws IOException {
		File gitDirectory = new File(properties.getLocalRepo());
		deleteDirectory(gitDirectory);
		//FileUtils.delete(gitDirectory);
		gitService.updateLocalRepository();

		assertNotNull(gitDirectory);
		assertTrue(gitDirectory.isDirectory());
		assertTrue(gitDirectory.list().length > 1);
	}

	@Test
	public void shouldUpdateExistingLocalRepo() {
		gitService.updateLocalRepository();

		File gitDirectory = new File(properties.getLocalRepo());
		
		assertNotNull(gitDirectory);
		assertTrue(gitDirectory.isDirectory());
		assertTrue(gitDirectory.list().length > 1);
	}
	
	private void deleteDirectory(File directorToDelete) {
		File[] files = directorToDelete.listFiles();
		if (files != null) {
			for (File f : files) {
				if (f.isDirectory()) {
					deleteDirectory(f);
				} else {
					f.delete();
				}
			}
		}
		directorToDelete.delete();
	}
	

}

@@ -409,24 +409,80 @@
 on.Profile;%0A
+import org.springframework.test.context.ActiveProfiles;%0A
 import org.s
@@ -842,17 +842,16 @@
 rvice;%0A%0A
-%0A
 @RunWith
@@ -954,16 +954,40 @@
 .class)%0A
+@ActiveProfiles(%22test%22)%0A
 @Profile
@@ -1026,17 +1026,18 @@
 Test %7B%0A%0A
-%09
+  
 @Autowir
@@ -1031,33 +1031,34 @@
 %7B%0A%0A  @Autowired%0A
-%09
+  
 private GitServi
@@ -1073,17 +1073,18 @@
 rvice;%0A%0A
-%09
+  
 @Autowir
@@ -1086,17 +1086,18 @@
 towired%0A
-%09
+  
 private 
@@ -1130,19 +1130,18 @@
 rties;%0A%0A
-%09%0A%09
+  
 private 
@@ -1213,18 +1213,19 @@
 s);%0A
-%09%0A%09
+%0A  
 @Test%0A
-%09
+  
 publ
@@ -1277,18 +1277,20 @@
 ption %7B%0A
-%09%09
+    
 File git
@@ -1338,18 +1338,20 @@
 epo());%0A
-%09%09
+    
 deleteDi
@@ -1377,12 +1377,15 @@
 y);%0A
-%09%09
+    
 //
+ 
 File
@@ -1412,18 +1412,20 @@
 ctory);%0A
-%09%09
+    
 gitServi
@@ -1453,18 +1453,20 @@
 ory();%0A%0A
-%09%09
+    
 assertNo
@@ -1478,34 +1478,36 @@
 (gitDirectory);%0A
-%09%09
+    
 assertTrue(gitDi
@@ -1522,34 +1522,36 @@
 isDirectory());%0A
-%09%09
+    
 assertTrue(gitDi
@@ -1582,20 +1582,23 @@
 1);%0A
-%09%7D%0A%0A%09
+  %7D%0A%0A  
 @Test%0A
-%09
+  
 publ
@@ -1639,18 +1639,20 @@
 epo() %7B%0A
-%09%09
+    
 gitServi
@@ -1680,18 +1680,20 @@
 ory();%0A%0A
-%09%09
+    
 File git
@@ -1745,13 +1745,13 @@
 ));%0A
-%09%09%0A%09%09
+%0A    
 asse
@@ -1775,18 +1775,20 @@
 ctory);%0A
-%09%09
+    
 assertTr
@@ -1819,18 +1819,20 @@
 ory());%0A
-%09%09
+    
 assertTr
@@ -1871,14 +1871,15 @@
 1);%0A
-%09%7D%0A%09%0A%09
+  %7D%0A%0A  
 priv
@@ -1928,18 +1928,20 @@
 lete) %7B%0A
-%09%09
+    
 File%5B%5D f
@@ -1977,18 +1977,20 @@
 iles();%0A
-%09%09
+    
 if (file
@@ -2002,19 +2002,22 @@
 null) %7B%0A
-%09%09%09
+      
 for (Fil
@@ -2031,20 +2031,24 @@
 iles) %7B%0A
-%09%09%09%09
+        
 if (f.is
@@ -2062,21 +2062,26 @@
 ry()) %7B%0A
-%09%09%09%09%09
+          
 deleteDi
@@ -2096,55 +2096,83 @@
 f);%0A
-%09%09%09%09%7D else %7B%0A%09%09%09%09%09f.delete();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09
+        %7D%0A        else %7B%0A          f.delete();%0A        %7D%0A      %7D%0A    %7D%0A    
 dire
@@ -2198,12 +2198,11 @@
 ();%0A
-%09%7D%0A%09
+  %7D
 %0A%0A%7D%0A

add active profiles annotation to test.