com.salmon.security.xacml.demo.springmvc.services.RestQueryUnitTest.java Source code

Java tutorial

Introduction

Here is the source code for com.salmon.security.xacml.demo.springmvc.services.RestQueryUnitTest.java

Source

package com.salmon.security.xacml.demo.springmvc.services;

import org.junit.Before;
import org.junit.Test;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

import org.springframework.test.web.servlet.MockMvc;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

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.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

import com.salmon.security.xacml.demo.springmvc.controller.*;
import com.salmon.security.xacml.demo.springmvc.domain.Driver;
import com.salmon.security.xacml.demo.springmvc.rest.controller.*;

import com.salmon.security.xacml.demo.springmvc.repository.fixtures.*;

public class RestQueryUnitTest {

    MockMvc mockMvc;

    @InjectMocks
    MarketPlaceQueryController restQueryController = new MarketPlaceQueryController();

    @Mock
    IMarketPlaceController marketPlaceController;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);

        this.mockMvc = standaloneSetup(restQueryController)
                .setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
    }

    String driver = "JOEBOGGS2015LICENSE";
    String nonExistingDriver = "JALDKJAKDJASLKDAJ";

    @Test
    public void getOneDriver() throws Exception {

        when(marketPlaceController.getDriverDetails(driver)).thenReturn(DriverFixtures.standardDriver());

        this.mockMvc.perform(get("/aggregators/dvla/drivers/{license}", driver).accept(MediaType.APPLICATION_JSON))
                //.andDo(print())
                .andExpect(status().isOk());
    }

    @Test
    public void listDriversTest() throws Exception {

        when(marketPlaceController.listDrivers()).thenReturn(DriverFixtures.allDrivers());

        this.mockMvc.perform(get("/aggregators/dvla/drivers").accept(MediaType.APPLICATION_JSON))
                //.andDo(print())
                .andExpect(status().isOk());
    }

    @Test
    public void listVehiclesTest() throws Exception {

        when(marketPlaceController.listVehicles()).thenReturn(VehicleFixtures.allVehicles());

        this.mockMvc.perform(get("/aggregators/dvla/vehicles").accept(MediaType.APPLICATION_JSON)).andDo(print())
                .andExpect(status().isOk());
    }

    @Test
    public void getNonExistingDriver() throws Exception {

        when(marketPlaceController.getDriverDetails(nonExistingDriver)).thenReturn(null);

        this.mockMvc
                .perform(get("/aggregators/dvla/drivers/{license}", nonExistingDriver)
                        .accept(MediaType.APPLICATION_JSON))
                //.andDo(print())
                .andExpect(status().isNotFound());
    }

}