<commit_before>package fi.hsl.parkandride.back;

import static fi.hsl.parkandride.core.domain.CapacityType.CAR;
import static fi.hsl.parkandride.core.domain.CapacityType.ELECTRIC_CAR;
import static fi.hsl.parkandride.core.domain.DayType.BUSINESS_DAY;
import static fi.hsl.parkandride.core.domain.DayType.SATURDAY;
import static fi.hsl.parkandride.core.domain.DayType.SUNDAY;
import static fi.hsl.parkandride.core.domain.FacilityStatus.EXCEPTIONAL_SITUATION;
import static fi.hsl.parkandride.core.domain.FacilityStatus.IN_OPERATION;
import static fi.hsl.parkandride.core.domain.PricingMethod.PARK_AND_RIDE_247_FREE;
import static fi.hsl.parkandride.core.domain.Service.ACCESSIBLE_TOILETS;
import static fi.hsl.parkandride.core.domain.Service.ELEVATOR;
import static fi.hsl.parkandride.core.domain.Service.LIGHTING;
import static fi.hsl.parkandride.core.domain.Service.TOILETS;
import static fi.hsl.parkandride.core.domain.Sort.Dir.ASC;
import static fi.hsl.parkandride.core.domain.Sort.Dir.DESC;
import static fi.hsl.parkandride.core.domain.Usage.COMMERCIAL;
import static fi.hsl.parkandride.core.domain.Usage.PARK_AND_RIDE;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

import java.util.*;

import javax.inject.Inject;

import org.geolatte.geom.Point;
import org.geolatte.geom.Polygon;
import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;

import fi.hsl.parkandride.core.back.ContactRepository;
import fi.hsl.parkandride.core.back.FacilityRepository;
import fi.hsl.parkandride.core.back.OperatorRepository;
import fi.hsl.parkandride.core.domain.*;
import fi.hsl.parkandride.core.service.ValidationException;

public class FacilityDaoTest extends AbstractDaoTest {

    public static final MultilingualString NAME = new MultilingualString("Facility");

    public static final MultilingualString STATUS_DESCRIPTION = new MultilingualString("Status description");

    public static final MultilingualString OPENING_HOURS_INFO = new MultilingualString("Opening Hours");

    public static final MultilingualUrl OPENING_HOURS_URL = new MultilingualUrl("http://www.hsl.fi");

    private static final Point PORT_LOCATION1 = (Point) Spatial.fromWkt("POINT(25.010822 60.25054)");

    private static final Point PORT_LOCATION2 = (Point) Spatial.fromWkt("POINT(25.012479 60.250337)");

    private static final Polygon LOCATION = (Polygon) Spatial.fromWkt("POLYGON((" +
            "25.010822 60.25054, " +
            "25.010822 60.250023, " +
            "25.012479 60.250337, " +
            "25.011449 60.250885, " +
            "25.010822 60.25054))");

    public static final Polygon OVERLAPPING_AREA = (Polygon) Spatial.fromWkt("POLYGON((" +
            "25.011942 60.251343, " +
            "25.011717 60.250454, " +
            "25.013487 60.250848, " +
            "25.011942 60.251343))");

    public static final Polygon NON_OVERLAPPING_AREA = (Polygon) Spatial.fromWkt("POLYGON((" +
            "25.011942 60.251343, " +
            "25.012211 60.250816, " +
            "25.013487 60.250848, " +
            "25.011942 60.251343))");

    public static final SortedSet<String> ALIASES = ImmutableSortedSet.of("alias", "blias");

    public static final List<Port> PORTS = ImmutableList.of(new Port(PORT_LOCATION1, true, false, true, false, "street", "00100", "city", "info"));

    public static final NullSafeSortedSet<Service> SERVICES = new NullSafeSortedSet<>(asList(ELEVATOR, TOILETS, ACCESSIBLE_TOILETS));

    public static final Pricing PRICING1 = new Pricing(CAR, PARK_AND_RIDE, 50, SATURDAY, "8", "18", "2 EUR/H");

    public static final Pricing PRICING2 = new Pricing(CAR, PARK_AND_RIDE, 50, SUNDAY, "8", "18", "1 EUR/H");

    public static final Map<CapacityType, Integer> BUILT_CAPACITY = ImmutableMap.of(
            CAR, 50,
            ELECTRIC_CAR, 2
    );

    public static final List<UnavailableCapacity> UNAVAILABLE_CAPACITIES = Arrays.asList(
            new UnavailableCapacity(CAR, PARK_AND_RIDE, 1)
    );

    @Inject
    ContactRepository contactDao;

    @Inject
    FacilityRepository facilityDao;

    @Inject
    OperatorRepository operatorDao;

    private FacilityContacts dummyContacts;

    private Long operatorId;

    @Before
    public void initialize() {
        dummyContacts = new FacilityContacts(createDummyContact(), createDummyContact());
        operatorId = createDummyOperator();
    }

    @Test
    public void create_read_update() {
        Facility facility = createFacility();

        // Insert
        final long id = facilityDao.insertFacility(facility);
        assertThat(id).isGreaterThan(0);
        assertThat(facility.id).isNotEqualTo(id);

        // Find by id
        facility = facilityDao.getFacility(id);
        assertDefault(facility);
        assertThat(facility.contacts).isEqualTo(dummyContacts);

        // Search
        assertDefault(facilityDao.findFacilities(new PageableFacilitySearch()).get(0));

        // Update
        final MultilingualString newName = new MultilingualString("changed name");
        final SortedSet<String> newAliases = ImmutableSortedSet.of("clias");
        final List<Port> newPorts = ImmutableList.of(new Port(PORT_LOCATION2, true, true, true, true), new Port(PORT_LOCATION1, false, false, false, false));
        final NullSafeSortedSet<Service> newServices = new NullSafeSortedSet<>(asList(LIGHTING));

        facility.name = newName;
        facility.status = IN_OPERATION;
        facility.pricingMethod = PARK_AND_RIDE_247_FREE;
        facility.statusDescription = null;
        facility.aliases = newAliases;
        facility.ports = newPorts;
        facility.services = newServices;

        facilityDao.updateFacility(id, facility);
        facility = facilityDao.getFacility(id);
        assertThat(facility.name).isEqualTo(newName);
        assertThat(facility.status).isEqualTo(IN_OPERATION);
        assertThat(facility.statusDescription).isNull();
        assertThat(facility.aliases).isEqualTo(newAliases);
        assertThat(facility.ports).isEqualTo(newPorts);
        assertThat(facility.services).isEqualTo(newServices);
        assertThat(facility.pricing).isEqualTo(asList(
                free24h(CAR, PARK_AND_RIDE, 50, BUSINESS_DAY),
                free24h(CAR, PARK_AND_RIDE, 50, SATURDAY),
                free24h(CAR, PARK_AND_RIDE, 50, SUNDAY),
                free24h(ELECTRIC_CAR, PARK_AND_RIDE, 2, BUSINESS_DAY),
                free24h(ELECTRIC_CAR, PARK_AND_RIDE, 2, SATURDAY),
                free24h(ELECTRIC_CAR, PARK_AND_RIDE, 2, SUNDAY)
        ));
        assertThat(facility.unavailableCapacities).isEqualTo(asList(
                new UnavailableCapacity(CAR, PARK_AND_RIDE, 1),
                new UnavailableCapacity(ELECTRIC_CAR, PARK_AND_RIDE, 0)
        ));

        // Remove aliases, capacities and ports
        facility.aliases = new HashSet<>();
        facility.ports = new ArrayList<>();
        facility.services = new NullSafeSortedSet<>();<commit_msg>test(materialization): Verify that all usages and capacityTypes are materialized<commit_after>package fi.hsl.parkandride.back;

import static fi.hsl.parkandride.core.domain.CapacityType.CAR;
import static fi.hsl.parkandride.core.domain.CapacityType.ELECTRIC_CAR;
import static fi.hsl.parkandride.core.domain.DayType.BUSINESS_DAY;
import static fi.hsl.parkandride.core.domain.DayType.SATURDAY;
import static fi.hsl.parkandride.core.domain.DayType.SUNDAY;
import static fi.hsl.parkandride.core.domain.FacilityStatus.EXCEPTIONAL_SITUATION;
import static fi.hsl.parkandride.core.domain.FacilityStatus.IN_OPERATION;
import static fi.hsl.parkandride.core.domain.PricingMethod.CUSTOM;
import static fi.hsl.parkandride.core.domain.PricingMethod.PARK_AND_RIDE_247_FREE;
import static fi.hsl.parkandride.core.domain.Service.ACCESSIBLE_TOILETS;
import static fi.hsl.parkandride.core.domain.Service.ELEVATOR;
import static fi.hsl.parkandride.core.domain.Service.LIGHTING;
import static fi.hsl.parkandride.core.domain.Service.TOILETS;
import static fi.hsl.parkandride.core.domain.Sort.Dir.ASC;
import static fi.hsl.parkandride.core.domain.Sort.Dir.DESC;
import static fi.hsl.parkandride.core.domain.Usage.COMMERCIAL;
import static fi.hsl.parkandride.core.domain.Usage.PARK_AND_RIDE;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.setAllowExtractingPrivateFields;
import static org.junit.Assert.fail;

import java.util.*;

import javax.inject.Inject;

import org.geolatte.geom.Point;
import org.geolatte.geom.Polygon;
import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;

import fi.hsl.parkandride.core.back.ContactRepository;
import fi.hsl.parkandride.core.back.FacilityRepository;
import fi.hsl.parkandride.core.back.OperatorRepository;
import fi.hsl.parkandride.core.domain.*;
import fi.hsl.parkandride.core.service.ValidationException;

public class FacilityDaoTest extends AbstractDaoTest {

    public static final MultilingualString NAME = new MultilingualString("Facility");

    public static final MultilingualString STATUS_DESCRIPTION = new MultilingualString("Status description");

    public static final MultilingualString OPENING_HOURS_INFO = new MultilingualString("Opening Hours");

    public static final MultilingualUrl OPENING_HOURS_URL = new MultilingualUrl("http://www.hsl.fi");

    private static final Point PORT_LOCATION1 = (Point) Spatial.fromWkt("POINT(25.010822 60.25054)");

    private static final Point PORT_LOCATION2 = (Point) Spatial.fromWkt("POINT(25.012479 60.250337)");

    private static final Polygon LOCATION = (Polygon) Spatial.fromWkt("POLYGON((" +
            "25.010822 60.25054, " +
            "25.010822 60.250023, " +
            "25.012479 60.250337, " +
            "25.011449 60.250885, " +
            "25.010822 60.25054))");

    public static final Polygon OVERLAPPING_AREA = (Polygon) Spatial.fromWkt("POLYGON((" +
            "25.011942 60.251343, " +
            "25.011717 60.250454, " +
            "25.013487 60.250848, " +
            "25.011942 60.251343))");

    public static final Polygon NON_OVERLAPPING_AREA = (Polygon) Spatial.fromWkt("POLYGON((" +
            "25.011942 60.251343, " +
            "25.012211 60.250816, " +
            "25.013487 60.250848, " +
            "25.011942 60.251343))");

    public static final SortedSet<String> ALIASES = ImmutableSortedSet.of("alias", "blias");

    public static final List<Port> PORTS = ImmutableList.of(new Port(PORT_LOCATION1, true, false, true, false, "street", "00100", "city", "info"));

    public static final NullSafeSortedSet<Service> SERVICES = new NullSafeSortedSet<>(asList(ELEVATOR, TOILETS, ACCESSIBLE_TOILETS));

    public static final Pricing PRICING1 = new Pricing(CAR, PARK_AND_RIDE, 50, SATURDAY, "8", "18", "2 EUR/H");

    public static final Pricing PRICING2 = new Pricing(CAR, PARK_AND_RIDE, 50, SUNDAY, "8", "18", "1 EUR/H");

    public static final Map<CapacityType, Integer> BUILT_CAPACITY = ImmutableMap.of(
            CAR, 50,
            ELECTRIC_CAR, 2
    );

    public static final List<UnavailableCapacity> UNAVAILABLE_CAPACITIES = Arrays.asList(
            new UnavailableCapacity(CAR, PARK_AND_RIDE, 1)
    );

    @Inject
    ContactRepository contactDao;

    @Inject
    FacilityRepository facilityDao;

    @Inject
    OperatorRepository operatorDao;

    private FacilityContacts dummyContacts;

    private Long operatorId;

    @Before
    public void initialize() {
        dummyContacts = new FacilityContacts(createDummyContact(), createDummyContact());
        operatorId = createDummyOperator();
    }

    @Test
    public void all_usages_and_capacities_are_materialized_and_projected() {
        Facility facility = createFacility();
        facility.pricingMethod = CUSTOM;
        final Map<CapacityType, Integer> builtCapacity = new HashMap<>();
        facility.pricing = new ArrayList<>();

        CapacityType[] capacityTypes = CapacityType.values();
        Usage[] usages = Usage.values();
        for (int i=0; i < capacityTypes.length || i < usages.length; i++) {
            CapacityType type = capacityTypes[i % capacityTypes.length];
            Usage usage = usages[i % usages.length];

            builtCapacity.put(type, i+1);
            facility.pricing.add(new Pricing(type, usage, i+1, BUSINESS_DAY, "0", "24", null));
        }
        facility.builtCapacity = builtCapacity;
        final long id = facilityDao.insertFacility(facility);
        facility = facilityDao.getFacility(id);
        assertThat(facility.builtCapacity).isEqualTo(builtCapacity);
        assertThat(facility.usages).isEqualTo(ImmutableSet.copyOf(Usage.values()));

        FacilitySummary facilitySummary = facilityDao.summarizeFacilities(new FacilitySearch());
        assertThat(facilitySummary.capacities).isEqualTo(builtCapacity);
    }

    @Test
    public void create_read_update() {
        Facility facility = createFacility();

        // Insert
        final long id = facilityDao.insertFacility(facility);
        assertThat(id).isGreaterThan(0);
        assertThat(facility.id).isNotEqualTo(id);

        // Find by id
        facility = facilityDao.getFacility(id);
        assertDefault(facility);
        assertThat(facility.contacts).isEqualTo(dummyContacts);

        // Search
        assertDefault(facilityDao.findFacilities(new PageableFacilitySearch()).get(0));

        // Update
        final MultilingualString newName = new MultilingualString("changed name");
        final SortedSet<String> newAliases = ImmutableSortedSet.of("clias");
        final List<Port> newPorts = ImmutableList.of(new Port(PORT_LOCATION2, true, true, true, true), new Port(PORT_LOCATION1, false, false, false, false));
        final NullSafeSortedSet<Service> newServices = new NullSafeSortedSet<>(asList(LIGHTING));

        facility.name = newName;
        facility.status = IN_OPERATION;
        facility.pricingMethod = PARK_AND_RIDE_247_FREE;
        facility.statusDescription = null;
        facility.aliases = newAliases;
        facility.ports = newPorts;
        facility.services = newServices;

        facilityDao.updateFacility(id, facility);
        facility = facilityDao.getFacility(id);
        assertThat(facility.name).isEqualTo(newName);
        assertThat(facility.status).isEqualTo(IN_OPERATION);
        assertThat(facility.statusDescription).isNull();
        assertThat(facility.aliases).isEqualTo(newAliases);
        assertThat(facility.ports).isEqualTo(newPorts);
        assertThat(facility.services).isEqualTo(newServices);
        assertThat(facility.pricing).isEqualTo(asList(
                free24h(CAR, PARK_AND_RIDE, 50, BUSINESS_DAY),
                free24h(CAR, PARK_AND_RIDE, 50, SATURDAY),
                free24h(CAR, PARK_AND_RIDE, 50, SUNDAY),
                free24h(ELECTRIC_CAR, PARK_AND_RIDE, 2, BUSINESS_DAY),
                free24h(ELECTRIC_CAR, PARK_AND_RIDE, 2, SATURDAY),
                free24h(ELECTRIC_CAR, PARK_AND_RIDE, 2, SUNDAY)
        ));
        assertThat(facility.usages).isEqualTo(ImmutableSet.of(PARK_AND_RIDE));
        assertThat(facility.unavailableCapacities).isEqualTo(asList(
                new UnavailableCapacity(CAR, PARK_AND_RIDE, 1),
                new UnavailableCapacity(ELECTRIC_CAR, PARK_AND_RIDE, 0)
        ));

        // Remove aliases, capacities and ports
        facility.aliases = new HashSet<>();
        facility.ports = new ArrayList<>();
        facility.services = new NullSafeSortedSet<>();
{'fraction_non_alphanumeric': 0.10718315156073711, 'fraction_numerical': 0.03748276294346246, 'mean_word_length': 3.623297594900029, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1919562', 'n_tokens_mistral': 5533, 'n_tokens_neox': 5073, 'n_words': 917}