Blame view

src/test/java/eu/omp/irap/vespa/epntapclient/EpnTapConnectionTest.java 11 KB
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * 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;
35daa117   Nathanael Jourdane   Improve JUnits te...
21
import static org.junit.Assert.assertTrue;
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
22
23

import java.util.ArrayList;
99642cfe   Nathanael Jourdane   Improve unit test...
24
import java.util.Date;
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
25
26
27
28
29
30
31
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;
35daa117   Nathanael Jourdane   Improve JUnits te...
32
import eu.omp.irap.vespa.epntapclient.service.ServiceCtrl;
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
33
34
35
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;
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
36
37
import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE;
import eu.omp.irap.vespa.votable.controller.CantGetVOTableException;
35daa117   Nathanael Jourdane   Improve JUnits te...
38
import eu.omp.irap.vespa.votable.votabledata.VOTableData;
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
39
40
41
42
43
44
45
46
47

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

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

35daa117   Nathanael Jourdane   Improve JUnits te...
48
49
50
51
52
53
54
55
56
57
58
59
	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";

99642cfe   Nathanael Jourdane   Improve unit test...
60
61
62
63
64
	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";
35daa117   Nathanael Jourdane   Improve JUnits te...
65
66
	// *** VOResources ***

5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
67
68
69

	@Test
	public void getVOResourceTest() throws VOResourceException {
35daa117   Nathanael Jourdane   Improve JUnits te...
70
		System.out.println("getVOResourceTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
71
		EpnTapConnection facade = new EpnTapConnection();
35daa117   Nathanael Jourdane   Improve JUnits te...
72

5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
73
74
75
76
77
78
79
80
81
82
		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 {
35daa117   Nathanael Jourdane   Improve JUnits te...
83
		System.out.println("getEPNVOResourcesTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
84
		EpnTapConnection facade = new EpnTapConnection();
35daa117   Nathanael Jourdane   Improve JUnits te...
85

5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
86
87
		List<Resource> resources = facade.getEPNVOResources();

35daa117   Nathanael Jourdane   Improve JUnits te...
88
89
90
91
92
93
94
95
96
97
98
99
		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;
			}
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
100
		}
35daa117   Nathanael Jourdane   Improve JUnits te...
101
102
103
104
		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());
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
105
106
107
108
	}

	@Test
	public void getEPNVOResourcesWithKeywordsTest() throws VOResourceException {
35daa117   Nathanael Jourdane   Improve JUnits te...
109
		System.out.println("getEPNVOResourcesWithKeywordsTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
110
		EpnTapConnection facade = new EpnTapConnection();
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
111

35daa117   Nathanael Jourdane   Improve JUnits te...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
		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);
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
126
127
	}

35daa117   Nathanael Jourdane   Improve JUnits te...
128
129
	// *** Services ***

5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
130
131
	@Test
	public void getEPNServiceTest() throws CantGetVOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
132
		System.out.println("getEPNServiceTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
133
134
		EpnTapConnection facade = new EpnTapConnection();

35daa117   Nathanael Jourdane   Improve JUnits te...
135
136
		VOTABLE voTable = facade.getEPNService("ivo://cdpp/amda");
		VOTableData data = ServiceCtrl.getVoTableData(voTable);
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
137

35daa117   Nathanael Jourdane   Improve JUnits te...
138
139
140
141
142
143
144
		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"));
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
145
146
147
148
	}

	@Test
	public void getEPNServiceWithAttributesTest() throws CantGetVOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
149
		System.out.println("getEPNServiceWithAttributesTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
150
151
		EpnTapConnection facade = new EpnTapConnection();

35daa117   Nathanael Jourdane   Improve JUnits te...
152
153
154
		final List<String> attributes = new ArrayList<>();
		attributes.add("access_url");
		attributes.add("short_name");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
155

35daa117   Nathanael Jourdane   Improve JUnits te...
156
157
		VOTABLE voTable = facade.getEPNService("ivo://cdpp/amda", attributes);
		VOTableData data = ServiceCtrl.getVoTableData(voTable);
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
158

35daa117   Nathanael Jourdane   Improve JUnits te...
159
160
161
162
163
164
		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"));
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
165
166
167
	}

	@Test
35daa117   Nathanael Jourdane   Improve JUnits te...
168
169
	public void getEPNServicesTest() throws CantGetVOTableException {
		System.out.println("getEPNServicesTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
170
		EpnTapConnection facade = new EpnTapConnection();
35daa117   Nathanael Jourdane   Improve JUnits te...
171
172
173
174
175
176
177
178
179
180

		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"));
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
181
182
183
184
	}

	@Test
	public void getEPNServicesWithAttributesTest()
35daa117   Nathanael Jourdane   Improve JUnits te...
185
186
			throws CantGetVOTableException {
		System.out.println("getEPNServicesWithAttributesTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
187
		EpnTapConnection facade = new EpnTapConnection();
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
188

35daa117   Nathanael Jourdane   Improve JUnits te...
189
190
191
192
193
194
195
196
197
198
199
200
		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"));
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
201
202
203
204
205
	}

	@Test
	public void getEPNServicesWithAttributesAndKeywordsTest()
			throws CantGetVOTableException, VOResourceException {
35daa117   Nathanael Jourdane   Improve JUnits te...
206
		System.out.println("getEPNServicesWithAttributesAndKeywordsTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
207
		EpnTapConnection facade = new EpnTapConnection();
35daa117   Nathanael Jourdane   Improve JUnits te...
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

		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"));
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
229
230
	}

35daa117   Nathanael Jourdane   Improve JUnits te...
231
232
	// *** Getters ***

5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
233
234
	@Test
	public void getEPNCoreTableNameTest() throws CantGetVOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
235
		System.out.println("getEPNCoreTableNameTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
236
		EpnTapConnection facade = new EpnTapConnection();
35daa117   Nathanael Jourdane   Improve JUnits te...
237

5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
238
		String epnCoreTableName = facade.getEPNCoreTableName("ivo://cdpp/amda");
35daa117   Nathanael Jourdane   Improve JUnits te...
239
240

		assertEquals("amdadb.epn_core", epnCoreTableName);
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
241
242
243
244
	}

	@Test
	public void getTAPURLTest() throws CantGetVOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
245
		System.out.println("getTAPURLTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
246
		EpnTapConnection facade = new EpnTapConnection();
35daa117   Nathanael Jourdane   Improve JUnits te...
247

5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
248
		String tapURL = facade.getTAPURL("ivo://cdpp/amda");
35daa117   Nathanael Jourdane   Improve JUnits te...
249

5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
250
251
252
		assertEquals("http://cdpp-epntap.cesr.fr/__system__/tap/run/tap", tapURL);
	}

35daa117   Nathanael Jourdane   Improve JUnits te...
253
254
	// *** Queries ***

5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
255
256
	@Test
	public void sendADQLQueryTest() throws CantGetVOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
257
		System.out.println("sendADQLQueryTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
258
		EpnTapConnection facade = new EpnTapConnection();
35daa117   Nathanael Jourdane   Improve JUnits te...
259

99642cfe   Nathanael Jourdane   Improve unit test...
260
261
		List<Granule> granules = facade.sendADQLQuery(SERVICE_EPNCOREV2_ACCESS_URL,
				SERVICE_EPNCOREV2_QUERY);
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
262

35daa117   Nathanael Jourdane   Improve JUnits te...
263
		assertNotNull("Granules list not returned.", granules);
99642cfe   Nathanael Jourdane   Improve unit test...
264
265
266
267
268
269
270
271
272
273
274
		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);
		assertEquals(mars.creationDate, new Date(2015, 8, 20));
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
275
276
277
278
	}

	@Test
	public void sendADQLQueryWithSchemaNameTest() throws CantGetVOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
279
		System.out.println("sendADQLQueryWithSchemaNameTest");
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
280
		EpnTapConnection facade = new EpnTapConnection();
99642cfe   Nathanael Jourdane   Improve unit test...
281
282
		List<Granule> granules = facade.sendQuery(SERVICE_EPNCOREV2_ACCESS_URL,
				SERVICE_EPNCOREV2_TABLE_NAME, SERVICE_EPNCOREV2_QUERY);
5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
283

35daa117   Nathanael Jourdane   Improve JUnits te...
284
		assertNotNull("Granules list not returned.", granules);
99642cfe   Nathanael Jourdane   Improve unit test...
285
286
287
288
289
290
291
292
293
294
295
296
		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(mars.obsId, 4);
		assertEquals((int) mars.processingLevel, 5);
		assertEquals(mars.creationDate, "2015-08-20");

5d00d3ff   Nathanael Jourdane   #5 Add JUnit test...
297
298
	}
}