From 35daa117abf2d8e2f37c7002e5020defd5f4a486 Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane Date: Thu, 12 May 2016 12:24:23 +0200 Subject: [PATCH] Improve JUnits tests fix some errors to make them works. --- src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java | 118 +++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------- src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapController.java | 2 +- src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapInterface.java | 16 +++++++++------- src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java | 2 +- src/main/java/eu/omp/irap/vespa/epntapclient/service/Queries.java | 25 ++++++++++++++++--------- src/main/java/eu/omp/irap/vespa/epntapclient/service/ServiceCtrl.java | 49 ++++++++++++++++++++++++++++++++++++++++--------- src/main/java/eu/omp/irap/vespa/epntapclient/voresource/VOResourceCtrl.java | 59 ++++++++++++++++++++++++++--------------------------------- src/test/java/eu/omp/irap/vespa/epntapclient/EpnTapConnectionTest.java | 194 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------- 8 files changed, 268 insertions(+), 197 deletions(-) diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java index 504fbb6..acfb001 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java @@ -19,22 +19,18 @@ package eu.omp.irap.vespa.epntapclient; import java.text.ParseException; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.logging.Logger; import eu.omp.irap.vespa.epntapclient.granule.Granule; import eu.omp.irap.vespa.epntapclient.granule.GranuleCtrl; import eu.omp.irap.vespa.epntapclient.service.Queries; -import eu.omp.irap.vespa.epntapclient.service.Service; import eu.omp.irap.vespa.epntapclient.service.ServiceCore; 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.Consts; import eu.omp.irap.vespa.votable.controller.CantGetVOTableException; -import eu.omp.irap.vespa.votable.controller.VOTableController; import eu.omp.irap.vespa.votable.utils.StringJoiner; import eu.omp.irap.vespa.votable.votabledata.VOTableData; @@ -58,122 +54,74 @@ public class EpnTapConnection implements EpnTapInterface { @Override public List getEPNVOResources() throws VOResourceException { - return VOResourceCtrl.getVOResources(ServiceCore.EPNCORE); + List ivoids = VOResourceCtrl.getVOResources(ServiceCore.EPNCORE); + return VOResourceCtrl.getVOResources(ivoids); } @Override - public List getEPNVOResources(Map keywords) + public List getEPNVOResources(List keywords) throws VOResourceException { - return VOResourceCtrl.getVOResources(ServiceCore.EPNCORE, keywords); + List ivoids = VOResourceCtrl.getVOResources(ServiceCore.EPNCORE, keywords); + return VOResourceCtrl.getVOResources(ivoids); } // *** Service *** - public Service _getEPNService(String ivoid) throws CantGetVOTableException { - return ServiceCtrl.getServiceFromIvoid(ivoid); - } - @Override public VOTABLE getEPNService(String ivoid) throws CantGetVOTableException { - String query = String.format(Queries.GET_TAP_SERVICES_WHERE_IVOID, ivoid); - VOTableController ctrl = new VOTableController(Consts.DEFAULT_REGISTRY_URL, query); - ctrl.readTable(); - return ctrl.getVOTable(); + String query = String.format(Queries.SELECT_ALL_TAP_SERVICES_WHERE_IVOID, ivoid); + return ServiceCtrl.getVoTable(query); } @Override public VOTABLE getEPNService(String ivoid, List attributes) throws CantGetVOTableException { - // TODO: optimiser le nombre de requêtes - String tableName = getEPNCoreTableName(ivoid); - String query = String.format(Queries.SELECT_FROM, StringJoiner.join(attributes), tableName); - VOTableController ctrl = new VOTableController(getTAPURL(ivoid), query); - ctrl.readTable(); - return ctrl.getVOTable(); + String select = StringJoiner.join(attributes); + String query = String.format(Queries.SELECT_TAP_SERVICES_WHERE_IVOID, select, ivoid); + return ServiceCtrl.getVoTable(query); } // *** Services *** @Override - public List getEPNServices() throws VOResourceException { - List voTables = new ArrayList<>(); - List ivoids = VOResourceCtrl.getIvoidResources(ServiceCore.EPNCORE); - // TODO: optimiser le nombre de requêtes - for (String ivoid : ivoids) { - try { - String query = String.format(Queries.SELECT_ALL, getEPNCoreTableName(ivoid)); - VOTableController ctrl = new VOTableController(getTAPURL(ivoid), query); - ctrl.readTable(); - voTables.add(ctrl.getVOTable()); - } catch (CantGetVOTableException e) { - logger.info("Can not get the service " + ivoid + ", skipping..."); - } - } - return voTables; + public VOTABLE getEPNServices() throws CantGetVOTableException { + String query = String.format(Queries.SELECT_ALL_TAP_SERVICES_WHERE_CORE, + ServiceCore.EPNCORE); + return ServiceCtrl.getVoTable(query); } @Override - public List getEPNServices(List attributes) - throws CantGetVOTableException, VOResourceException { - List voTables = new ArrayList<>(); - List ivoids = VOResourceCtrl.getIvoidResources(ServiceCore.EPNCORE); - // TODO: optimiser le nombre de requêtes - for (String ivoid : ivoids) { - try { - String tableName = getEPNCoreTableName(ivoid); - String query = String.format(Queries.SELECT_FROM, StringJoiner.join(attributes), - tableName); - VOTableController ctrl = new VOTableController(getTAPURL(ivoid), query); - ctrl.readTable(); - voTables.add(ctrl.getVOTable()); - } catch (CantGetVOTableException e) { - logger.info("Can not get the service " + ivoid + ", skipping..."); - } - } - return voTables; + public VOTABLE getEPNServices(List attributes) throws CantGetVOTableException { + String select = StringJoiner.join(attributes); + String query = String.format(Queries.SELECT_TAP_SERVICES, select); + return ServiceCtrl.getVoTable(query); } @Override - public List getEPNServices(Map keywords, List attributes) - throws CantGetVOTableException, VOResourceException { - List voTables = new ArrayList<>(); - List ivoids = VOResourceCtrl.getIvoidResources(ServiceCore.EPNCORE, keywords); - // TODO: optimiser le nombre de requêtes - for (String ivoid : ivoids) { - try { - String tableName = getEPNCoreTableName(ivoid); - String query = String.format(Queries.SELECT_FROM, StringJoiner.join(attributes), - tableName); - VOTableController ctrl = new VOTableController(getTAPURL(ivoid), query); - ctrl.readTable(); - voTables.add(ctrl.getVOTable()); - } catch (CantGetVOTableException e) { - logger.info("Can not get the service " + ivoid + ", skipping..."); - } + public VOTABLE getEPNServices(List keywords, List attributes) + throws CantGetVOTableException { + attributes.add("res_subject"); + String select = StringJoiner.join(attributes); + List whereList = new ArrayList<>(); + for (String keyword : keywords) { + whereList.add("res_subject = '" + keyword + "'"); } - return voTables; + String where = StringJoiner.join(whereList, " OR "); + String query = String.format(Queries.SELECT_TAP_SERVICES_WHERE_SUBJECT, select, where); + // TODO: move this code to a new class in ServiceCtrl() + return ServiceCtrl.getVoTable(query); } // *** Getters *** @Override - public String getEPNCoreTableName(String ivoid) - throws CantGetVOTableException { - String query = String.format(Queries.GET_TAP_SERVICES_SELECT_WHERE_IVOID, "table_name", - ivoid); - VOTableController ctrl = new VOTableController(Consts.DEFAULT_REGISTRY_URL, query); - ctrl.readTable(); - return (String) ctrl.getVOTableData().getCell(0, 0); + public String getEPNCoreTableName(String ivoid) throws CantGetVOTableException { + return (String) ServiceCtrl.getParameter(ivoid, "table_name"); } @Override - public String getTAPURL(String ivoid) - throws CantGetVOTableException { - String query = String.format(Queries.GET_TAP_SERVICES_SELECT_WHERE_IVOID, "access_url", - ivoid); - VOTableController ctrl = new VOTableController(Consts.DEFAULT_REGISTRY_URL, query); - ctrl.readTable(); - return (String) ctrl.getVOTableData().getCell(0, 0); + public String getTAPURL(String ivoid) throws CantGetVOTableException { + return (String) ServiceCtrl.getParameter(ivoid, "access_url"); } // *** Queries *** diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapController.java b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapController.java index 9f0a10d..c45c6d0 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapController.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapController.java @@ -52,7 +52,7 @@ public class EpnTapController { * Method constructor, which initialize servicesController, resultsController and mainView. */ public EpnTapController() { - String query = String.format(Queries.GET_TAP_SERVICES_WHERE_CORE, ServiceCore.EPNCORE); + String query = String.format(Queries.SELECT_ALL_TAP_SERVICES_WHERE_CORE, ServiceCore.EPNCORE); servicesCtrl = new VOTableController(Consts.DEFAULT_REGISTRY_URL, query); } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapInterface.java b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapInterface.java index f47b553..e415b03 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapInterface.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapInterface.java @@ -17,7 +17,6 @@ package eu.omp.irap.vespa.epntapclient; import java.util.List; -import java.util.Map; import eu.omp.irap.vespa.epntapclient.granule.Granule; import eu.omp.irap.vespa.epntapclient.voresource.VOResourceException; @@ -44,7 +43,7 @@ public interface EpnTapInterface { * Returns a set of VOREsource elements (one per EPN-TAP service corresponding to the keywords). * The way keywords are defined is still to be defined. */ - public List getEPNVOResources(Map keywords) + public List getEPNVOResources(List keywords) throws VOResourceException; // *** Service *** @@ -53,6 +52,8 @@ public interface EpnTapInterface { * returns a VOTable containing the attributes of the corresponding service (from a predefined * list) * + * @throws VOResourceException + * @throws CantGetVOTableException * @throws IllegalAccessException * @throws CantGetXMLException * @throws CantDisplayVOTableException @@ -63,6 +64,8 @@ public interface EpnTapInterface { * returns a VOTable containing the attributes of the corresponding service (from the list of * attributes) * + * @throws VOResourceException + * @throws CantGetVOTableException * @throws CantGetXMLException * @throws CantDisplayVOTableException */ @@ -78,7 +81,7 @@ public interface EpnTapInterface { * @throws CantGetXMLException * @throws CantDisplayVOTableException */ - public List getEPNServices() throws CantGetVOTableException, VOResourceException; + public VOTABLE getEPNServices() throws CantGetVOTableException; /** * returns a VOTable containing the list of EPN-TAP services and their attributes (from the list @@ -87,8 +90,7 @@ public interface EpnTapInterface { * @throws CantGetXMLException * @throws CantDisplayVOTableException */ - public List getEPNServices(List attributes) - throws CantGetVOTableException, VOResourceException; + public VOTABLE getEPNServices(List attributes) throws CantGetVOTableException; /** * returns a VOTable containing the list of EPN-TAP services corresponding to the keywords and @@ -97,8 +99,8 @@ public interface EpnTapInterface { * @throws CantGetXMLException * @throws CantDisplayVOTableException */ - public List getEPNServices(Map keywords, List attributes) - throws CantGetVOTableException, VOResourceException; + public VOTABLE getEPNServices(List keywords, List attributes) + throws CantGetVOTableException; // *** Getters *** diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java b/src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java index 2734603..5949fc0 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java @@ -117,7 +117,7 @@ public class GranuleCtrl { } public static boolean isV2(VOTableData data) { - return !data.containsColumn("index"); + return !data.isContainingColumnName("index"); } public List getGranulesFromVOTable() throws ParseException { diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/service/Queries.java b/src/main/java/eu/omp/irap/vespa/epntapclient/service/Queries.java index e4d3599..f708ac0 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/service/Queries.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/service/Queries.java @@ -29,7 +29,7 @@ import eu.omp.irap.vespa.votable.utils.StringJoiner; */ public final class Queries { - private static final String SELECT = "SELECT short_name, res_title AS schema_title, " + private static final String SELECT = "SELECT DISTINCT short_name, res_title, " + "table_name, schema_name, ivoid, access_url "; private static final String FROM = "FROM rr.resource " @@ -49,32 +49,39 @@ public final class Queries { //@noformat /** Query to get all TAP services. */ - public static final String GET_TAP_SERVICES = + public static final String SELECT_ALL_TAP_SERVICES = Queries.SELECT + Queries.FROM + Queries.WHERE_TAP + Queries.ORDER_BY; /** Query to get TAP services which implement the specified core. */ - public static final String GET_TAP_SERVICES_WHERE_CORE = + public static final String SELECT_ALL_TAP_SERVICES_WHERE_CORE = Queries.SELECT + Queries.FROM + Queries.WHERE_TAP + "AND 1=ivo_nocasematch(detail_value, 'ivo://vopdc.obspm/std/%s%%') " + Queries.ORDER_BY; /** Query to get the TAP service with the specified ivoid. */ - public static final String GET_TAP_SERVICES_WHERE_IVOID = + public static final String SELECT_ALL_TAP_SERVICES_WHERE_IVOID = Queries.SELECT + Queries.FROM + Queries.WHERE_TAP + "AND ivoid = '%s'"; - public static final String GET_TAP_SERVICES_SELECT_WHERE_IVOID = - "SELECT %s " + Queries.FROM + Queries.WHERE_TAP + public static final String SELECT_TAP_SERVICES_WHERE_IVOID = + "SELECT DISTINCT %s " + Queries.FROM + Queries.WHERE_TAP + "AND ivoid = '%s'"; + public static final String SELECT_TAP_SERVICES = + "SELECT DISTINCT %s " + Queries.FROM + Queries.WHERE_TAP; + + public static final String SELECT_TAP_SERVICES_WHERE_SUBJECT = + "SELECT DISTINCT %s " + Queries.FROM + "NATURAL JOIN rr.res_subject " + + Queries.WHERE_TAP + "AND (%s)"; + public static final String SELECT_FROM = - "SELECT %s FROM %s"; + "SELECT DISTINCT %s FROM %s"; public static final String SELECT_ALL = - "SELECT * FROM %s"; - + "SELECT DISTINCT * FROM %s"; //@format + /** Constructor to hide the implicit public one. */ private Queries() { } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/service/ServiceCtrl.java b/src/main/java/eu/omp/irap/vespa/epntapclient/service/ServiceCtrl.java index 6199cf2..39f0b77 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/service/ServiceCtrl.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/service/ServiceCtrl.java @@ -19,6 +19,7 @@ package eu.omp.irap.vespa.epntapclient.service; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.logging.Logger; import javax.xml.datatype.XMLGregorianCalendar; @@ -27,11 +28,14 @@ import eu.omp.irap.vespa.epntapclient.voresource.model.ContentLevel; import eu.omp.irap.vespa.epntapclient.voresource.model.Creator; import eu.omp.irap.vespa.epntapclient.voresource.model.Resource; import eu.omp.irap.vespa.epntapclient.voresource.model.Type; +import eu.omp.irap.vespa.epntapclient.votable.model.Table; +import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; import eu.omp.irap.vespa.votable.Consts; import eu.omp.irap.vespa.votable.controller.CantGetVOTableException; import eu.omp.irap.vespa.votable.controller.VOTableController; import eu.omp.irap.vespa.votable.utils.StringJoiner; import eu.omp.irap.vespa.votable.votabledata.VOTableData; +import eu.omp.irap.vespa.votable.votabledata.VOTableDataParser; /** * @author N. Jourdane @@ -44,7 +48,7 @@ public class ServiceCtrl { public static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss"; - public final static Service getServiceFromResource(Resource resource) { + public final static Service getService(Resource resource) { Service service = new Service(resource.getIdentifier()); service.setTitle(resource.getTitle()); service.setShortName(resource.getShortName()); @@ -71,7 +75,7 @@ public class ServiceCtrl { return service; } - public final static List getServicesFromVOTableData(VOTableData data) { + public static final List getServices(VOTableData data) { List services = new ArrayList<>(); for (int i = 0; i < data.getNbRows(); i++) { Service service = new Service((String) data.getCell(i, "ivoid")); @@ -90,13 +94,40 @@ public class ServiceCtrl { return services; } - public static Service getServiceFromIvoid(String ivoid) throws CantGetVOTableException { - // TODO: utiliser Resource plutôt que VOTable - String query = String.format(Queries.GET_TAP_SERVICES_WHERE_IVOID, ivoid); - VOTableController ctrl = new VOTableController(Consts.DEFAULT_REGISTRY_URL, query); - ctrl.readTable(); - List services = ServiceCtrl.getServicesFromVOTableData(ctrl.getVOTableData()); - return services.get(0); + public static VOTableData getVoTableData(String query) throws CantGetVOTableException { + return getVoTableData(getVoTable(query)); + } + + public static VOTableData getVoTableData(VOTABLE voTable) throws CantGetVOTableException { + VOTableController.checkVOTable(voTable); + + Table table = (Table) voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().get(0); + VOTableDataParser dataParser = new VOTableDataParser(table); + dataParser.parseData(); + return dataParser.getData(); + } + + public static VOTABLE getVoTable(String query) throws CantGetVOTableException { + VOTableController voTableCtrl = new VOTableController(Consts.DEFAULT_REGISTRY_URL, query); + voTableCtrl.readTable(); + return voTableCtrl.getVOTable(); + } + + public static Map getParameters(String ivoid, String parameters) + throws CantGetVOTableException { + String query = String.format(Queries.SELECT_TAP_SERVICES_WHERE_IVOID, parameters, ivoid); + return getVoTableData(query).getRowMap(0); + } + + public static Map getParameters(String ivoid, List parameters) + throws CantGetVOTableException { + return getParameters(ivoid, StringJoiner.join(parameters)); + } + + public static Object getParameter(String ivoid, String parameters) + throws CantGetVOTableException { + String query = String.format(Queries.SELECT_TAP_SERVICES_WHERE_IVOID, parameters, ivoid); + return getVoTableData(query).getCell(0, 0); } private static String XMLDateToString(XMLGregorianCalendar date) { diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/voresource/VOResourceCtrl.java b/src/main/java/eu/omp/irap/vespa/epntapclient/voresource/VOResourceCtrl.java index 2c24545..ac83c0c 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/voresource/VOResourceCtrl.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/voresource/VOResourceCtrl.java @@ -53,7 +53,7 @@ public class VOResourceCtrl { private static final String GET_VORESOURCE_URL = "http://voparis-registry.obspm.fr/vo/ivoa/1/voresources.xml"; - private static final String GET_IVOID_RESOURCES_URL = "http://voparis-registry.obspm.fr/vo/ivoa/1/voresources/search"; + private static final String GET_JSONRESOURCES_URL = "http://voparis-registry.obspm.fr/vo/ivoa/1/voresources/search"; private static final int MAX_VORESOURCES = 100; @@ -67,37 +67,31 @@ public class VOResourceCtrl { private static final String NAMESPACE_TR = "http://www.ivoa.net/xml/VODataService/v1.1"; - public static List getVOResources(ServiceCore type, Map keywords) - throws VOResourceException { - List ivoids = VOResourceCtrl.getIvoidResources(type, keywords); - return VOResourceCtrl.getVOResourcesFromIvoids(ivoids); - } - - public static List getVOResources(ServiceCore type) throws VOResourceException { - List ivoids = VOResourceCtrl.getIvoidResources(type); - return VOResourceCtrl.getVOResourcesFromIvoids(ivoids); - } + public static List getVOResources(ServiceCore type) throws VOResourceException { + List ivoidResources; + Map parameters = new HashMap(); - public static List getIvoidResources(ServiceCore type) throws VOResourceException { - return VOResourceCtrl.getIvoidResources(type, new HashMap()); + parameters.put("keywords", "datamodel:\"EpnCore\""); + parameters.put("max", String.valueOf(MAX_VORESOURCES)); + String query = Network.buildQuery(GET_JSONRESOURCES_URL, parameters); + try { + ivoidResources = parseIvoidResources(Network.readJson(query)); + } catch (CantSendQueryException e) { + throw new CantGetVOResourceException(GET_VORESOURCE_URL, e); + } + logger.info("Got resources: " + StringJoiner.join(ivoidResources)); + return ivoidResources; } - public static List getIvoidResources(ServiceCore type, Map keywords) + public static List getVOResources(ServiceCore type, List keywords) throws CantGetVOResourceException { List ivoidResources; - - keywords.put("datamodel", type.toString()); - // standardid="ivo://ivoa.net/std/TAP" is not necessary - - StringJoiner keywordJoiner = new StringJoiner(" "); - for (Map.Entry keyword : keywords.entrySet()) { - keywordJoiner.add(keyword.getKey().toLowerCase() + ":\"" + keyword.getValue() + "\""); - } - Map parameters = new HashMap(); - parameters.put("keywords", keywordJoiner.toString()); - parameters.put("max", String.valueOf(VOResourceCtrl.MAX_VORESOURCES)); - String query = Network.buildQuery(GET_IVOID_RESOURCES_URL, parameters); + String subjects = StringJoiner.join(keywords); + parameters.put("keywords", "datamodel:\"EpnCore\" subjects:\"" + subjects + "\""); + parameters.put("max", String.valueOf(MAX_VORESOURCES)); + + String query = Network.buildQuery(GET_JSONRESOURCES_URL, parameters); try { ivoidResources = parseIvoidResources(Network.readJson(query)); } catch (CantSendQueryException e) { @@ -113,13 +107,13 @@ public class VOResourceCtrl { String voResourcePath; try { - VOResourceCtrl.logger.info("Trying to get VOResource '" + identifier + "'..."); - String query = Network.buildQuery(VOResourceCtrl.GET_VORESOURCE_URL, parameters); + logger.info("Trying to get VOResource '" + identifier + "'..."); + String query = Network.buildQuery(GET_VORESOURCE_URL, parameters); voResourcePath = Network.saveQuery(query); } catch (CantSendQueryException e) { - throw new CantGetVOResourceException(VOResourceCtrl.GET_VORESOURCE_URL, e); + throw new CantGetVOResourceException(GET_VORESOURCE_URL, e); } - VOResourceCtrl.logger.info("VOResource downloaded in " + voResourcePath); + logger.info("VOResource downloaded in " + voResourcePath); try { changeNamespaces(voResourcePath); @@ -140,7 +134,6 @@ public class VOResourceCtrl { } catch (JAXBException e) { throw new VOResourceIsNotValidException(voResourcePath, e); } - return voResource; } @@ -152,11 +145,11 @@ public class VOResourceCtrl { XMLUtils.changeRootAttributes(voResourcePath, attributes); } - public static List getVOResourcesFromIvoids(List ivoidResources) + public static List getVOResources(List ivoidResources) throws VOResourceException { List resources = new ArrayList<>(); for (String ivoid : ivoidResources) { - resources.add(VOResourceCtrl.getVOresource(ivoid)); + resources.add(getVOresource(ivoid)); } return resources; } diff --git a/src/test/java/eu/omp/irap/vespa/epntapclient/EpnTapConnectionTest.java b/src/test/java/eu/omp/irap/vespa/epntapclient/EpnTapConnectionTest.java index b970129..f363dd1 100644 --- a/src/test/java/eu/omp/irap/vespa/epntapclient/EpnTapConnectionTest.java +++ b/src/test/java/eu/omp/irap/vespa/epntapclient/EpnTapConnectionTest.java @@ -18,9 +18,9 @@ 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.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; @@ -28,13 +28,13 @@ 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.Field; -import eu.omp.irap.vespa.epntapclient.votable.model.Table; 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 @@ -44,10 +44,26 @@ 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"; + + // *** 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()); @@ -58,118 +74,189 @@ public class EpnTapConnectionTest { @Test public void getEPNVOResourcesTest() throws VOResourceException { + System.out.println("getEPNVOResourcesTest"); EpnTapConnection facade = new EpnTapConnection(); + List resources = facade.getEPNVOResources(); - assertEquals(13, resources.size()); - for (Resource r : resources) { - assertNotNull(r); + 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 Map keywords = new HashMap<>(); - keywords.put("shortName", "IKS"); - List resources = facade.getEPNVOResources(); - assertEquals(2, resources.size()); + final List keywords = new ArrayList<>(); + keywords.add(AMDA_SUBJECT); + List 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 epnService = facade.getEPNService("ivo://cdpp/amda"); - Table table = (Table) epnService.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().get(0); - String firstInfoVal = epnService.getRESOURCE().get(0).getINFO().get(0).getValueAttribute(); - String firstFieldName = ((Field) table.getFIELDOrPARAMOrGROUP().get(0)).getID(); + VOTABLE voTable = facade.getEPNService("ivo://cdpp/amda"); + VOTableData data = ServiceCtrl.getVoTableData(voTable); - assertEquals("http://gavo.aip.de", firstInfoVal); - assertEquals("short_name", firstFieldName); + 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 serviceAttributes = new ArrayList<>(); - serviceAttributes.add("target_name"); - serviceAttributes.add("time_min"); - serviceAttributes.add("time_max"); + final List attributes = new ArrayList<>(); + attributes.add("access_url"); + attributes.add("short_name"); - VOTABLE epnService = facade.getEPNService("ivo://cdpp/amda", serviceAttributes); - Table table = (Table) epnService.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().get(0); - String firstInfoVal = epnService.getRESOURCE().get(0).getINFO().get(0).getValueAttribute(); - String firstFieldName = ((Field) table.getFIELDOrPARAMOrGROUP().get(0)).getID(); + VOTABLE voTable = facade.getEPNService("ivo://cdpp/amda", attributes); + VOTableData data = ServiceCtrl.getVoTableData(voTable); - assertEquals("http://cdpp-epntap.cesr.fr", firstInfoVal); - assertEquals("short_name", firstFieldName); + 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 VOResourceException { + public void getEPNServicesTest() throws CantGetVOTableException { + System.out.println("getEPNServicesTest"); EpnTapConnection facade = new EpnTapConnection(); - List voTables = facade.getEPNServices(); - assertEquals(11, voTables.size()); + + 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 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 VOResourceException, CantGetVOTableException { - final List serviceAttributes = new ArrayList<>(); - serviceAttributes.add("target_name"); - serviceAttributes.add("time_min"); - serviceAttributes.add("time_max"); - + throws CantGetVOTableException { + System.out.println("getEPNServicesWithAttributesTest"); EpnTapConnection facade = new EpnTapConnection(); - List voTables = facade.getEPNServices(serviceAttributes); - String firstInfoVal = voTables.get(0).getRESOURCE().get(0).getINFO().get(0) - .getValueAttribute(); - assertEquals("http://gavo.aip.de", firstInfoVal); + final List 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 { - final List serviceAttributes = new ArrayList<>(); - serviceAttributes.add("target_name"); - serviceAttributes.add("time_min"); - serviceAttributes.add("time_max"); - - final Map keywords = new HashMap<>(); - keywords.put("shortName", "IKS"); - + System.out.println("getEPNServicesWithAttributesAndKeywordsTest"); EpnTapConnection facade = new EpnTapConnection(); - List voTables = facade.getEPNServices(keywords, serviceAttributes); - String firstInfoVal = voTables.get(0).getRESOURCE().get(0).getINFO().get(0) - .getValueAttribute(); - assertEquals("http://gavo.aip.de", firstInfoVal); + + final List attributes = new ArrayList<>(); + attributes.add("access_url"); + attributes.add("short_name"); + + final List 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("?", epnCoreTableName); + + 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(); + String tapURL = "http://voparis-tap-planeto.obspm.fr/__system__/tap/run/tap"; String query = "SELECT TOP 1 * FROM planets.epn_core"; List granules = facade.sendADQLQuery(tapURL, query); + assertNotNull("Granules list not returned.", granules); + assertTrue("No granules returned.", granules.size() > 0); assertEquals("?", granules.get(0).granuleUid); assertEquals("?", granules.get(0).timeMin); assertEquals("?", granules.get(0).processingLevel); @@ -178,12 +265,15 @@ public class EpnTapConnectionTest { @Test public void sendADQLQueryWithSchemaNameTest() throws CantGetVOTableException { + System.out.println("sendADQLQueryWithSchemaNameTest"); EpnTapConnection facade = new EpnTapConnection(); String tapURL = "http://voparis-tap-planeto.obspm.fr/__system__/tap/run/tap"; String schemaName = "amdadb.epn_core"; String query = "SELECT TOP 1 * FROM planets.epn_core"; List granules = facade.sendQuery(tapURL, schemaName, query); + assertNotNull("Granules list not returned.", granules); + assertTrue("No granules returned.", granules.size() > 0); assertEquals("?", granules.get(0).granuleUid); assertEquals("?", granules.get(0).timeMin); assertEquals("?", granules.get(0).processingLevel); -- libgit2 0.21.2