Skip to content
Snippets Groups Projects
Commit 09d327c9 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Configured TokenFilter. Retrieved user groups in CreateNodeController. Changes on unit tests

parent c3ec5823
No related branches found
No related tags found
No related merge requests found
package it.inaf.oats.vospace;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class BaseNodeController {
@Autowired
private HttpServletRequest servletRequest;
/**
* Slash is a special character in defining REST endpoints and trying to
* define a PathVariable containing slashes doesn't work, so the endpoint
* has been defined using "/nodes/**" instead of "/nodes/{path}" and the
* path is extracted manually parsing the request URL.
*/
protected String getPath() {
String requestURL = servletRequest.getRequestURL().toString();
String[] split = requestURL.split("/nodes/");
String path = "/";
if (split.length == 2) {
path += split[1];
}
return path;
}
}
package it.inaf.oats.vospace; package it.inaf.oats.vospace;
import it.inaf.ia2.aa.data.User;
import net.ivoa.xml.vospace.v2.Node; import net.ivoa.xml.vospace.v2.Node;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import it.inaf.oats.vospace.persistence.NodeDAO; import it.inaf.oats.vospace.persistence.NodeDAO;
import net.ivoa.xml.vospace.v2.Property;
import java.util.List; import java.util.List;
import org.springframework.web.bind.annotation.PutMapping;
@RestController @RestController
public class CreateNodeController { public class CreateNodeController extends BaseNodeController {
@Autowired @Autowired
NodeDAO node_dao; private NodeDAO nodeDao;
@PostMapping(value = "/{path}", @PutMapping(value = {"/nodes", "/nodes/**"},
consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE},
produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public Node createNode(@PathVariable("path") String path, @RequestBody Node node) { public Node createNode(@RequestBody Node node, User principal) {
System.out.println("In createNodeController");
node_dao.createNode(node);
return node;
}
private class RequestWrapper {
List<Property> nodeProperty;
String nodeId;
String nodeType;
String path = getPath();
List<String> userGroups = principal.getGroups();
nodeDao.createNode(node);
return node;
} }
} }
...@@ -12,7 +12,7 @@ import javax.servlet.http.HttpServletRequest; ...@@ -12,7 +12,7 @@ import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@RestController @RestController
public class ListNodeController { public class ListNodeController extends BaseNodeController {
@Autowired @Autowired
private NodeDAO nodeDAO; private NodeDAO nodeDAO;
...@@ -20,24 +20,7 @@ public class ListNodeController { ...@@ -20,24 +20,7 @@ public class ListNodeController {
@GetMapping(value = {"/nodes", "/nodes/**"}, @GetMapping(value = {"/nodes", "/nodes/**"},
produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_XML_VALUE}) produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_XML_VALUE})
public ResponseEntity<Node> listNode(HttpServletRequest request) { public ResponseEntity<Node> listNode(HttpServletRequest request) {
String path = getPath(request); String path = getPath();
return ResponseEntity.ok(nodeDAO.listNode(path)); return ResponseEntity.ok(nodeDAO.listNode(path));
} }
/**
* Slash is a special character in defining REST endpoints and trying to
* define a PathVariable containing slashes doesn't work, so the endpoint
* has been defined using "/nodes/**" instead of "/nodes/{path}" and the
* path is extracted manually parsing the request URL.
*/
private String getPath(HttpServletRequest request) {
String requestURL = request.getRequestURL().toString();
String[] split = requestURL.split("/nodes/");
String path = "/";
if (split.length == 2) {
path += split[1];
}
return path;
}
} }
package it.inaf.oats.vospace;
import javax.servlet.http.HttpServletRequest;
import net.ivoa.xml.vospace.v2.Node;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import it.inaf.ia2.aa.data.User;
@RestController
public class PrivateController {
@GetMapping(value = "/private",
produces = {MediaType.APPLICATION_JSON_VALUE})
public User getUser(HttpServletRequest request) {
User user = (User)request.getUserPrincipal();
return user;
}
}
...@@ -4,7 +4,7 @@ import org.springframework.boot.SpringApplication; ...@@ -4,7 +4,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import it.inaf.ia2.aa.LoginFilter; import it.inaf.ia2.aa.TokenFilter;
@SpringBootApplication @SpringBootApplication
public class VospaceApplication { public class VospaceApplication {
...@@ -14,10 +14,10 @@ public class VospaceApplication { ...@@ -14,10 +14,10 @@ public class VospaceApplication {
} }
@Bean @Bean
public FilterRegistrationBean loginFilterRegistration() { public FilterRegistrationBean tokenFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean(); FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new LoginFilter()); registration.setFilter(new TokenFilter());
registration.addUrlPatterns("/private/*"); registration.addUrlPatterns("/*");
return registration; return registration;
} }
} }
package it.inaf.oats.vospace; package it.inaf.oats.vospace;
import it.inaf.oats.vospace.persistence.NodeDAO;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import net.ivoa.xml.vospace.v2.Property; import net.ivoa.xml.vospace.v2.Property;
import net.ivoa.xml.vospace.v2.UnstructuredDataNode; import net.ivoa.xml.vospace.v2.UnstructuredDataNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import org.mockito.InjectMocks;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.mockito.ArgumentMatchers.any;
import org.junit.jupiter.api.Disabled; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@ExtendWith(MockitoExtension.class) import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
@SpringBootTest
@AutoConfigureMockMvc
public class CreateNodeControllerTest { public class CreateNodeControllerTest {
@InjectMocks @MockBean
@Spy private NodeDAO nodeDao;
@SpyBean
@Autowired
private CreateNodeController controller; private CreateNodeController controller;
@Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@BeforeEach
public void init() {
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test @Test
@Disabled
public void testFromJsonToXml() throws Exception { public void testFromJsonToXml() throws Exception {
String requestBody = getResourceFileContent("create-unstructured-data-node.json"); String requestBody = getResourceFileContent("create-unstructured-data-node.json");
mockMvc.perform(post("/mypath") mockMvc.perform(put("/nodes/mydata1")
.content(requestBody) .content(requestBody)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_XML)) .accept(MediaType.APPLICATION_XML))
...@@ -51,11 +49,10 @@ public class CreateNodeControllerTest { ...@@ -51,11 +49,10 @@ public class CreateNodeControllerTest {
} }
@Test @Test
@Disabled
public void testFromXmlToJson() throws Exception { public void testFromXmlToJson() throws Exception {
String requestBody = getResourceFileContent("create-unstructured-data-node.xml"); String requestBody = getResourceFileContent("create-unstructured-data-node.xml");
mockMvc.perform(post("/mypath") mockMvc.perform(put("/nodes/mydata1")
.content(requestBody) .content(requestBody)
.contentType(MediaType.APPLICATION_XML) .contentType(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_JSON)) .accept(MediaType.APPLICATION_JSON))
...@@ -66,11 +63,10 @@ public class CreateNodeControllerTest { ...@@ -66,11 +63,10 @@ public class CreateNodeControllerTest {
} }
@Test @Test
@Disabled
public void testFromXmlToXml() throws Exception { public void testFromXmlToXml() throws Exception {
String requestBody = getResourceFileContent("create-unstructured-data-node.xml"); String requestBody = getResourceFileContent("create-unstructured-data-node.xml");
mockMvc.perform(post("/mypath") mockMvc.perform(put("/nodes/mydata1")
.content(requestBody) .content(requestBody)
.contentType(MediaType.APPLICATION_XML) .contentType(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_XML)) .accept(MediaType.APPLICATION_XML))
...@@ -81,11 +77,10 @@ public class CreateNodeControllerTest { ...@@ -81,11 +77,10 @@ public class CreateNodeControllerTest {
} }
@Test @Test
@Disabled
public void testFromJsonToJson() throws Exception { public void testFromJsonToJson() throws Exception {
String requestBody = getResourceFileContent("create-unstructured-data-node.json"); String requestBody = getResourceFileContent("create-unstructured-data-node.json");
mockMvc.perform(post("/mypath") mockMvc.perform(put("/nodes/mydata1")
.content(requestBody) .content(requestBody)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)) .accept(MediaType.APPLICATION_JSON))
...@@ -96,14 +91,14 @@ public class CreateNodeControllerTest { ...@@ -96,14 +91,14 @@ public class CreateNodeControllerTest {
} }
private void verifyArguments() { private void verifyArguments() {
verify(controller).createNode(eq("mypath"), verify(controller).createNode(
argThat(node -> { argThat(node -> {
UnstructuredDataNode udn = (UnstructuredDataNode) node; UnstructuredDataNode udn = (UnstructuredDataNode) node;
Property property = udn.getProperties().getProperty().get(0); Property property = udn.getProperties().getProperty().get(0);
return "vos:UnstructuredDataNode".equals(udn.getType()) return "vos:UnstructuredDataNode".equals(udn.getType())
&& "test value".equals(property.getValue()) && "test value".equals(property.getValue())
&& "ivo://ivoa.net/vospace/core#description".equals(property.getUri()); && "ivo://ivoa.net/vospace/core#description".equals(property.getUri());
})); }), any());
} }
protected static String getResourceFileContent(String fileName) throws Exception { protected static String getResourceFileContent(String fileName) throws Exception {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment