/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2017-2017 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2017 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.features.geolocation.services;

import java.util.List;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennms.core.test.OpenNMSJUnit4ClassRunner;
import org.opennms.core.test.db.annotations.JUnitTemporaryDatabase;
import org.opennms.features.geocoder.Coordinates;
import org.opennms.features.geolocation.api.GeolocationInfo;
import org.opennms.features.geolocation.api.GeolocationQuery;
import org.opennms.features.geolocation.api.GeolocationQueryBuilder;
import org.opennms.features.geolocation.api.GeolocationResolver;
import org.opennms.features.geolocation.api.StatusCalculationStrategy;
import org.opennms.features.geolocation.services.status.AlarmStatusCalculator;
import org.opennms.features.geolocation.services.status.OutageStatusCalculator;
import org.opennms.netmgt.dao.DatabasePopulator;
import org.opennms.netmgt.dao.api.AlarmDao;
import org.opennms.netmgt.dao.api.DistPollerDao;
import org.opennms.netmgt.dao.api.GenericPersistenceAccessor;
import org.opennms.netmgt.dao.api.NodeDao;
import org.opennms.netmgt.model.OnmsGeolocation;
import org.opennms.netmgt.model.OnmsSeverity;
import org.opennms.test.JUnitConfigurationEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;

@RunWith(OpenNMSJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml",
        "classpath:/META-INF/opennms/applicationContext-minimal-conf.xml",
        "classpath:/META-INF/opennms/applicationContext-soa.xml",
        "classpath:/META-INF/opennms/applicationContext-dao.xml",
        "classpath*:/META-INF/opennms/component-dao.xml",
        "classpath*:/META-INF/opennms/component-service.xml",
        "classpath:/META-INF/opennms/mockEventIpcManager.xml",
        "classpath:/META-INF/opennms/applicationContext-databasePopulator.xml" })
@JUnitConfigurationEnvironment
@JUnitTemporaryDatabase
public class DefaultGeolocationServiceIT {

    private static final Coordinates coordinates = new Coordinates(51.485278, -3.186667);

    @Autowired
    private GenericPersistenceAccessor genericPersistenceAccessor;

    @Autowired
    private NodeDao nodeDao;

    @Autowired
    private AlarmDao alarmDao;

    @Autowired
    private DistPollerDao distPollerDao;

    @Autowired
    private DatabasePopulator databasePopulator;

    private GeolocationResolver geolocationResolver;

    private DefaultGeolocationService geolocationService;

    @Before
    public void before() {
        geolocationResolver = new DefaultGeolocationResolver(address -> coordinates, nodeDao);
        geolocationService = new DefaultGeolocationService(genericPersistenceAccessor, geolocationResolver);

        // Initialize Database and clean up alarms
        databasePopulator.populateDatabase();
        alarmDao.findAll().forEach(a -> alarmDao.delete(a));
        alarmDao.flush();
    }

    @After
    public void after() {
        databasePopulator.resetDatabase();
    }

    @Test
    public void verifyGetStatusCalculator() {
        Assert.assertEquals(DefaultGeolocationService.NULL_STATUS_CALCULATOR, geolocationService.getStatusCalculator(null));
        Assert.assertEquals(AlarmStatusCalculator.class, geolocationService.getStatusCalculator(StatusCalculationStrategy.Alarms).getClass());
        Assert.assertEquals(OutageStatusCalculator.class, geolocationService.getStatusCalculator(StatusCalculationStrategy.Outages).getClass());
    }

    @Test
    @Transactional
    public void verifyMerging() {
        // Set coordinates for all
        nodeDao.findAll().forEach(n -> {
            n.getAssetRecord().getGeolocation().setLongitude(coordinates.getLongitude());
            n.getAssetRecord().getGeolocation().setLatitude(coordinates.getLatitude());
            nodeDao.saveOrUpdate(n);
        });
        // Query
        GeolocationQuery query = new GeolocationQueryBuilder()
                .withStatusCalculationStrategy(StatusCalculationStrategy.Alarms)
                .build();

        // We do not have any alarms, therefore all nodes should be "NORMAL"
        List<GeolocationInfo> locations = geolocationService.getLocations(query);
        Assert.assertEquals(nodeDao.countAll(), locations.size());
        locations.forEach(l -> Assert.assertEquals("Normal", l.getSeverityInfo().getLabel()));

        // Add an alarm for one node and try again
        alarmDao.save(TestUtils.createAlarm(databasePopulator.getNode1(), OnmsSeverity.MAJOR, distPollerDao.whoami()));
        locations = geolocationService.getLocations(query);
        Assert.assertEquals(nodeDao.countAll(), locations.size());
        locations.forEach(l -> {
            if (l.getNodeInfo().getNodeId() == databasePopulator.getNode1().getId()) {
                Assert.assertEquals("Major", l.getSeverityInfo().getLabel());
            } else {
                Assert.assertEquals("Normal", l.getSeverityInfo().getLabel());
            }
        });
    }

    @Test
    @Transactional
    public void verifyLocationLookup() {
        // Query
        GeolocationQuery query = new GeolocationQueryBuilder()
                .withStatusCalculationStrategy(StatusCalculationStrategy.Alarms)
                .build();

        // no node has long/lat set, the lookup is empty
        Assert.assertEquals(0, geolocationService.getLocations(query).size());

        // Set dummy geolocation for all nodes
        nodeDao.findAll().forEach(n -> {
            OnmsGeolocation geolocation = n.getAssetRecord().getGeolocation();
            geolocation.setCity("Pittsboro");
            geolocation.setState("NC");
            geolocation.setCountry("USA");
            nodeDao.saveOrUpdate(n);
        });
        Assert.assertEquals(nodeDao.countAll(), geolocationService.getLocations(query).size());
    }
}
@@ -6384,849 +6384,6 @@
    %7D
-%0A%0A    @Test%0A    @Transactional%0A    public void verifyLocationLookup() %7B%0A        // Query%0A        GeolocationQuery query = new GeolocationQueryBuilder()%0A                .withStatusCalculationStrategy(StatusCalculationStrategy.Alarms)%0A                .build();%0A%0A        // no node has long/lat set, the lookup is empty%0A        Assert.assertEquals(0, geolocationService.getLocations(query).size());%0A%0A        // Set dummy geolocation for all nodes%0A        nodeDao.findAll().forEach(n -%3E %7B%0A            OnmsGeolocation geolocation = n.getAssetRecord().getGeolocation();%0A            geolocation.setCity(%22Pittsboro%22);%0A            geolocation.setState(%22NC%22);%0A            geolocation.setCountry(%22USA%22);%0A            nodeDao.saveOrUpdate(n);%0A        %7D);%0A        Assert.assertEquals(nodeDao.countAll(), geolocationService.getLocations(query).size());%0A    %7D
 %0A%7D

Remove obsolete test