package com.modesteam.urutau.controller;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.powermock.api.easymock.PowerMock;

import br.com.caelum.vraptor.util.test.MockResult;
import br.com.caelum.vraptor.util.test.MockValidator;

import com.modesteam.urutau.UserManager;
import com.modesteam.urutau.builder.ArtifactBuilder;
import com.modesteam.urutau.builder.UserBuilder;
import com.modesteam.urutau.model.Artifact;
import com.modesteam.urutau.model.Epic;
import com.modesteam.urutau.model.Feature;
import com.modesteam.urutau.model.Storie;
import com.modesteam.urutau.model.UseCase;
import com.modesteam.urutau.model.User;
import com.modesteam.urutau.service.RequirementService;

public class RequirementsControllerTest {
	
	private MockResult mockResult;
	private UserManager mockUserSession;
	private MockValidator mockValidator;
	private RequirementService mockArtifactService;
	
	
	@Before
	public void setup() {
		// Mocks supported by vraptor
		mockResult = new MockResult();
		mockValidator = new MockValidator();
				
		// System's components
		mockArtifactService = EasyMock.createMock(RequirementService.class);
		mockUserSession = EasyMock.createMock(UserManager.class);
		
	}
	
	@Test
	public void createValidFeature() {
		ArtifactBuilder builderFeature = new ArtifactBuilder();
		
		Feature feature = builderFeature
					.id(1L)
					.title("exemple")
					.description("blabla")
					.buildFeature();

		mockService();
		mockAdd(feature);
		PowerMock.replayAll();
		RequirementController controllerMock = new RequirementController(mockResult,mockUserSession,
																			mockArtifactService,mockValidator);
		controllerMock.createFeature(feature);
	}
	
	@Test
	public void createValidEpic() {
		ArtifactBuilder builderEpic = new ArtifactBuilder();
		
		Epic epic = builderEpic
					.id(1L)
					.title("exemple")
					.description("blabla")
					.buildEpic();

		mockService();
		mockAdd(epic);
		PowerMock.replayAll();
		RequirementController controllerMock = new RequirementController(mockResult,mockUserSession,
																			mockArtifactService,mockValidator);
		controllerMock.createEpic(epic);
	}
	
	@Test
	public void createValidStorie() {
		ArtifactBuilder builderStorie = new ArtifactBuilder();
		
		Storie storie = builderStorie
					.id(1L)
					.title("exemple")
					.description("blabla")
					.buildStorie();

		mockService();
		mockAdd(storie);
		PowerMock.replayAll();
		RequirementController controllerMock = new RequirementController(mockResult,mockUserSession,
																			mockArtifactService,mockValidator);
		controllerMock.createUserStory(storie);
	}
	
	@Test
	public void createValidUseCase() {
		ArtifactBuilder builderUseCase = new ArtifactBuilder();
		
		UseCase useCase = builderUseCase
					.id(1L)
					.title("exemple")
					.description("blabla")
					.buildUseCase();

		mockService();
		mockAdd(useCase);
		PowerMock.replayAll();
		RequirementController controllerMock = new RequirementController(mockResult,mockUserSession,
																			mockArtifactService,mockValidator);
		controllerMock.createUseCase(useCase);
	}

	@Test
	public void successfullyDeletedRequirement() {
		ArtifactBuilder builderEpic = new ArtifactBuilder();
		
		Epic epic = builderEpic
					.id(null)
					.title("exemple")
					.description("blabla")
					.buildEpic();

		mockAdd(epic);
		mockRemove(null);
		
		
	}
	
	
	private void mockAdd(Artifact artifact){
		mockArtifactService.save(artifact);
		EasyMock.expectLastCall();
	}
	
	private void mockService() {
		User userMocked = EasyMock.createNiceMock(User.class);
		EasyMock.expect(mockUserSession.getUserLogged()).andReturn(userMocked);
	}
	
	private void mockRemove(Long id){
		mockArtifactService.excludeRequirement(id);
		EasyMock.expectLastCall();
	}
}
@@ -3168,27 +3168,20 @@
 yDeleted
-Requirement
+Epic
 () %7B%0A%09%09A
@@ -3270,20 +3270,18 @@
 %09%09%09%09.id(
-null
+1L
 )%0A%09%09%09%09%09.
@@ -3355,41 +3355,269 @@
 mock
-Add(epic);%0A%09%09mockRemove(null);%0A%09%09
+Service();%0A%09%09mockAdd(epic);%0A%09%09PowerMock.replayAll();%0A%09%09mockRemove(1L);%0A%09%09RequirementController controllerMock = new RequirementController(mockResult,mockUserSession,%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09mockArtifactService,mockValidator);%0A%09%09controllerMock.excludeRequirement(1L);
 %0A%09%09%0A

Fix test of DeleteRequirement