package com.gentics.cailun.core.data.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.gentics.cailun.core.data.model.auth.PermissionType;
import com.gentics.cailun.core.data.service.ObjectSchemaService;
import com.gentics.cailun.core.repository.ObjectSchemaRepository;
import com.gentics.cailun.demo.DemoDataProvider;
import com.gentics.cailun.demo.UserInfo;
import com.gentics.cailun.test.AbstractDBTest;

@Transactional
public class ObjectSchemaTest extends AbstractDBTest {

	@Autowired
	private ObjectSchemaService objectSchemaService;

	@Autowired
	private ObjectSchemaRepository objectSchemaRepository;

	@Before
	public void setup() throws Exception {
		setupData();
	}

	@Test
	public void testFindByName() {
		assertNotNull(objectSchemaService.findByName(DemoDataProvider.PROJECT_NAME, "content"));
		assertNull(objectSchemaService.findByName(DemoDataProvider.PROJECT_NAME, "content1235"));
	}

	@Test
	public void testFindAllSchemasForProject() {
		Iterable<ObjectSchema> result = objectSchemaService.findAll(DemoDataProvider.PROJECT_NAME);

		int nSchemas = 0;
		for (ObjectSchema schema : result) {
			assertNotNull(schema);
		}
		nSchemas++;
		assertEquals("There should be exactly one object schema for the given project with the name {" + DemoDataProvider.PROJECT_NAME + "}", 2,
				nSchemas);
	}

	@Test
	public void testDeleteByObject() {
		ObjectSchema schema = data().getContentSchema();
		objectSchemaService.delete(schema);
		assertNull(objectSchemaService.findOne(schema.getId()));
	}

	@Test
	public void testDeleteByName() {
		ObjectSchema schema = data().getContentSchema();
		Project project = data().getProject();
		objectSchemaService.deleteByName(project.getName(), schema.getName());
		assertNull(objectSchemaService.findOne(schema.getId()));
	}

	@Test
	public void testDeleteByNameWithInvalidProjectName() {
		ObjectSchema schema = data().getContentSchema();
		objectSchemaService.deleteByName("bogus", schema.getName());
		assertNotNull(objectSchemaService.findOne(schema.getId()));
	}

	@Test
	public void testDeleteByUUID() {
		ObjectSchema schema = data().getContentSchema();
		objectSchemaService.deleteByUUID(schema.getUuid());
		assertNull(objectSchemaService.findOne(schema.getId()));
	}

	@Test
	public void testDeleteWithNoPermission() {
		UserInfo info = data().getUserInfo();
		ObjectSchema schema = data().getContentSchema();
		roleService.revokePermission(info.getRole(), schema, PermissionType.DELETE);
		objectSchemaService.deleteByUUID(schema.getUuid());
		assertNotNull(objectSchemaService.findOne(schema.getId()));
	}

	@Test
	public void testObjectSchemaRootNode() {
		int nSchemasBefore = objectSchemaRepository.findRoot().getSchemas().size();

		ObjectSchema schema = new ObjectSchema("test1235");
		objectSchemaRepository.save(schema);

		int nSchemasAfter = objectSchemaRepository.findRoot().getSchemas().size();
		assertEquals(nSchemasBefore + 1, nSchemasAfter);
	}
}

@@ -221,16 +221,54 @@
 t.Test;%0A
+import org.neo4j.graphdb.Transaction;%0A
 import o
@@ -325,73 +325,8 @@
 red;
-%0Aimport org.springframework.transaction.annotation.Transactional;
 %0A%0Aim
@@ -659,23 +659,8 @@
 t;%0A%0A
-@Transactional%0A
 publ
@@ -1870,24 +1870,70 @@
 tProject();%0A
+%09%09try (Transaction tx = graphDb.beginTx()) %7B%0A%09
 %09%09objectSche
@@ -1985,32 +1985,53 @@
 ema.getName());%0A
+%09%09%09tx.success();%0A%09%09%7D%0A
 %09%09assertNull(obj
@@ -2183,32 +2183,79 @@
 ontentSchema();%0A
+%09%09try (Transaction tx = graphDb.beginTx()) %7B%0A%0A%09
 %09%09objectSchemaSe
@@ -2297,24 +2297,45 @@
 getName());%0A
+%09%09%09tx.success();%0A%09%09%7D%0A
 %09%09assertNotN
@@ -2472,32 +2472,79 @@
 ontentSchema();%0A
+%09%09try (Transaction tx = graphDb.beginTx()) %7B%0A%0A%09
 %09%09objectSchemaSe
@@ -2573,32 +2573,53 @@
 ema.getUuid());%0A
+%09%09%09tx.success();%0A%09%09%7D%0A
 %09%09assertNull(obj
@@ -2807,16 +2807,62 @@
 hema();%0A
+%09%09try (Transaction tx = graphDb.beginTx()) %7B%0A%09
 %09%09roleSe
@@ -2930,24 +2930,25 @@
 .DELETE);%0A%09%09
+%09
 objectSchema
@@ -2983,24 +2983,45 @@
 getUuid());%0A
+%09%09%09tx.success();%0A%09%09%7D%0A
 %09%09assertNotN
@@ -3256,16 +3256,62 @@
 1235%22);%0A
+%09%09try (Transaction tx = graphDb.beginTx()) %7B%0A%09
 %09%09object
@@ -3340,16 +3340,37 @@
 schema);
+%0A%09%09%09tx.success();%0A%09%09%7D
 %0A%0A%09%09int 

Fix some object schema tests