com.salmon.security.xacml.demo.springmvc.rest.HTTPPopulatorsTest.java Source code

Java tutorial

Introduction

Here is the source code for com.salmon.security.xacml.demo.springmvc.rest.HTTPPopulatorsTest.java

Source

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

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import com.salmon.security.xacml.demo.springmvc.domain.Driver;
import com.salmon.security.xacml.demo.springmvc.domain.Vehicle;
import com.salmon.security.xacml.demo.springmvc.repository.fixtures.DriverFixtures;
import com.salmon.security.xacml.demo.springmvc.repository.fixtures.VehicleFixtures;
import com.salmon.security.xacml.demo.springmvc.rest.controller.MarketPlaceQueryController;

import java.util.List;
import java.util.Arrays;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.fail;

public class HTTPPopulatorsTest {

    private static Logger LOG = LoggerFactory.getLogger(HTTPPopulatorsTest.class);

    @Before
    public void setup() {
        this.eraseAllData();
    }

    @Test
    public void testCreateDriver() {

        ResponseEntity<Driver> entity = this.createOneDriver();

        String path = entity.getHeaders().getLocation().getPath();

        assertEquals(HttpStatus.CREATED, entity.getStatusCode());
        Driver driver = entity.getBody();

        LOG.info("The Driver ID is " + driver.getInternalID());
        LOG.info("The Location is " + entity.getHeaders().getLocation());
        LOG.info("The Path is " + path);

        assertEquals("JOEBOGGS2015LICENSE", driver.getDriversLicense());
        assertTrue(path.startsWith("/xacml/aggregators/dvla/drivers/"));

    }

    @Test
    public void testCreateVehicle() {

        ResponseEntity<Vehicle> entity = this.createOneVehicle();

        String path = entity.getHeaders().getLocation().getPath();

        assertEquals(HttpStatus.CREATED, entity.getStatusCode());
        Vehicle vehicle = entity.getBody();

        LOG.info("The Vehicle PLATE is " + vehicle.getPlate());
        LOG.info("The Location is " + entity.getHeaders().getLocation());

        assertEquals("RE 11 ODH", vehicle.getPlate());
        assertTrue(path.startsWith("/xacml/aggregators/dvla/vehicles/"));

    }

    @Test
    public void testAllDataIsErased() {

        this.createOneDriver();
        this.createOneVehicle();

        ResponseEntity<Integer> entity = this.eraseAllData();

        Integer totalItemsDeleted = entity.getBody();
        assertEquals(HttpStatus.OK, entity.getStatusCode());
        assertEquals(2, totalItemsDeleted.intValue());
    }

    @Test
    public void badUserPrevented() {
        HttpHeaders headers = this.getHeaders("wrongusername" + ":" + "wrongpwd");

        RestTemplate template = new RestTemplate();

        HttpEntity<String> requestEntity = new HttpEntity<String>(DriverFixtures.standardDriverJSON(), headers);

        try {
            ResponseEntity<Driver> entity = template.postForEntity(
                    "http://localhost:8085/xacml/populators/dvla/driveradd", requestEntity, Driver.class);
            fail("Request Passed incorrectly with status " + entity.getStatusCode());
        } catch (HttpClientErrorException ex) {
            assertEquals(HttpStatus.UNAUTHORIZED, ex.getStatusCode());
        }

    }

    private HttpHeaders getHeaders(String auth) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

        byte[] encodedAuthorisation = Base64.encode(auth.getBytes());
        headers.add("Authorization", "Basic " + new String(encodedAuthorisation));

        return headers;
    }

    private ResponseEntity<Integer> eraseAllData() {
        HttpHeaders headers = this.getHeaders("myusername" + ":" + "mypwd");

        RestTemplate template = new RestTemplate();

        HttpEntity<String> requestEntity = new HttpEntity<String>("{}", headers);

        ResponseEntity<Integer> entity = template.postForEntity("http://localhost:8085/xacml/populators/dvla/reset",
                requestEntity, Integer.class);

        return entity;
    }

    private ResponseEntity<Vehicle> createOneVehicle() {
        HttpHeaders headers = this.getHeaders("myusername" + ":" + "mypwd");

        RestTemplate template = new RestTemplate();

        HttpEntity<String> requestEntity = new HttpEntity<String>(VehicleFixtures.strandardVehicleJSON(), headers);

        ResponseEntity<Vehicle> entity = template.postForEntity(
                "http://localhost:8085/xacml/populators/dvla/vehicleadd", requestEntity, Vehicle.class);
        return entity;
    }

    private ResponseEntity<Driver> createOneDriver() {
        HttpHeaders headers = this.getHeaders("myusername" + ":" + "mypwd");

        RestTemplate template = new RestTemplate();

        HttpEntity<String> requestEntity = new HttpEntity<String>(DriverFixtures.standardDriverJSON(), headers);

        ResponseEntity<Driver> entity = template.postForEntity(
                "http://localhost:8085/xacml/populators/dvla/driveradd", requestEntity, Driver.class);
        return entity;
    }

}