<commit_before>package org.gem.engine.hazard.memcached;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.lang.reflect.Type;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import net.spy.memcached.MemcachedClient;

import org.junit.Before;
import org.junit.Test;
import org.opensha.commons.data.Site;
import org.opensha.commons.geo.Location;
import org.opensha.gem.GEM1.calc.gemOutput.GEMHazardCurveRepository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class HazardCurveSerializerTest
{

    private static final int PORT = 11211;
    private static final String LOCALHOST = "localhost";

    private MemcachedClient client;

    @Before
    public void setUp() throws Exception
    {
        client = new MemcachedClient(new InetSocketAddress(LOCALHOST, PORT));
        client.flush(); // clear the server side cache
    }

    @Test
    // learning test for http://sites.google.com/site/gson
    public void canSerializeAndDeserializeData()
    {
        List<Double> data = new ArrayList<Double>();
        data.add(1.0);
        data.add(2.0);
        data.add(3.0);

        Type listType = new TypeToken<List<Double>>(){}.getType();
        assertEquals(data, new Gson().fromJson(new Gson().toJson(data), listType));
    }

    @Test
    public void serializesASingleCurve() throws Exception
    {
        new HazardCurveSerializer("CURVE", new Cache(LOCALHOST, PORT)).serialize(sampleCurveAtSite(1.0, 2.0));

        assertThat(cachedCurveAtKey("CURVE"), is(sampleCurveAtSite(1.0, 2.0)));
    }

    @Test
    public void serializesMultipleCurves() throws Exception
    {
        List<HazardCurveDTO> curves = new ArrayList<HazardCurveDTO>();
        curves.add(sampleCurveAtSite(1.0, 2.0));
        curves.add(sampleCurveAtSite(2.0, 3.0));
        curves.add(sampleCurveAtSite(3.0, 4.0));

        Cache cache = mock(Cache.class);
        new HazardCurveSerializer("CURVE", cache).serialize(curves);

        // testing with a mock that the serializer serializes all the curves
        verify(cache).set("CURVE", sampleCurveAtSite(1.0, 2.0).toJSON());
        verify(cache).set("CURVE", sampleCurveAtSite(2.0, 3.0).toJSON());
        verify(cache).set("CURVE", sampleCurveAtSite(3.0, 4.0).toJSON());
    }

    @Test
    public void serializesMultipleCurvesUsingGEMHazardCurveRepository() throws Exception
    {
        // Hazard engine API
        GEMHazardCurveRepository repository = new GEMHazardCurveRepository();

        // sites where the curves are defined
        ArrayList<Site> nodes = new ArrayList<Site>();
        nodes.add(new Site(new Location(2.0, 1.0)));
        nodes.add(new Site(new Location(4.0, 4.0)));

        repository.setGridNode(nodes);

        // X values
        ArrayList<Double> groundMotionLevels = new ArrayList<Double>();
        groundMotionLevels.add(1.0);
        groundMotionLevels.add(2.0);
        groundMotionLevels.add(3.0);
        groundMotionLevels.add(4.0);

        repository.setGmLevels(groundMotionLevels);

        // Y values
        Double[] values = { 1.0, 2.0, 3.0, 4.0 };
        ArrayList<Double[]> probabilitiesOfExc = new ArrayList<Double[]>();

        probabilitiesOfExc.add(values);
        probabilitiesOfExc.add(values);
        probabilitiesOfExc.add(values);

        repository.setProbExList(probabilitiesOfExc);

        Cache cache = mock(Cache.class);
        new HazardCurveSerializer("CURVE", cache).serialize(repository);

        // testing with a mock that the serializer serializes all the curves
        verify(cache).set("CURVE", sampleCurveAtSite(1.0, 2.0).toJSON());
        verify(cache).set("CURVE", sampleCurveAtSite(4.0, 4.0).toJSON());
    }

    private HazardCurveDTO sampleCurveAtSite(Double lon, Double lat)
    {
        // X sample values
        List<Double> groundMotionLevels = new ArrayList<Double>();
        groundMotionLevels.add(1.0);<commit_msg>Hazard curve memcached serialization<commit_after>package org.gem.engine.hazard.memcached;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.lang.reflect.Type;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import net.spy.memcached.MemcachedClient;

import org.junit.Before;
import org.junit.Test;
import org.opensha.commons.data.Site;
import org.opensha.commons.geo.Location;
import org.opensha.gem.GEM1.calc.gemOutput.GEMHazardCurveRepository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class HazardCurveSerializerTest
{

    private static final String CACHE_KEY = "CURVE";

    private static final int PORT = 11211;
    private static final String LOCALHOST = "localhost";

    private MemcachedClient client;

    @Before
    public void setUp() throws Exception
    {
        client = new MemcachedClient(new InetSocketAddress(LOCALHOST, PORT));
        client.flush(); // clear the server side cache
    }

    @Test
    // learning test for http://sites.google.com/site/gson
    public void canSerializeAndDeserializeData()
    {
        List<Double> data = new ArrayList<Double>();
        data.add(1.0);
        data.add(2.0);
        data.add(3.0);

        Type listType = new TypeToken<List<Double>>(){}.getType();
        assertEquals(data, new Gson().fromJson(new Gson().toJson(data), listType));
    }

    @Test
    public void serializesASingleCurve() throws Exception
    {
        new HazardCurveSerializer(CACHE_KEY, new Cache(LOCALHOST, PORT)).serialize(sampleCurveAtSite(1.0, 2.0));

        assertThat(cachedCurveAtKey(CACHE_KEY), is(sampleCurveAtSite(1.0, 2.0)));
    }

    @Test
    public void serializesMultipleCurves() throws Exception
    {
        List<HazardCurveDTO> curves = new ArrayList<HazardCurveDTO>();
        curves.add(sampleCurveAtSite(1.0, 2.0));
        curves.add(sampleCurveAtSite(2.0, 3.0));
        curves.add(sampleCurveAtSite(3.0, 4.0));

        Cache cache = mock(Cache.class);
        new HazardCurveSerializer(CACHE_KEY, cache).serialize(curves);

        // testing with a mock that the serializer serializes all the curves
        verify(cache).set(CACHE_KEY, sampleCurveAtSite(1.0, 2.0).toJSON());
        verify(cache).set(CACHE_KEY, sampleCurveAtSite(2.0, 3.0).toJSON());
        verify(cache).set(CACHE_KEY, sampleCurveAtSite(3.0, 4.0).toJSON());
    }

    @Test
    public void serializesMultipleCurvesUsingGEMHazardCurveRepository() throws Exception
    {
        // Hazard engine API
        GEMHazardCurveRepository repository = new GEMHazardCurveRepository();

        // sites where the curves are defined
        ArrayList<Site> nodes = new ArrayList<Site>();
        nodes.add(new Site(new Location(2.0, 1.0)));
        nodes.add(new Site(new Location(4.0, 4.0)));

        repository.setGridNode(nodes);

        // X values
        ArrayList<Double> groundMotionLevels = new ArrayList<Double>();
        groundMotionLevels.add(1.0);
        groundMotionLevels.add(2.0);
        groundMotionLevels.add(3.0);
        groundMotionLevels.add(4.0);

        repository.setGmLevels(groundMotionLevels);

        // Y values
        Double[] values = { 1.0, 2.0, 3.0, 4.0 };
        ArrayList<Double[]> probabilitiesOfExc = new ArrayList<Double[]>();

        probabilitiesOfExc.add(values);
        probabilitiesOfExc.add(values);
        probabilitiesOfExc.add(values);

        repository.setProbExList(probabilitiesOfExc);

        Cache cache = mock(Cache.class);
        new HazardCurveSerializer(CACHE_KEY, cache).serialize(repository);

        // testing with a mock that the serializer serializes all the curves
        verify(cache).set(CACHE_KEY, sampleCurveAtSite(1.0, 2.0).toJSON());
        verify(cache).set(CACHE_KEY, sampleCurveAtSite(4.0, 4.0).toJSON());
    }

    private HazardCurveDTO sampleCurveAtSite(Double lon, Double lat)
    {
        // X sample values
        List<Double> groundMotionLevels = new ArrayList<Double>();
        groundMotionLevels.add(1.0);
{'fraction_non_alphanumeric': 0.11981234211476001, 'fraction_numerical': 0.018765788523998555, 'mean_word_length': 3.2679671457905544, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '420467', 'n_tokens_mistral': 2617, 'n_tokens_neox': 2544, 'n_words': 514}