lundi 20 janvier 2020

Unit test endpoint using Spring-test: com.disney.ops.wfm.lsp.api.lsp.template.api.BanquetLaborHoursApiTest

I am trying to test the next endpoint, it is not a SpringBoot Application, in the past I had worked only with springBoot Apps using spring-boot-starter-test, in this case I am not pretty sure what dependency I have to use to test this endpoint:

package com.disney.ops.wfm.lsp.banquet.labor.hours.api;

import com.disney.ops.wfm.lsp.banquet.labor.hours.domain.FilesService;
import com.disney.ops.wfm.lsp.banquet.labor.hours.domain.SFTPServiceImpl;

import com.disney.ops.wfm.lsp.api.lsp.template.utils.ApiExceptionMapper.ErrorMessage;

import com.disney.ops.wfm.lsp.api.lsp.template.utils.ImplicitSwaggerAnnotations;

import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;

import javax.annotation.Nonnull;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.net.URI;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import static javax.servlet.http.HttpServletResponse.*;
import static org.apache.http.HttpStatus.SC_CREATED;

@Path("/v1")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Api
public class BanquetLaborHoursApi {
    private static final Logger logger = LoggerFactory.getLogger(BanquetLaborHoursApi.class);
    public static final String FILE_FIND_ALL = "/all";
    public static final String MOVE_FILE = "/move";
    private final FilesService filesService = new SFTPServiceImpl(new DefaultSftpSessionFactory(true));

    /**
     * CRUD Add the content into the specified directory in the repository with the file name
     *
     * @param directory the directory where the file will be added.
     * @param fileName  the name of the file which will be added
     * @param file      the file to will be added (in byte[] format).
     * @return the file added to the directory.
     * // * @throws IOException
     */
    @POST
    @Path("/")
    @ApiResponses(value = {
            @ApiResponse(code = SC_CREATED,
                    message = "Successfully Added file to the specified repository.",
                    responseHeaders = {@ResponseHeader(name = "Location", response = String.class)}),
            @ApiResponse(code = SC_BAD_REQUEST, response = ErrorMessage.class, message = "The request was malformed or missing a required field"),
            @ApiResponse(code = SC_INTERNAL_SERVER_ERROR, response = ErrorMessage.class, message = "There was an issue processing the request and the server has failed out of the action"),
    })
    @ApiOperation(value = "Used to add a file to specified repository",
            tags = "banquet-labor.hours",
            nickname = "post-banquet-labor.hours")
    @ImplicitSwaggerAnnotations
    public Response uploadBanquetLabourHourFile(
            @Nonnull @ApiParam("Directory of the file") @QueryParam("directory") String directory,
            @Nonnull @ApiParam @QueryParam("Name of the file.") String fileName,
            @Nonnull @ApiParam("File to be Uploaded") @QueryParam("file") MultipartFile file) throws IOException {
        logger.debug("adding file {}", fileName);
        logger.info("Adding the content into the specified directory in the repository with the file name." + filesService );
        System.out.println("filesService:" + filesService);
        filesService.addFile(directory, fileName, file.getBytes());
        return Response.created(URI.create("/v1/"))
                .build();
    }


}

The current code of my test is the next:

package com.disney.ops.wfm.lsp.api.lsp.template.api;

import com.disney.ops.wfm.lsp.banquet.labor.hours.api.BanquetLaborHoursApi;
import com.disney.ops.wfm.lsp.banquet.labor.hours.domain.FilesService;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = BanquetLaborHoursApi.class)
@AutoConfigureMockMvc
public class BanquetLaborHoursApiTest {

   @Mock
    FilesService filesService;
    @Autowired
    protected MockMvc mvc;

    private BanquetLaborHoursApi banquetLaborHoursApi;

    private MockMvc mockMvc;

    private static final String TEST_PATH = "C:\\apache-maven-3.6.3\\apache-maven-3.6.3\\README.txt";

    @BeforeEach
    public void init() {
        MockitoAnnotations.initMocks(this);
        banquetLaborHoursApi = new BanquetLaborHoursApi();
    }


    @Test
    public void uploadBanquetLabourHourFile() throws Exception {
        String directory = "testDirectory";
        String fileName = "testFileName";
        MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes());

        MvcResult result = executeFindRequest(directory, fileName, file);
        mvc.perform(MockMvcRequestBuilders.asyncDispatch(result))
                .andExpect(status().isOk());
    }


    private MvcResult executeFindRequest(String directory, String fileName, MockMultipartFile file) throws Exception {
        return mvc.perform(MockMvcRequestBuilders
                .get("/", directory, fileName, file)
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(request().asyncStarted())
                .andReturn();
    }


}

I am wondering if I can use the same approach of a springBoot endpoint in this case or I have to use another kind of approach.

Now I am getting the next issue:

java.lang.NoSuchMethodError: org.springframework.context.annotation.AnnotatedBeanDefinitionReader.registerBean(Ljava/lang/Class;Ljava/lang/String;)V

My pom.xml dependencies are the next:

<dependencies>

    <dependency>
      <groupId>com.disney.ops.wfm.lsp</groupId>
      <artifactId>api-lsp-template</artifactId>
      <version>[0.0.1, 1.0.0)</version>
    </dependency>

    <dependency>
      <groupId>com.disney.ops.wfm.lsp</groupId>
      <artifactId>lsp-common-security-filter</artifactId>
      <version>[3.0.0, 4.0.0)</version>
    </dependency>

    <dependency>
      <groupId>org.webjars</groupId>
      <artifactId>swagger-ui</artifactId>
      <version>${version.swagger-ui}</version>
    </dependency>

    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-jaxrs</artifactId>
      <version>1.5.24</version>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
      <version>${version.cxf}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-service-description</artifactId>
      <version>${version.cxf}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>${version.cxf}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>${version.cxf}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>${version.cxf}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-features-logging</artifactId>
      <version>${version.cxf}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-core</artifactId>
      <version>${version.cxf}</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback.contrib</groupId>
      <artifactId>logback-jackson</artifactId>
      <version>${version.logback.contrib}</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback.contrib</groupId>
      <artifactId>logback-json-classic</artifactId>
      <version>${version.logback.contrib}</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${version.logback}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.integration</groupId>
      <artifactId>spring-integration-sftp</artifactId>
      <version>5.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>software.sham</groupId>
      <artifactId>sham-ssh</artifactId>
      <version>0.1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>com.disney.ops.wfm.lsp</groupId>
      <artifactId>api-lsp-template</artifactId>
      <version>[0.0.1, 1.0.0)</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.3.RELEASE</version>
      <scope>test</scope>
    </dependency>



    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <version>2.2.3.RELEASE</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

Thanks!

Aucun commentaire:

Enregistrer un commentaire