Newer
Older
import it.inaf.ia2.aa.data.User;
import static it.inaf.oats.vospace.VOSpaceXmlTestUtil.loadDocument;
import it.inaf.oats.vospace.persistence.JobDAO;
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 it.inaf.oats.vospace.persistence.model.Storage;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import net.ivoa.xml.uws.v1.ExecutionPhase;
import net.ivoa.xml.uws.v1.Jobs;
import net.ivoa.xml.uws.v1.ShortJobDescription;
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.Protocol;
import net.ivoa.xml.vospace.v2.Transfer;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.matchesPattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.w3c.dom.Document;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import static org.mockito.ArgumentMatchers.argThat;
@SpringBootTest
@AutoConfigureMockMvc
@ContextConfiguration(classes = {TokenFilterConfig.class})
@TestPropertySource(properties = "spring.main.allow-bean-definition-overriding=true")
public class TransferControllerTest {
@MockBean
private JobDAO jobDao;
@MockBean
private NodeDAO nodeDao;
@MockBean
private LocationDAO locationDao;
@MockBean
private AsyncTransferService asyncTransfService;
@Autowired
private MockMvc mockMvc;
@BeforeEach
public void init() {
Location asyncLocation = new Location();
asyncLocation.setType(LocationType.ASYNC);
asyncLocation.setId(1);
when(locationDao.getNodeLocation(eq("/mynode"))).thenReturn(Optional.of(asyncLocation));
Location portalLocation = new Location();
portalLocation.setType(LocationType.PORTAL);
portalLocation.setId(2);
Storage portalStorage = new Storage();
portalStorage.setHostname("archive.lbto.org");
portalStorage.setBasePath("/files");
portalLocation.setSource(portalStorage);
when(locationDao.getNodeLocation(eq("/portalnode"))).thenReturn(Optional.of(portalLocation));
when(locationDao.findPortalLocation(any())).thenReturn(Optional.of(portalLocation));
}
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
@Test
public void testPullFromVoSpaceAsync() throws Exception {
Node node = mockPublicDataNode();
when(nodeDao.listNode(eq("/mynode"))).thenReturn(Optional.of(node));
String requestBody = getResourceFileContent("pullFromVoSpace.xml");
String redirect = mockMvc.perform(post("/transfers?PHASE=RUN")
.content(requestBody)
.contentType(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(status().is3xxRedirection())
.andReturn().getResponse().getHeader("Location");
assertThat(redirect, matchesPattern("^/transfers/.*"));
}
@Test
public void testPullFromVoSpaceSync() throws Exception {
Node node = mockPublicDataNode();
when(nodeDao.listNode(eq("/mynode"))).thenReturn(Optional.of(node));
String requestBody = getResourceFileContent("pullFromVoSpace.xml");
String redirect = mockMvc.perform(post("/synctrans")
.content(requestBody)
.contentType(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(status().is3xxRedirection())
.andReturn().getResponse().getHeader("Location");
assertThat(redirect, matchesPattern("^/transfers/.*/results/transferDetails"));
}
@Test
public void testPullToVoSpaceTape() throws Exception {
testPullToVoSpace("/mynode", getResourceFileContent("pullToVoSpace-tape.xml"));
verify(asyncTransfService, times(1)).startJob(any());
}
@Test
public void testPullToVoSpacePortal() throws Exception {
when(nodeDao.getNodeOsName(eq("/portalnode"))).thenReturn("file.fits");
testPullToVoSpace("/portalnode", getResourceFileContent("pullToVoSpace-portal.xml"));
verify(nodeDao, times(1)).setNodeLocation(eq("/portalnode"), eq(2), eq("lbcr.20130512.060722.fits.gz"));
verify(jobDao, times(1)).updateJob(argThat(j -> {
assertTrue(j.getResults().get(0).getHref().startsWith("http://archive.lbto.org"));
assertEquals(ExecutionPhase.COMPLETED, j.getPhase());
return true;
}));
}
private void testPullToVoSpace(String path, String requestBody) throws Exception {
Node node = mockPublicDataNode();
when(nodeDao.listNode(eq(path))).thenReturn(Optional.of(node));
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
String redirect = mockMvc.perform(post("/transfers?PHASE=RUN")
.content(requestBody)
.contentType(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(status().is3xxRedirection())
.andReturn().getResponse().getHeader("Location");
assertThat(redirect, matchesPattern("^/transfers/.*"));
}
@Test
public void testSetJobPhase() throws Exception {
Node node = mockPublicDataNode();
when(nodeDao.listNode(eq("/mynode"))).thenReturn(Optional.of(node));
JobSummary job = getFakePendingJob();
when(jobDao.getJob(eq("123"))).thenReturn(Optional.of(job));
User user = new User();
user.setUserId("ownerId");
String redirect = mockMvc.perform(post("/transfers/123/phase")
.header("Authorization", "Bearer user1_token")
.param("PHASE", "RUN")
.accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(status().is3xxRedirection())
.andReturn().getResponse().getHeader("Location");
verify(jobDao, times(1)).updateJob(any());
assertThat(redirect, matchesPattern("^/transfers/.*"));
}
@Test
public void testGetTransferDetails() throws Exception {
JobSummary job = getFakePendingJob();
when(jobDao.getJob(eq("123"))).thenReturn(Optional.of(job));
mockMvc.perform(get("/transfers/123/results/transferDetails")
.header("Authorization", "Bearer user1_token")
.accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(status().isOk());
}
@Test
public void testGetJobPhase() throws Exception {
JobSummary job = getFakePendingJob();
when(jobDao.getJob(eq("123"))).thenReturn(Optional.of(job));
String phase = mockMvc.perform(get("/transfers/123/phase")
.header("Authorization", "Bearer user1_token"))
.andDo(print())
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
assertEquals("PENDING", phase);
}
private Node mockPublicDataNode() {
Node node = new DataNode();
Property property = new Property();
property.setUri("ivo://ivoa.net/vospace/core#publicread");
property.setValue("true");
node.getProperties().add(property);
return node;
}
@Test
public void testGetJob() throws Exception {
JobSummary job = new JobSummary();
when(jobDao.getJob(eq("123"))).thenReturn(Optional.of(job));
String xml = mockMvc.perform(get("/transfers/123")
.accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
Document doc = loadDocument(xml);
assertEquals("uws:job", doc.getDocumentElement().getNodeName());
verify(jobDao, times(1)).getJob(eq("123"));
}
@Test
public void testGetJobs() throws Exception {
when(jobDao.getJobs(eq("user1"), any(), any(), any(), any()))
.thenReturn(this.getFakeJobs());
mockMvc.perform(get("/transfers")
.header("Authorization", "Bearer user1_token")
.param("LAST", "-3")
.accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(status().is4xxClientError());
String xml2 = mockMvc.perform(get("/transfers")
.header("Authorization", "Bearer user1_token")
.accept(MediaType.APPLICATION_XML))
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
.andDo(print())
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
}
private Jobs getFakeJobs() {
Jobs jobs = new Jobs();
jobs.setVersion("1.1");
List<ShortJobDescription> sjdList = jobs.getJobref();
sjdList.add(getFakeSJD1());
return jobs;
}
private ShortJobDescription getFakeSJD1() {
ShortJobDescription sjd = new ShortJobDescription();
sjd.setId("pippo1");
sjd.setPhase(ExecutionPhase.QUEUED);
sjd.setOwnerId("user1");
sjd.setType(JobService.JobType.pullFromVoSpace.toString());
LocalDateTime now = LocalDateTime.now();
Timestamp ts = Timestamp.valueOf(now);
sjd.setCreationTime(JobDAO.toXMLGregorianCalendar(ts));
return sjd;
}
private JobSummary getFakePendingJob() {
JobSummary job = new JobSummary();
job.setPhase(ExecutionPhase.PENDING);
job.setOwnerId("user1");
Transfer transfer = new Transfer();
transfer.setDirection("pullFromVoSpace");
transfer.setTarget("vos://example.com!vospace/mynode");
Protocol protocol = new Protocol();
protocol.setUri("ivo://ivoa.net/vospace/core#httpget");
transfer.getProtocols().add(protocol);
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
return job;
}
protected static String getResourceFileContent(String fileName) throws Exception {
try (InputStream in = TransferControllerTest.class.getClassLoader().getResourceAsStream(fileName)) {
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}