package gluu.scim2.client;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.ITestContext;
import org.testng.annotations.BeforeSuite;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

/**
 * Created by jgomer on 2017-06-09.
 * Base class for tests (as former BaseScimTest) but reads contents for properties from files instead of a having all JSON
 * content written in a single .properties file.
 */
public class BaseTest {

    private static final String FILE_PREFIX="file:";
    private static final Charset DEFAULT_CHARSET=Charset.forName("UTF-8");
    private static final String NEW_LINE=System.getProperty("line.separator");

    private Logger logger = LogManager.getLogger(getClass());

    private String decodeFileValue(String value){

        String decoded = value;
        if (value.startsWith(FILE_PREFIX)) {
            value = value.substring(FILE_PREFIX.length());    //remove the prefix
            try (BufferedReader bfr = Files.newBufferedReader(Paths.get(value), DEFAULT_CHARSET)) {     //create reader
                //appends every line after another
                decoded = bfr.lines().reduce("", (partial, next) -> partial + NEW_LINE + next);
                if (decoded.length()==0)
                    logger.warn("Key '{}' is empty", value);
            }
            catch (IOException e){
                logger.error(e.getMessage(), e);
                decoded=null;
            }
            //No need to close bfr: try-with-resources statements does it for me
        }
        return decoded;

    }

    @BeforeSuite
    public void initTestSuite(ITestContext context) throws Exception {

        logger.info("Invoked initTestSuite of '{}'", context.getCurrentXmlTest().getName());

        //Properties with the file: preffix will point to real .json files stored under src/test/resources folder
        String properties = context.getCurrentXmlTest().getParameter("file");
        Properties prop = new Properties();
        prop.load(Files.newBufferedReader(Paths.get(properties), DEFAULT_CHARSET));     //do not bother much about IO issues here. Ensure your files are accessible!

        Map<String, String> parameters = new Hashtable<>();
        //do not bother about empty keys... but
        //If a value is found null, this will throw a NPE since we are using a Hashtable
        prop.forEach((Object key, Object value) -> parameters.put(key.toString(), decodeFileValue(value.toString())));
        // Override test parameters
        context.getSuite().getXmlSuite().setParameters(parameters);
    }

}

@@ -32,83 +32,432 @@
 ort 
-org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger
+gluu.scim2.client.factory.ScimClientFactory;%0Aimport gluu.scim2.client.rest.ClientSideService;%0Aimport org.apache.commons.lang.StringUtils;%0Aimport org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0Aimport org.codehaus.jackson.map.DeserializationConfig;%0Aimport org.codehaus.jackson.map.ObjectMapper;%0Aimport org.codehaus.jackson.type.TypeReference;%0Aimport org.gluu.oxtrust.model.scim2.user.UserResource
 ;%0Aim
@@ -528,16 +528,68 @@
 reSuite;
+%0Aimport org.xdi.oxauth.client.service.ClientFactory;
 %0A%0Aimport
@@ -1265,21 +1265,75 @@
 %0A%0A    pr
-iva
+otected static ClientSideService client=null;%0A    protec
 te
+d
  Logger 
@@ -1370,24 +1370,78 @@
 getClass());
+%0A    protected ObjectMapper mapper=new ObjectMapper();
 %0A%0A    privat
@@ -3278,16 +3278,1182 @@
 meters);
+%0A%0A        if (client==null) %7B%0A            setupClient(context.getSuite().getXmlSuite().getParameters());%0A            mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);%0A        %7D%0A    %7D%0A%0A    private void setupClient(Map%3CString, String%3E params) throws Exception%7B%0A%0A        logger.info(%22Initializing client...%22);%0A        boolean testMode= StringUtils.isNotEmpty(System.getProperty(%22testmode%22));%0A        if (testMode)%0A            //client=ScimClientFactory.getTestClient(params.get(%22domainURL%22), params.get(%22OIDCMetadataUrl%22));%0A            client=ScimClientFactory.getDummyClient(params.get(%22domainURL%22));%0A        else%0A            client=ScimClientFactory.getClient(%0A                    params.get(%22domainURL%22),%0A                    params.get(%22umaMetaDataUrl%22),%0A                    params.get(%22umaAatClientId%22),%0A                    params.get(%22umaAatClientJksPath%22),%0A                    params.get(%22umaAatClientJksPassword%22),%0A                    params.get(%22umaAatClientKeyId%22));%0A    %7D%0A%0A    public UserResource getDeepCloneUsr(UserResource bean) throws Exception%7B%0A        return mapper.readValue(mapper.writeValueAsString(bean), UserResource.class);
 %0A    %7D%0A%0A

Add methods and members common to all test suites