/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2013 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.plugin.deskmanager.test.command.manager;

import org.geomajas.command.CommandDispatcher;
import org.geomajas.command.CommandResponse;
import org.geomajas.plugin.deskmanager.command.manager.dto.DeleteGeodeskRequest;
import org.geomajas.plugin.deskmanager.command.security.dto.RetrieveRolesRequest;
import org.geomajas.plugin.deskmanager.domain.Geodesk;
import org.geomajas.plugin.deskmanager.security.DeskmanagerSecurityService;
import org.geomajas.plugin.deskmanager.security.ProfileService;
import org.geomajas.plugin.deskmanager.service.common.GeodeskService;
import org.geomajas.security.GeomajasSecurityException;
import org.geomajas.security.SecurityManager;
import org.geomajas.security.SecurityService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Oliver May
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/org/geomajas/spring/geomajasContext.xml",
		"/org/geomajas/plugin/deskmanager/spring/**/*.xml", "/applicationContext.xml" })
public class DeleteBlueprintCommandTest {

	@Autowired
	private SecurityService securityService;

	@Autowired
	private ProfileService profileService;

	@Autowired
	private SecurityManager securityManager;

	@Autowired
	private CommandDispatcher dispatcher;

	@Autowired
	private GeodeskService geodeskService;
	
	private String userToken;

	private String guestToken;

	@Before
	public void setup() throws Exception {
		// First profile in list is admin
		userToken = ((DeskmanagerSecurityService) securityService).registerRole(RetrieveRolesRequest.MANAGER_ID,
				profileService.getProfiles().get(0));
		guestToken = ((DeskmanagerSecurityService) securityService).registerRole(RetrieveRolesRequest.MANAGER_ID,
				DeskmanagerSecurityService.createGuestProfile());

		// Log in
		securityManager.createSecurityContext(userToken);

	}

	@Test
	@Transactional
	public void testDeleteGeodesk() throws GeomajasSecurityException {

		Geodesk gd = geodeskService.getGeodesks().get(0);
		int count = geodeskService.getGeodesks().size();
		
		DeleteGeodeskRequest request = new DeleteGeodeskRequest();
		request.setGeodeskId(gd.getId());

		CommandResponse response = dispatcher.execute(DeleteGeodeskRequest.COMMAND, request, userToken, "en");
		Assert.assertTrue(response.getErrors().isEmpty());
		Assert.assertTrue(geodeskService.getGeodesks().size() == count -1);
	}

	/**
	 * Test security.
	 */
	@Test
	public void testNotAllowed() {
		CommandResponse response = dispatcher.execute(DeleteGeodeskRequest.COMMAND, new DeleteGeodeskRequest(),
				guestToken, "en");

		Assert.assertFalse(response.getExceptions().isEmpty());
		Assert.assertEquals(response.getExceptions().get(0).getClassName(), GeomajasSecurityException.class.getName());
	}
}

@@ -628,31 +628,33 @@
 r.dto.Delete
-Geodesk
+Blueprint
 Request;%0Aimp
@@ -778,23 +778,25 @@
 .domain.
-Geodesk
+Blueprint
 ;%0Aimport
@@ -983,23 +983,25 @@
 .common.
-Geodesk
+Blueprint
 Service;
@@ -2038,30 +2038,34 @@
 ate 
-GeodeskService geodesk
+BlueprintService blueprint
 Serv
@@ -2640,23 +2640,25 @@
 stDelete
-Geodesk
+Blueprint
 () throw
@@ -2694,28 +2694,32 @@
 %0A%0A%09%09
-Geodesk gd = geodesk
+Blueprint gd = blueprint
 Serv
@@ -2721,31 +2721,33 @@
 tService.get
-Geodesk
+Blueprint
 s().get(0);%0A
@@ -2760,23 +2760,25 @@
 count = 
-geodesk
+blueprint
 Service.
@@ -2772,39 +2772,41 @@
 printService.get
-Geodesk
+Blueprint
 s().size();%0A%09%09%0A%09
@@ -2808,31 +2808,33 @@
 %0A%09%09%0A%09%09Delete
-Geodesk
+Blueprint
 Request requ
@@ -2841,39 +2841,41 @@
 est = new Delete
-Geodesk
+Blueprint
 Request();%0A%09%09req
@@ -2882,23 +2882,25 @@
 uest.set
-Geodesk
+Blueprint
 Id(gd.ge
@@ -2954,39 +2954,41 @@
 r.execute(Delete
-Geodesk
+Blueprint
 Request.COMMAND,
@@ -3092,15 +3092,17 @@
 rue(
-geodesk
+blueprint
 Serv
@@ -3108,23 +3108,25 @@
 vice.get
-Geodesk
+Blueprint
 s().size
@@ -3264,31 +3264,33 @@
 ecute(Delete
-Geodesk
+Blueprint
 Request.COMM
@@ -3308,15 +3308,17 @@
 lete
-Geodesk
+Blueprint
 Requ

move gwt code to gwt module