package org.orienteer.devutils;

import org.orienteer.core.OrienteerWebApplication;
import org.orienteer.core.module.IOrienteerModule;
import org.orienteer.devutils.web.ToolsPage;
import org.orienteer.junit.OrienteerTestRunner;
import org.orienteer.junit.OrienteerTester;
import org.orienteer.junit.Sudo;

import static org.junit.Assert.*;

import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OResultSet;

import ru.ydn.wicket.wicketconsole.ScriptExecutor;
import ru.ydn.wicket.wicketconsole.ScriptExecutorHolder;


@RunWith(OrienteerTestRunner.class)
@Singleton
public class TestModule
{
	@Inject
	private OrienteerTester tester;
    
	@Test
	public void testModuleLoaded()
	{
	    OrienteerWebApplication app = tester.getApplication();
	    assertNotNull(app);
	    IOrienteerModule module = app.getModuleByName("devutils");
	    assertNotNull(module);
	    assertTrue(module instanceof Module);
	}
	
	@Test
	public void testSimpleSQL() {
		ScriptExecutor se = ScriptExecutorHolder.get().getScriptExecutor();
		assertEquals(tester.getSchema().getClass("OUser").count(), 
				(Object)  ((OResultSet<ODocument>)se.executeWithoutHistory("SELECT count(1) from OUser","SQL",null).getResult()).get(0).field("count"));
	}
	
	@Test
	@Sudo
	public void testPageLoad() {
		tester.startPage(ToolsPage.class, new PageParameters().add("tab", "console"));
		tester.assertRenderedPage(ToolsPage.class);
		tester.startPage(ToolsPage.class, new PageParameters().add("tab", "monitoring"));
		tester.assertRenderedPage(ToolsPage.class);
	}
}

@@ -128,16 +128,62 @@
 Module;%0A
+import org.orienteer.core.util.OSchemaHelper;%0A
 import o
@@ -219,16 +219,16 @@
 lsPage;%0A
-
 import o
@@ -446,16 +446,71 @@
 meters;%0A
+import org.junit.Before;%0Aimport org.junit.BeforeClass;%0A
 import o
@@ -863,16 +863,69 @@
 Holder;%0A
+import ru.ydn.wicket.wicketorientdb.utils.DBClosure;%0A
 %0A%0A@RunWi
@@ -1000,16 +1000,16 @@
 @Inject%0A
-
 %09private
@@ -1033,16 +1033,441 @@
 tester;%0A
+%09%0A%09private static boolean isDevUtilsActivated = false;%0A%09%0A%09@Before%0A%09public void activateModel() %7B%0A%09%09if(!isDevUtilsActivated) OSchemaHelper.bind(tester.getDatabase()).oClass(IOrienteerModule.OMODULE_CLASS)%0A%09%09%09%09%09.oDocument(IOrienteerModule.OMODULE_NAME, %22devutils%22)%0A%09%09%09%09%09.doOnODocument(doc -%3E %7B%0A%09%09%09%09%09%09doc.field(IOrienteerModule.OMODULE_ACTIVATE, true);%0A%09%09%09%09%09%09DBClosure.sudoSave(doc);%0A%09%09%09%09%09%09isDevUtilsActivated=true;%0A%09%09%09%09%09%7D);%0A%09%7D%0A
     %0A%09@T

Fix NPE in devutils tests due to disabled module by default