All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.automationrockstars.gir.data.impl.SimpleTestDataService Maven / Gradle / Ivy

The newest version!
/*
 * 
 */

package com.automationrockstars.gir.data.impl;

import com.automationrockstars.gir.data.TestData;
import com.automationrockstars.gir.data.TestDataRecord;
import com.automationrockstars.gir.data.TestDataService;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

public class SimpleTestDataService implements TestDataService {

    public static final String NAME = "DEFAULT";
    private static final Logger LOG = LoggerFactory.getLogger(SimpleTestDataService.class);
    private final Map, TestData> sharedTestDataCache = Maps.newConcurrentMap();
    //cache disabled
    // is sharing data disabled as well?
    private final Map, List>> RECORDS_CACHE = Maps.newConcurrentMap();

    @Override
    public void close() throws IOException {
        Files.write(new GsonBuilder().setPrettyPrinting().create().toJson(RECORDS_CACHE), Paths.get("output.json").toFile(), Charset.defaultCharset());
        RECORDS_CACHE.clear();
    }

    private void populate(TestData testData, List> dataToBeAdded) {
        if (dataToBeAdded != null) {
            for (Map dataEntry : dataToBeAdded) {
                testData.addNew().with(dataEntry);
            }
        }
    }

    @SuppressWarnings("unchecked")
    private  TestData getShared(Class type) {
        TestData result = (TestData) sharedTestDataCache.get(type);
        if (result == null) {
            result = new SimpleTestData<>(true, type);
            sharedTestDataCache.put(type, result);
            populate(result, RECORDS_CACHE.get(type));
        }
        return result;
    }

    @Override
    public  TestData provide(String name, boolean shared, Class type) {
        if (SimpleTestData.NAME.equals(name)) {
            if (shared) {
                return getShared(type);
            } else {
                TestData result = new SimpleTestData<>(false, type);
                populate(result, RECORDS_CACHE.get(type));
                return result;
            }
        } else return null;
    }

    @Override
    public String name() {
        return NAME;
    }

    @Override
    public void loadFrom(String file) {
        try {
            RECORDS_CACHE.putAll(DataLoader.loadFrom(file));
        } catch (IOException e) {
            LOG.error("Reading {} failed", file, e);
        }

    }

    @Override
    public void loadFrom(InputStream content) {
        try {
            RECORDS_CACHE.putAll(DataLoader.loadFrom(content));
        } catch (IOException e) {
            LOG.error("Reading failed", e);
        }

    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy