Commit 6052f216 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Updated ListNodeControllerTest

parent 40bdd54a
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -5,6 +5,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import it.inaf.ia2.aa.LoginFilter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

@SpringBootApplication
public class VospaceApplication {
@@ -13,7 +16,6 @@ public class VospaceApplication {
        SpringApplication.run(VospaceApplication.class, args);
    }

    
    @Bean
    public FilterRegistrationBean loginFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
@@ -22,4 +24,14 @@ public class VospaceApplication {
        return registration;
    }

    @Bean
    public Marshaller marshaller() {
        try {
            JAXBContext context = JAXBContext.newInstance();
            Marshaller marshaller = context.createMarshaller();
            return marshaller;
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }
}
+36 −18
Original line number Diff line number Diff line
package it.inaf.oats.vospace;

import it.inaf.oats.vospace.persistence.NodeDAO;
import org.junit.jupiter.api.BeforeEach;
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.DataNode;
import net.ivoa.xml.vospace.v2.Node;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.mockito.ArgumentMatchers.eq;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.mockito.junit.jupiter.MockitoExtension;
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.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@ExtendWith(MockitoExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ListNodeControllerTest {

    @Mock
    private NodeDAO dao;
    private static final String URI_PREFIX = "vos://example.com!vospace";

    @InjectMocks
    private ListNodeController controller;
    @MockBean
    private NodeDAO dao;

    @Autowired
    private MockMvc mockMvc;

    @BeforeEach
    public void init() {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }

    @Test
    public void testRootXml() throws Exception {

        mockMvc.perform(get("/nodes")
        when(dao.listNode(eq("/"))).thenReturn(getRootNode());

        String xml = mockMvc.perform(get("/nodes")
                .accept(MediaType.APPLICATION_XML))
                .andDo(print())
                .andExpect(status().isOk());
                .andExpect(status().isOk())
                .andReturn().getResponse().getContentAsString();

        System.out.println(xml);

        verify(dao, times(1)).listNode(eq("/"));
    }
@@ -47,6 +50,8 @@ public class ListNodeControllerTest {
    @Test
    public void testNodeXml() throws Exception {

        when(dao.listNode(eq("/mynode"))).thenReturn(getDataNode());

        mockMvc.perform(get("/nodes/mynode")
                .accept(MediaType.APPLICATION_XML))
                .andDo(print())
@@ -54,4 +59,17 @@ public class ListNodeControllerTest {

        verify(dao, times(1)).listNode(eq("/mynode"));
    }

    private Node getRootNode() {
        ContainerNode root = new ContainerNode();
        root.setUri(URI_PREFIX + "/");
        root.getNodes().add(getDataNode());
        return root;
    }

    private Node getDataNode() {
        DataNode node = new DataNode();
        node.setUri(URI_PREFIX + "/mynode");
        return node;
    }
}