package org.ddouglascarr.unit;

import org.ddouglascarr.controllers.AreasController;
import org.ddouglascarr.enums.ExceptionCodes;
import org.ddouglascarr.exceptions.ItemNotFoundException;
import org.ddouglascarr.exceptions.MemberUnprivilegedException;
import org.ddouglascarr.models.Area;
import org.ddouglascarr.models.UserDetailsImpl;
import org.ddouglascarr.services.AreaService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class AreasControllerTests
{
    @Mock
    private AreaService areaService;

    @Mock
    private UserDetailsImpl userDetails;

    @InjectMocks
    private AreasController areasController = new AreasController();

    private Area mockArea;
    private List<Area> mockAreaList;
    private static Long UNIT_ID = new Long(2);
    private static Long AREA_ID = new Long(12);
    private static Long MEMBER_ID = new Long(32);

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

        when(userDetails.getId()).thenReturn(MEMBER_ID);
        mockArea = new Area();
        mockArea.setId(AREA_ID);
        mockAreaList = new ArrayList<>();
    }

    @Test
    public void getAreasShould401IfUnprivileged() throws Exception
    {
        when(areaService.findByUnitId(MEMBER_ID, UNIT_ID))
                .thenThrow(new MemberUnprivilegedException());
        ResponseEntity<List<Area>> resp = areasController.getAreas(userDetails, UNIT_ID);
        assertEquals(resp.getStatusCode(), HttpStatus.UNAUTHORIZED);
        assertEquals(resp.getHeaders().getFirst("error-code"),
                ExceptionCodes.UNPRIVILEGED.toString());
    }

    @Test
    public void getAreasShouldReturn404IfUnitDoesNotExist() throws Exception
    {
        when(areaService.findByUnitId(MEMBER_ID, UNIT_ID))
                .thenThrow(new ItemNotFoundException());
        ResponseEntity<List<Area>> resp = areasController.getAreas(userDetails, UNIT_ID);
        verify(areaService, times(1)).findByUnitId(MEMBER_ID, UNIT_ID);
        assertEquals(HttpStatus.NOT_FOUND, resp.getStatusCode());
        assertEquals(resp.getHeaders().getFirst("error-code"),
                ExceptionCodes.ITEM_NOT_FOUND.toString());
    }

    @Test
    public void getAreasShouldReturnListOfAreas() throws Exception
    {
        mockAreaList.add(mockArea);
        when(areaService.findByUnitId(MEMBER_ID, UNIT_ID)).thenReturn(mockAreaList);
        ResponseEntity<List<Area>> resp = areasController.getAreas(userDetails, UNIT_ID);
        assertEquals(resp.getStatusCode(), HttpStatus.OK);
        List<Area> returnedList = resp.getBody();
        assertEquals(returnedList, mockAreaList);
    }

    @Test
    public void getAreaShould401IfNotUnitReadPrivileged() throws Exception
    {
        when(areaService.findOneByUnitId(MEMBER_ID, UNIT_ID, AREA_ID))
                .thenThrow(new MemberUnprivilegedException());
        ResponseEntity<Area> resp = areasController.getArea(userDetails, UNIT_ID, AREA_ID);
        assertEquals(HttpStatus.UNAUTHORIZED, resp.getStatusCode());
        assertEquals(resp.getHeaders().getFirst("error-code"),
                ExceptionCodes.UNPRIVILEGED.toString());
    }

    @Test
    public void getAreaShouldReturnArea() throws Exception
    {
        when(areaService.findOneByUnitId(MEMBER_ID, UNIT_ID, AREA_ID)).thenReturn(mockArea);

        ResponseEntity<Area> resp = areasController.getArea(userDetails, UNIT_ID, AREA_ID);
        assertEquals(HttpStatus.OK, resp.getStatusCode());
        assertEquals(mockArea, resp.getBody());
    }

}

@@ -1432,1065 +1432,8 @@
  %7D%0A%0A
-    @Test%0A    public void getAreasShould401IfUnprivileged() throws Exception%0A    %7B%0A        when(areaService.findByUnitId(MEMBER_ID, UNIT_ID))%0A                .thenThrow(new MemberUnprivilegedException());%0A        ResponseEntity%3CList%3CArea%3E%3E resp = areasController.getAreas(userDetails, UNIT_ID);%0A        assertEquals(resp.getStatusCode(), HttpStatus.UNAUTHORIZED);%0A        assertEquals(resp.getHeaders().getFirst(%22error-code%22),%0A                ExceptionCodes.UNPRIVILEGED.toString());%0A    %7D%0A%0A    @Test%0A    public void getAreasShouldReturn404IfUnitDoesNotExist() throws Exception%0A    %7B%0A        when(areaService.findByUnitId(MEMBER_ID, UNIT_ID))%0A                .thenThrow(new ItemNotFoundException());%0A        ResponseEntity%3CList%3CArea%3E%3E resp = areasController.getAreas(userDetails, UNIT_ID);%0A        verify(areaService, times(1)).findByUnitId(MEMBER_ID, UNIT_ID);%0A        assertEquals(HttpStatus.NOT_FOUND, resp.getStatusCode());%0A        assertEquals(resp.getHeaders().getFirst(%22error-code%22),%0A                ExceptionCodes.ITEM_NOT_FOUND.toString());%0A    %7D%0A%0A
     

remove areasController.getAreas() error unit tests