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.NodeDAO;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import net.ivoa.xml.uws.v1.ExecutionPhase;
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;
@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 TapeService tapeService;
@Autowired
private MockMvc mockMvc;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
@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 testPullToVoSpace() throws Exception {
String requestBody = getResourceFileContent("pullToVoSpace.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/.*"));
verify(tapeService, times(1)).startJob(any());
}
@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());
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
verify(jobDao, times(1)).getJob(eq("123"));
}
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);
}