/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.integrationtest.htmlunit;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.openengsb.integrationtest.util.AbstractExamTestHelper;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

@RunWith(JUnit4TestRunner.class)
public class BaseUiInfrastructureIT extends AbstractExamTestHelper {

    @Test
    public void testIfAllMainNavigationLinksWork() throws Exception {
        final WebClient webClient = new WebClient();
        String loginPageEntryUrl =
            "http://localhost:8090/openengsb/?wicket:bookmarkablePage=:org.openengsb.ui.web.LoginPage";
        final HtmlPage page = webClient.getPage(loginPageEntryUrl);
        HtmlForm form = page.getForms().get(0);
        HtmlSubmitInput loginButton = form.getInputByValue("Login");
        form.getInputByName("username").setValueAttribute("admin");
        form.getInputByName("password").setValueAttribute("password");
        HtmlPage indexPage = loginButton.click();
        assertTrue(indexPage.asText().contains("This page represents"));
        HtmlPage testClient = indexPage.getAnchorByText("Test Client").click();
        assertTrue(testClient.asText().contains("Current Project"));
        HtmlPage sendEventpage = testClient.getAnchorByText("Send Event Page").click();
        assertTrue(sendEventpage.asText().contains("Current Project"));
        HtmlPage contextPage = testClient.getAnchorByText("Context").click();
        assertTrue(contextPage.asText().contains("Current Project"));
        HtmlPage servicePage = testClient.getAnchorByText("Services").click();
        assertTrue(servicePage.asText().contains("Services with state = Connecting"));
        HtmlPage usermanagementPage = testClient.getAnchorByText("User Management").click();
        assertTrue(usermanagementPage.asText().contains("Create new user"));
        webClient.closeAllWindows();
    }

}

@@ -720,16 +720,102 @@
 rtTrue;%0A
+import static org.junit.Assert.fail;%0A%0Aimport org.junit.After;%0Aimport org.junit.Before;
 %0Aimport 
@@ -980,16 +980,85 @@
 unner;%0A%0A
+import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;%0A
 import c
@@ -1364,16 +1364,250 @@
 lper %7B%0A%0A
+    private WebClient webClient;%0A%0A    @Before%0A    public void setUp() throws Exception %7B%0A        webClient = new WebClient();%0A    %7D%0A%0A    @After%0A    public void tearDown() throws Exception %7B%0A        webClient.closeAllWindows();%0A    %7D%0A%0A
     @Tes
@@ -1682,61 +1682,8 @@
 n %7B%0A
-        final WebClient webClient = new WebClient();%0A
     
@@ -3061,42 +3061,868 @@
 ));%0A
+%0A
     
+%7D%0A%0A
     
-webClient.closeAllWindows(
+@Test(expected = FailingHttpStatusCodeException.class)%0A    public void testUserLoginWithLimitedAccess() throws Exception %7B%0A        String loginPageEntryUrl =%0A            %22http://localhost:8090/openengsb/?wicket:bookmarkablePage=:org.openengsb.ui.web.LoginPage%22;%0A        final HtmlPage page = webClient.getPage(loginPageEntryUrl);%0A        HtmlForm form = page.getForms().get(0);%0A        HtmlSubmitInput loginButton = form.getInputByValue(%22Login%22);%0A        form.getInputByName(%22username%22).setValueAttribute(%22user%22);%0A        form.getInputByName(%22password%22).setValueAttribute(%22password%22);%0A        HtmlPage indexPage = loginButton.click();%0A        assertTrue(indexPage.asText().contains(%22This page represents%22));%0A        HtmlPage testClient;%0A        testClient = indexPage.getAnchorByText(%22Test Client%22).click();%0A        fail(%22could display Test client%22
 );%0A 

add ui-test with accessDeniedException