EpnTapConnectionTest.java 10.9 KB
/*
 * This file is a part of EpnTAPClient.
 * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
 * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
 * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
 *
 * This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version. This program 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 General Public License for more details. You should have received a copy of
 * the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package eu.omp.irap.vespa.epntapclient;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.junit.Test;

import eu.omp.irap.vespa.epntapclient.granule.Granule;
import eu.omp.irap.vespa.epntapclient.service.ServiceCtrl;
import eu.omp.irap.vespa.epntapclient.voresource.VOResourceCtrl;
import eu.omp.irap.vespa.epntapclient.voresource.VOResourceException;
import eu.omp.irap.vespa.epntapclient.voresource.model.Resource;
import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE;
import eu.omp.irap.vespa.votable.controller.CantGetVOTableException;
import eu.omp.irap.vespa.votable.votabledata.VOTableData;

/**
 * @author N. Jourdane
 */
public class EpnTapConnectionTest {

	/** The logger for the class Main. */
	private static final Logger logger = Logger.getLogger(EpnTapConnectionTest.class.getName());

	private static final String AMDA_IVOID = "ivo://cdpp/amda";

	private static final String AMDA_SHORT_NAME = "AMDA";

	private static final String AMDA_ACCESS_URL = "http://cdpp-epntap.cesr.fr/__system__/tap/run/tap";

	private static final String AMDA_SUBJECT = "Space plasmas";

	private static final String APIS_IVOID = "ivo://vopdc.obspm/lesia/apis/epn";

	private static final String APIS_SHORT_NAME = "APIS";

	private static final String SERVICE_EPNCOREV2_ACCESS_URL = "http://voparis-tap-planeto.obspm.fr/__system__/tap/run/tap";

	private static final String SERVICE_EPNCOREV2_QUERY = "SELECT * FROM planets.epn_core";

	private static final String SERVICE_EPNCOREV2_TABLE_NAME = "planets.epn_core";
	// *** VOResources ***


	@Test
	public void getVOResourceTest() throws VOResourceException {
		System.out.println("getVOResourceTest");
		EpnTapConnection facade = new EpnTapConnection();

		Resource resource = VOResourceCtrl.getVOresource("ivo://cdpp/amda");

		assertEquals("AMDA", resource.getShortName());
		assertEquals("CDPP AMDA DataBase", resource.getTitle());
		assertEquals("Centre de Données de la Physique des Plasmas",
				resource.getCuration().getCreator().get(0).getName().getValue());
	}

	@Test
	public void getEPNVOResourcesTest() throws VOResourceException {
		System.out.println("getEPNVOResourcesTest");
		EpnTapConnection facade = new EpnTapConnection();

		List<Resource> resources = facade.getEPNVOResources();

		int nbResources = resources.size();
		assertTrue(nbResources + " ∉ [13;20]", nbResources >= 13 && nbResources <= 20);

		Resource amda = null;
		Resource apis = null;
		for (Resource resource : resources) {
			if (AMDA_IVOID.equals(resource.getIdentifier())) {
				amda = resource;
			}
			if (APIS_IVOID.equals(resource.getIdentifier())) {
				apis = resource;
			}
		}
		assertNotNull("AMDA resource should be present.", amda);
		assertNotNull("APIS resource should be present.", apis);
		assertEquals(AMDA_SHORT_NAME, amda.getShortName());
		assertEquals(APIS_SHORT_NAME, apis.getShortName());
	}

	@Test
	public void getEPNVOResourcesWithKeywordsTest() throws VOResourceException {
		System.out.println("getEPNVOResourcesWithKeywordsTest");
		EpnTapConnection facade = new EpnTapConnection();

		final List<String> keywords = new ArrayList<>();
		keywords.add(AMDA_SUBJECT);
		List<Resource> resources = facade.getEPNVOResources(keywords);

		assertEquals(resources.size(), 1);

		Resource amda = null;
		for (Resource resource : resources) {
			System.out.println(resource.getIdentifier());
			if (AMDA_IVOID.equals(resource.getIdentifier())) {
				amda = resource;
			}
		}
		assertNotNull("AMDA resource should be present.", amda);
	}

	// *** Services ***

	@Test
	public void getEPNServiceTest() throws CantGetVOTableException {
		System.out.println("getEPNServiceTest");
		EpnTapConnection facade = new EpnTapConnection();

		VOTABLE voTable = facade.getEPNService("ivo://cdpp/amda");
		VOTableData data = ServiceCtrl.getVoTableData(voTable);

		assertEquals(1, data.getNbRows());
		assertTrue("Column name ivoid not found.", data.isContainingColumnName("ivoid"));
		assertTrue("Column name short_name not found.", data.isContainingColumnName("access_url"));
		assertTrue("Column name short_name not found.", data.isContainingColumnName("short_name"));
		assertEquals(AMDA_IVOID, data.getCell(0, "ivoid"));
		assertEquals(AMDA_ACCESS_URL, data.getCell(0, "access_url"));
		assertEquals(AMDA_SHORT_NAME, data.getCell(0, "short_name"));
	}

	@Test
	public void getEPNServiceWithAttributesTest() throws CantGetVOTableException {
		System.out.println("getEPNServiceWithAttributesTest");
		EpnTapConnection facade = new EpnTapConnection();

		final List<String> attributes = new ArrayList<>();
		attributes.add("access_url");
		attributes.add("short_name");

		VOTABLE voTable = facade.getEPNService("ivo://cdpp/amda", attributes);
		VOTableData data = ServiceCtrl.getVoTableData(voTable);

		assertEquals(1, data.getNbRows());
		assertEquals(2, data.getNbColumns());
		assertTrue("Column name short_name not found.", data.isContainingColumnName("access_url"));
		assertTrue("Column name short_name not found.", data.isContainingColumnName("short_name"));
		assertEquals(AMDA_ACCESS_URL, data.getCell(0, "access_url"));
		assertEquals(AMDA_SHORT_NAME, data.getCell(0, "short_name"));
	}

	@Test
	public void getEPNServicesTest() throws CantGetVOTableException {
		System.out.println("getEPNServicesTest");
		EpnTapConnection facade = new EpnTapConnection();

		VOTABLE voTable = facade.getEPNServices();
		VOTableData data = ServiceCtrl.getVoTableData(voTable);
		int nbServices = data.getNbRows();
		assertTrue(nbServices + " ∉ [13;20].", nbServices >= 13 && nbServices <= 20);
		assertTrue("Column name ivoid not found.", data.isContainingColumnName("short_name"));
		assertTrue("AMDA ivoid not found.", data.isColumnContainingValue("ivoid", AMDA_IVOID));
		Map<String, Object> amda = data.getRowMapByValue("ivoid", AMDA_IVOID);
		assertEquals(AMDA_ACCESS_URL, data.getCell(0, "access_url"));
		assertEquals(AMDA_SHORT_NAME, data.getCell(0, "short_name"));
	}

	@Test
	public void getEPNServicesWithAttributesTest()
			throws CantGetVOTableException {
		System.out.println("getEPNServicesWithAttributesTest");
		EpnTapConnection facade = new EpnTapConnection();

		final List<String> attributes = new ArrayList<>();
		attributes.add("access_url");
		attributes.add("short_name");

		VOTABLE voTable = facade.getEPNService("ivo://cdpp/amda", attributes);
		VOTableData data = ServiceCtrl.getVoTableData(voTable);

		assertEquals(2, data.getNbColumns());
		assertTrue("Column name ivoid not found.", data.isContainingColumnName("access_url"));
		assertTrue("Column name ivoid not found.", data.isContainingColumnName("short_name"));
		assertEquals(AMDA_ACCESS_URL, data.getCell(0, "access_url"));
		assertEquals(AMDA_SHORT_NAME, data.getCell(0, "short_name"));
	}

	@Test
	public void getEPNServicesWithAttributesAndKeywordsTest()
			throws CantGetVOTableException, VOResourceException {
		System.out.println("getEPNServicesWithAttributesAndKeywordsTest");
		EpnTapConnection facade = new EpnTapConnection();

		final List<String> attributes = new ArrayList<>();
		attributes.add("access_url");
		attributes.add("short_name");

		final List<String> keywords = new ArrayList<>();
		keywords.add(AMDA_SUBJECT);

		VOTABLE voTable = facade.getEPNServices(keywords, attributes);
		VOTableData data = ServiceCtrl.getVoTableData(voTable);

		assertEquals(3, data.getNbColumns());
		// TODO: Should be return only 2 columns, because keywords do not contains res_subject.
		assertTrue("Column name access_url not found.", data.isContainingColumnName("access_url"));
		assertTrue("Column name short_name not found.", data.isContainingColumnName("short_name"));
		assertTrue("AMDA short_name not found.",
				data.isColumnContainingValue("short_name", AMDA_SHORT_NAME));
		assertTrue("AMDA access_url not found.",
				data.isColumnContainingValue("access_url", AMDA_ACCESS_URL));
		assertEquals(AMDA_SHORT_NAME, data.getCell(0, "short_name"));
		assertEquals(AMDA_ACCESS_URL, data.getCell(0, "access_url"));
	}

	// *** Getters ***

	@Test
	public void getEPNCoreTableNameTest() throws CantGetVOTableException {
		System.out.println("getEPNCoreTableNameTest");
		EpnTapConnection facade = new EpnTapConnection();

		String epnCoreTableName = facade.getEPNCoreTableName("ivo://cdpp/amda");

		assertEquals("amdadb.epn_core", epnCoreTableName);
	}

	@Test
	public void getTAPURLTest() throws CantGetVOTableException {
		System.out.println("getTAPURLTest");
		EpnTapConnection facade = new EpnTapConnection();

		String tapURL = facade.getTAPURL("ivo://cdpp/amda");

		assertEquals("http://cdpp-epntap.cesr.fr/__system__/tap/run/tap", tapURL);
	}

	// *** Queries ***

	@Test
	public void sendADQLQueryTest() throws CantGetVOTableException {
		System.out.println("sendADQLQueryTest");
		EpnTapConnection facade = new EpnTapConnection();

		List<Granule> granules = facade.sendADQLQuery(SERVICE_EPNCOREV2_ACCESS_URL,
				SERVICE_EPNCOREV2_QUERY);

		assertNotNull("Granules list not returned.", granules);
		assertEquals(8, granules.size());
		Granule mars = null;
		for (Granule granule : granules) {
			if ("Mars".equals(granule.granuleUid)) {
				mars = granule;
			}
		}
		assertNotNull("granule 'Mars' is not found.", mars);
		assertEquals("4", mars.obsId);
		assertEquals(new Integer(5), mars.processingLevel);
	}

	@Test
	public void sendADQLQueryWithSchemaNameTest() throws CantGetVOTableException {
		System.out.println("sendADQLQueryWithSchemaNameTest");
		EpnTapConnection facade = new EpnTapConnection();
		List<Granule> granules = facade.sendQuery(SERVICE_EPNCOREV2_ACCESS_URL,
				SERVICE_EPNCOREV2_TABLE_NAME, Query.GET_SCENE_FROM_TARGET_AND_TIME_INTERVAL);

		assertNotNull("Granules list not returned.", granules);
		assertEquals(8, granules.size());
		Granule mars = null;
		for (Granule granule : granules) {
			if ("Mars".equals(granule.granuleUid)) {
				mars = granule;
			}
		}
		assertNotNull("granule 'Mars' is not found.", mars);
		assertEquals("4", mars.obsId);
		assertEquals(new Integer(5), mars.processingLevel);
	}
}