/*
 *
 *  * Copyright (C) 2016 Singular Studios (a.k.a Atom Tecnologia) - www.opensingular.com
 *  *
 *  * 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.opensingular.requirement.commons.wicket;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.junit.Assert;
import org.junit.Test;
import org.opensingular.form.wicket.helpers.AssertionsWComponent;
import org.opensingular.form.wicket.helpers.SingularWicketTester;
import org.opensingular.requirement.commons.admin.healthsystem.extension.AdministrationEntryExtension;
import org.opensingular.requirement.commons.admin.healthsystem.extension.CacheAdminEntry;
import org.opensingular.requirement.commons.admin.healthsystem.extension.DatabaseTablesAdminEntry;
import org.opensingular.requirement.commons.admin.healthsystem.extension.HtmlToPdfAdminEntry;
import org.opensingular.requirement.commons.admin.healthsystem.extension.JobsAdminEntry;
import org.opensingular.requirement.commons.admin.healthsystem.extension.PermissionAdminEntry;
import org.opensingular.requirement.commons.admin.healthsystem.extension.WebAdminEntry;

import org.opensingular.requirement.commons.test.SingularServletContextTestExecutionListener;
import org.opensingular.requirement.commons.admin.healthsystem.HealthSystemPage;
import org.opensingular.requirement.commons.test.CommonsApplicationMock;
import org.opensingular.requirement.commons.test.SingularCommonsBaseTest;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.context.TestExecutionListeners;

import javax.inject.Inject;
import javax.transaction.Transactional;

import static org.opensingular.requirement.commons.admin.healthsystem.HealthSystemPage.ENTRY_PATH_PARAM;

@TestExecutionListeners(listeners = {SingularServletContextTestExecutionListener.class}, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public class HealthSystemPageTest extends SingularCommonsBaseTest {

    @Inject
    CommonsApplicationMock singularApplication;

    private SingularWicketTester tester;

    @WithUserDetails("vinicius.nunes")
    @Transactional
    @Test
    public void testHealthSystemPageRendering() {
        tester = new SingularWicketTester(singularApplication);
        Page p = new HealthSystemPage();
        tester.startPage(p);
        tester.assertRenderedPage(HealthSystemPage.class);
    }

    @WithUserDetails("vinicius.nunes")
    @Transactional
    @Test
    public void testClickDbButton() {
        clickButtonAndCheckPanel(new DatabaseTablesAdminEntry());
    }

    @WithUserDetails("vinicius.nunes")
    @Transactional
    @Test
    public void testClickCacheButton() {
        clickButtonAndCheckPanel(new CacheAdminEntry());
    }

    @WithUserDetails("vinicius.nunes")
    @Transactional
    @Test
    public void testClickJobButton() {
        clickButtonAndCheckPanel(new JobsAdminEntry());
    }

    @WithUserDetails("vinicius.nunes")
    @Transactional
    @Test
    public void testClickPermissionButton() {
        clickButtonAndCheckPanel(new PermissionAdminEntry());
    }

    @WithUserDetails("vinicius.nunes")
    @Transactional
    @Test
    public void testClickWebButton() {
        clickButtonAndCheckPanel(new WebAdminEntry());
    }

    @WithUserDetails("vinicius.nunes")
    @Transactional
    @Test
    public void testClickHtmlToPdfButton() {
        clickButtonAndCheckPanel(new HtmlToPdfAdminEntry());
    }

    private void clickButtonAndCheckPanel(AdministrationEntryExtension administrationEntryExtension) {
        tester = new SingularWicketTester(singularApplication);
        Page p = new HealthSystemPage(new PageParameters().add(ENTRY_PATH_PARAM, administrationEntryExtension.getKey()));
        tester.startPage(p);
        Component container = new AssertionsWComponent(p).getSubComponentWithId("content").getTarget();
        if (tester.isRenderedPage(HealthSystemPage.class).wasFailed() || !administrationEntryExtension.makePanel("id").getClass().isInstance(container)) {
            Assert.fail(administrationEntryExtension + " não foi renderizado corretamente");
        }
    }
}

@@ -916,20 +916,93 @@
 g.junit.
-Test
+FixMethodOrder;%0Aimport org.junit.Test;%0Aimport org.junit.runners.MethodSorters
 ;%0Aimport
@@ -2422,16 +2422,62 @@
 PARAM;%0A%0A
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)%0A
 @TestExe
@@ -2883,16 +2883,17 @@
 oid test
+1
 HealthSy
@@ -3198,24 +3198,25 @@
 ic void test
+2
 ClickDbButto
@@ -3378,24 +3378,25 @@
 ic void test
+9
 ClickCacheBu
@@ -3552,24 +3552,25 @@
 ic void test
+4
 ClickJobButt
@@ -3723,24 +3723,25 @@
 ic void test
+5
 ClickPermiss
@@ -3907,24 +3907,25 @@
 ic void test
+6
 ClickWebButt
@@ -4053,32 +4053,32 @@
 ional%0A    @Test%0A
-
     public void 
@@ -4081,16 +4081,17 @@
 oid test
+7
 ClickHtm

Revert "[SGL-547] - Remoção do teste unitario que testa o click do cacheButton"