/*
 * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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.wso2.am.integration.tests.listener;

import org.apache.commons.lang.StringUtils;
import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

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

public class APIMAlterSuiteListener implements IAlterSuiteListener {
    @Override
    public void alter(List<XmlSuite> list) {
        String testsToRunCommaSeparated = System.getenv("PRODUCT_APIM_TESTS");
        if (StringUtils.isBlank(testsToRunCommaSeparated)) {
            return;
        }
        String[] enabledTests = testsToRunCommaSeparated.split(",");
        for (XmlSuite suite: list) {
            if ("ApiManager-features-test-suite".equals(suite.getName())) {
                List<XmlTest> newXMLTests = new ArrayList<>();
                for(XmlTest test: suite.getTests()) {
                    for (String enabledTest: enabledTests) {
                        if (enabledTest.trim().equals(test.getName().trim())) {
                            newXMLTests.add(test);
                        }
                    }
                }
                suite.setTests(newXMLTests);
            }
        }
    }
}

@@ -797,16 +797,48 @@
 stener;%0A
+import org.testng.xml.XmlClass;%0A
 import o
@@ -1164,159 +1164,630 @@
     
-if (StringUtils.isBlank(testsToRunCommaSeparated)) %7B%0A            return;%0A        %7D%0A        String%5B%5D enabledTests = testsToRunCommaSeparated.split(%22,%22);
+String testClassesToRunCommaSeparated = System.getenv(%22PRODUCT_APIM_TEST_CLASSES%22);%0A        if (StringUtils.isBlank(testsToRunCommaSeparated) && StringUtils.isBlank(testClassesToRunCommaSeparated) ) %7B%0A            return;%0A        %7D%0A        String%5B%5D enabledTests = new String%5B%5D%7B%7D;%0A        String%5B%5D enabledTestClasses = new String%5B%5D%7B%7D;%0A        if (!StringUtils.isBlank(testsToRunCommaSeparated)) %7B%0A            enabledTests = testsToRunCommaSeparated.split(%22,%22);%0A        %7D%0A        if (!StringUtils.isBlank(testClassesToRunCommaSeparated)) %7B%0A            enabledTestClasses = testClassesToRunCommaSeparated.split(%22,%22);%0A        %7D
 %0A   
@@ -1978,16 +1978,17 @@
      for
+ 
 (XmlTest
@@ -1988,17 +1988,20 @@
 XmlTest 
-t
+xmlT
 est: sui
@@ -2041,125 +2041,1834 @@
     
-for (String enabledTest: enabledTests) %7B%0A                        if (enabledTest.trim().equals(test.getName().trim())
+// process PRODUCT_APIM_TESTS to select xml tests to run%0A                    boolean xmlTestAdded = false;%0A                    for (String enabledTest: enabledTests) %7B%0A                        if (enabledTest.trim().equals(xmlTest.getName().trim())) %7B%0A                            xmlTestAdded = true;%0A                            newXMLTests.add(xmlTest);%0A                        %7D%0A                    %7D%0A%0A                    List%3CXmlClass%3E selectedXmlClassList = new ArrayList%3C%3E();%0A                    // process PRODUCT_APIM_TEST_CLASSES to select xml test classes to run%0A                    for (String enabledTestClass : enabledTestClasses) %7B%0A                        List%3CXmlClass%3E xmlClassList = xmlTest.getClasses();%0A                        for (int i = 0; i %3C xmlClassList.size(); i++) %7B%0A                            if (enabledTestClass.trim().equals(xmlClassList.get(i).getName().trim())) %7B%0A                                // add first XML test class of the XML test always if any of the test xml class is%0A                                // selected.%0A                                if (i != 0) %7B%0A                                    selectedXmlClassList.add(xmlClassList.get(0));%0A                                %7D%0A                                selectedXmlClassList.add(xmlClassList.get(i));%0A                            %7D%0A                        %7D%0A                    %7D%0A                    // if any xml class is selected in the particular xml test, we need to run the xml test if it is%0A                    //  not added to the suite to run%0A                    if (selectedXmlClassList.size() %3E 0) %7B%0A                        xmlTest.setClasses(selectedXmlClassList);%0A                        // when the xml is not added already from the PRODUCT_APIM_TESTS list, need to add them.%0A                        if (!xmlTestAdded
 ) %7B%0A
@@ -3911,17 +3911,20 @@
 sts.add(
-t
+xmlT
 est);%0A  

add ability to run individual tests using PRODUCT_APIM_TEST_CLASSES env