Newer
Older
import it.inaf.ia2.aa.ServletRapClient;
import it.inaf.ia2.aa.data.User;
import it.inaf.oats.vospace.datamodel.NodeProperties;
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;
import net.ivoa.xml.uws.v1.JobSummary;
import net.ivoa.xml.vospace.v2.DataNode;
import net.ivoa.xml.vospace.v2.Node;
import net.ivoa.xml.vospace.v2.Property;
import net.ivoa.xml.vospace.v2.Transfer;
import static org.junit.jupiter.api.Assertions.assertEquals;
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;
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;
@MockBean
private ServletRapClient rapClient;
@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() {
return mock(HttpServletRequest.class);
}
}
@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);
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());
}
@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"));
}
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;