/*
 * Copyright 2015-2016 Red Hat, Inc, and individual contributors.
 *
 * 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.jboss.hal.testsuite.test.configuration;

import java.util.ArrayList;
import java.util.List;

import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.hal.meta.token.NameTokens;
import org.jboss.hal.resources.Ids;
import org.jboss.hal.resources.Names;
import org.jboss.hal.testsuite.Console;
import org.jboss.hal.testsuite.creaper.ManagementClientProvider;
import org.jboss.hal.testsuite.fragment.finder.ColumnFragment;
import org.jboss.hal.testsuite.fragment.finder.FinderPath;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebElement;
import org.wildfly.extras.creaper.core.online.OnlineManagementClient;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.Operations;

import static org.jboss.hal.dmr.ModelDescriptionConstants.SUBSYSTEM;
import static org.junit.Assert.assertEquals;

@RunWith(Arquillian.class)
public class SubsystemsTest {

    private static final OnlineManagementClient client = ManagementClientProvider.createOnlineManagementClient();
    private static final Operations operations = new Operations(client);
    private static List<String> subsystems = new ArrayList<>();

    @BeforeClass
    public static void beforeClass() throws Exception {
        subsystems = operations.readChildrenNames(Address.root(), SUBSYSTEM).stringListValue();
    }

    @Inject private Console console;
    private ColumnFragment column;

    @Before
    public void setUp() throws Exception {
        column = console.finder(NameTokens.CONFIGURATION,
                new FinderPath().append(Ids.CONFIGURATION, Ids.asId(Names.SUBSYSTEMS)))
                .column(Ids.CONFIGURATION_SUBSYSTEM);
    }

    @Test
    public void numberOfSubsystems() {
        assertEquals(subsystems.size(), column.getItems().size());
    }

    @Test
    public void filter() {
        String filter = "io";
        column.filter(filter);

        long filtered = subsystems.stream()
                .filter(subsystem -> subsystem.toLowerCase().contains(filter))
                .count();
        long visible = column.getItems().stream()
                .filter(WebElement::isDisplayed)
                .count();
        assertEquals(filtered, visible);
    }
}

@@ -1528,16 +1528,55 @@
 tions;%0A%0A
+import static java.util.Arrays.asList;%0A
 import s
@@ -1636,16 +1636,16 @@
 SYSTEM;%0A
-
 import s
@@ -1680,16 +1680,60 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertFalse;
 %0A%0A@RunWi
@@ -2034,16 +2034,267 @@
 ist%3C%3E();
+%0A    // this is from org.jboss.hal.client.configuration.subsystem.SubsystemColumn%0A    private static final List%3CString%3E EMPTY_SUBSYSTEMS = asList(%22bean-validation%22, %22core-management%22, %22ee-security%22,%0A            %22jaxrs%22, %22jdr%22, %22jsr77%22, %22pojo%22, %22sar%22);
 %0A%0A    @B
@@ -2885,16 +2885,42 @@
 s.size()
+ - EMPTY_SUBSYSTEMS.size()
 , column
@@ -2939,32 +2939,209 @@
 size());%0A    %7D%0A%0A
+    @Test%0A    public void emptySubsystemsNotDisplayed() %7B%0A        for (String subsys: EMPTY_SUBSYSTEMS) %7B%0A            assertFalse(column.containsItem(subsys));%0A        %7D%0A    %7D%0A%0A
     @Test%0A    pu
@@ -3259,32 +3259,32 @@
 ystems.stream()%0A
-
                 
@@ -3344,16 +3344,83 @@
 (filter)
+%0A                        && EMPTY_SUBSYSTEMS.indexOf(subsystem) %3C 0
 )%0A      

Add tests for "HAL-1462 Hide configuration screens that doesn't contain any attribute or sub-resources"