Newer
Older
import it.inaf.ia2.aa.ServletRapClient;
import it.inaf.ia2.aa.data.User;
import it.inaf.oats.vospace.datamodel.NodeProperties;
Sonia Zorba
committed
import it.inaf.oats.vospace.exception.InvalidArgumentException;
import it.inaf.oats.vospace.exception.ProtocolNotSupportedException;
import it.inaf.oats.vospace.persistence.LocationDAO;
import it.inaf.oats.vospace.persistence.NodeDAO;
import it.inaf.oats.vospace.persistence.model.Location;
import it.inaf.oats.vospace.persistence.model.LocationType;
import javax.servlet.http.HttpServletRequest;
Nicola Fulvio Calabria
committed
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.DataNode;
import net.ivoa.xml.vospace.v2.Node;
import net.ivoa.xml.vospace.v2.Property;
Sonia Zorba
committed
import net.ivoa.xml.vospace.v2.Protocol;
import net.ivoa.xml.vospace.v2.Transfer;
import static org.junit.jupiter.api.Assertions.assertEquals;
Sonia Zorba
committed
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.BeforeEach;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
Nicola Fulvio Calabria
committed
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.TestPropertySource;
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(properties = {"vospace-authority=example.com!vospace", "file-service-url=http://file-service"})
public class UriServiceTest {
@MockBean
@MockBean
private LocationDAO locationDAO;
Sonia Zorba
committed
@MockBean
private ServletRapClient rapClient;
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
@MockBean
private CreateNodeController createNodeController;
@Autowired
private HttpServletRequest servletRequest;
private UriService uriService;
@TestConfiguration
public static class TestConfig {
/**
* Necessary because MockBean doesn't work with HttpServletRequest.
*/
@Bean
@Primary
public HttpServletRequest servletRequest() {
Sonia Zorba
committed
HttpServletRequest request = mock(HttpServletRequest.class);
User user = new User().setUserId("anonymous");
when(request.getUserPrincipal()).thenReturn(user);
return request;
Sonia Zorba
committed
@BeforeEach
public void init() {
Location location = new Location();
location.setType(LocationType.ASYNC);
when(locationDAO.getNodeLocation(any())).thenReturn(Optional.of(location));
}
@Test
public void testPublicUrl() {
Node node = new DataNode();
Property property = new Property();
property.setUri(NodeProperties.PUBLIC_READ_URI);
property.setValue("true");
node.getProperties().add(property);
when(nodeDAO.listNode(eq("/mydata1"))).thenReturn(Optional.of(node));
JobSummary job = getJob();
uriService.setTransferJobResult(job, uriService.getTransfer(job));
assertEquals("http://file-service/mydata1?jobId=job-id", job.getResults().get(0).getHref());
}
public void testPrivateUrl() {
Nicola Fulvio Calabria
committed
Property creator = new Property();
creator.setUri(NodeProperties.CREATOR_URI);
creator.setValue("user1");
node.getProperties().add(creator);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
Property readgroup = new Property();
readgroup.setUri(NodeProperties.GROUP_READ_URI);
readgroup.setValue("group1");
node.getProperties().add(readgroup);
when(nodeDAO.listNode(eq("/mydata1"))).thenReturn(Optional.of(node));
User user = mock(User.class);
when(user.getAccessToken()).thenReturn("<token>");
Nicola Fulvio Calabria
committed
when(user.getName()).thenReturn("user1");
when(servletRequest.getUserPrincipal()).thenReturn(user);
when(rapClient.exchangeToken(argThat(req -> {
assertEquals("<token>", req.getSubjectToken());
assertEquals("http://file-service/mydata1", req.getResource());
return true;
}), any())).thenReturn("<new-token>");
JobSummary job = getJob();
Nicola Fulvio Calabria
committed
Transfer tr = uriService.getTransfer(job);
uriService.setTransferJobResult(job, tr);
assertEquals("http://file-service/mydata1?jobId=job-id&token=<new-token>", job.getResults().get(0).getHref());
}
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
@Test
public void pushToNonexistentNode() {
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
ContainerNode node = new ContainerNode();
node.setUri("vos://example.com!vospace/mydata1");
Property creator = new Property();
creator.setUri(NodeProperties.CREATOR_URI);
creator.setValue("user1");
node.getProperties().add(creator);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
Property readgroup = new Property();
readgroup.setUri(NodeProperties.GROUP_READ_URI);
readgroup.setValue("group1");
node.getProperties().add(readgroup);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
DataNode dnode = new DataNode();
dnode.setUri("vos://example.com!vospace/mydata1/mydata2");
dnode.getProperties().add(creator);
dnode.getProperties().add(readgroup);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
Property writegroup = new Property();
writegroup.setUri(NodeProperties.GROUP_WRITE_URI);
writegroup.setValue("group1");
dnode.getProperties().add(writegroup);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
when(nodeDAO.listNode(eq("/mydata1"))).thenReturn(Optional.of(node));
when(nodeDAO.listNode(eq("/mydata1/mydata2"))).thenReturn(Optional.empty());
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
User user = mock(User.class);
when(user.getAccessToken()).thenReturn("<token>");
when(user.getName()).thenReturn("user1");
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
when(servletRequest.getUserPrincipal()).thenReturn(user);
when(rapClient.exchangeToken(argThat(req -> {
assertEquals("<token>", req.getSubjectToken());
assertEquals("http://file-service/mydata1/mydata2", req.getResource());
return true;
}), any())).thenReturn("<new-token>");
JobSummary job = getPushToVoSpaceJob();
Transfer tr = uriService.getTransfer(job);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
when(createNodeController.createNode(any(), eq(user))).thenReturn(dnode);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
uriService.setTransferJobResult(job, tr);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
verify(createNodeController, times(1)).createNode(any(), eq(user));
Sonia Zorba
committed
assertEquals("http://file-service/mydata1/mydata2?jobId=job-id2&token=<new-token>", job.getResults().get(0).getHref());
Nicola Fulvio Calabria
committed
}
@Test
public void setNodeRemoteLocationTest() {
String nodeUri = "vos://example.com!vospace/test/f1/lbtfile.fits";
String contentUri = "http://archive.lbto.org/files/lbtfile.fits";
Location location = new Location();
location.setId(5);
when(locationDAO.findPortalLocation(eq("archive.lbto.org"))).thenReturn(Optional.of(location));
uriService.setNodeRemoteLocation(nodeUri, contentUri);
verify(nodeDAO).setNodeLocation(eq("/test/f1/lbtfile.fits"), eq(5), eq("lbtfile.fits"));
}
Sonia Zorba
committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
@Test
public void testSetSyncTransferEndpointsPullFromVoSpace() {
mockPublicNode();
Transfer transfer = new Transfer();
transfer.setTarget("vos://example.com!vospace/mydata1");
transfer.setDirection("pullFromVoSpace");
Protocol protocol1 = new Protocol();
protocol1.setUri("ivo://ivoa.net/vospace/core#httpget");
transfer.getProtocols().add(protocol1);
Protocol protocol2 = new Protocol();
protocol2.setUri("invalid_protocol");
transfer.getProtocols().add(protocol2);
JobSummary job = new JobSummary();
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
assertEquals(2, transfer.getProtocols().size());
uriService.setSyncTransferEndpoints(job);
// invalid protocol is removed
assertEquals(1, transfer.getProtocols().size());
assertEquals("ivo://ivoa.net/vospace/core#httpget", transfer.getProtocols().get(0).getUri());
}
@Test
public void testSetSyncTransferEndpointsPushToVoSpace() {
Node node = mockPublicNode();
Property creator = new Property();
creator.setUri(NodeProperties.CREATOR_URI);
creator.setValue("user1");
node.getProperties().add(creator);
User user = mock(User.class);
when(user.getName()).thenReturn("user1");
when(servletRequest.getUserPrincipal()).thenReturn(user);
Transfer transfer = new Transfer();
transfer.setTarget("vos://example.com!vospace/mydata1");
transfer.setDirection("pushToVoSpace");
Protocol protocol1 = new Protocol();
protocol1.setUri("ivo://ivoa.net/vospace/core#httpput");
transfer.getProtocols().add(protocol1);
Protocol protocol2 = new Protocol();
protocol2.setUri("invalid_protocol");
transfer.getProtocols().add(protocol2);
JobSummary job = new JobSummary();
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
assertEquals(2, transfer.getProtocols().size());
uriService.setSyncTransferEndpoints(job);
// invalid protocol is removed
assertEquals(1, transfer.getProtocols().size());
assertEquals("ivo://ivoa.net/vospace/core#httpput", transfer.getProtocols().get(0).getUri());
}
@Test
public void testSetSyncTransferEndpointsUnsupportedProtocol() {
Transfer transfer = new Transfer();
transfer.setTarget("vos://example.com!vospace/mydata1");
transfer.setDirection("pullFromVoSpace");
Protocol protocol = new Protocol();
protocol.setUri("invalid_protocol");
transfer.getProtocols().add(protocol);
JobSummary job = new JobSummary();
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
try {
uriService.setSyncTransferEndpoints(job);
fail("Expected ProtocolNotSupportedException");
} catch (ProtocolNotSupportedException ex) {
}
}
@Test
public void testSetSyncTransferEndpointsNoProtocols() {
Transfer transfer = new Transfer();
transfer.setTarget("vos://example.com!vospace/mydata1");
transfer.setDirection("pullFromVoSpace");
JobSummary job = new JobSummary();
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
try {
uriService.setSyncTransferEndpoints(job);
fail("Expected InvalidArgumentException");
} catch (InvalidArgumentException ex) {
}
}
private Node mockPublicNode() {
DataNode node = new DataNode();
node.setUri("vos://example.com!vospace/mydata1");
Property publicProperty = new Property();
publicProperty.setUri(NodeProperties.PUBLIC_READ_URI);
publicProperty.setValue(String.valueOf(true));
node.getProperties().add(publicProperty);
when(nodeDAO.listNode(eq("/mydata1"))).thenReturn(Optional.of(node));
return node;
}
private JobSummary getJob() {
Transfer transfer = new Transfer();
transfer.setTarget("vos://example.com!vospace/mydata1");
Nicola Fulvio Calabria
committed
transfer.setDirection(JobService.JobType.pullFromVoSpace.toString());
JobSummary job = new JobSummary();
job.setJobId("job-id");
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
return job;
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
private JobSummary getPushToVoSpaceJob() {
Transfer transfer = new Transfer();
transfer.setTarget("vos://example.com!vospace/mydata1/mydata2");
transfer.setDirection(JobService.JobType.pushToVoSpace.toString());
JobSummary job = new JobSummary();
job.setJobId("job-id2");
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
Sonia Zorba
committed
return job;
Nicola Fulvio Calabria
committed
}