/*
 * Copyright 2013 Franklin Bristow <franklin.bristow@phac-aspc.gc.ca>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ca.corefacility.bioinformatics.irida.web.controller.test.unit;

import ca.corefacility.bioinformatics.irida.model.roles.impl.Identifier;
import ca.corefacility.bioinformatics.irida.service.CRUDService;
import ca.corefacility.bioinformatics.irida.web.controller.GenericController;
import ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity;
import ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestResource;
import com.google.common.net.HttpHeaders;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.springframework.hateoas.Link;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Unit tests for the {@link GenericController}.
 *
 * @author Franklin Bristow <franklin.bristow@phac-aspc.gc.ca>
 */
public class GenericControllerTest {

    private GenericController<Identifier, IdentifiableTestEntity, IdentifiableTestResource> controller;
    private CRUDService<Identifier, IdentifiableTestEntity> crudService;
    private IdentifiableTestEntity e;
    private Identifier id;

    @Before
    public void setUp() {
        crudService = mock(CRUDService.class);
        id = new Identifier();
        e = new IdentifiableTestEntity();
        e.setIdentifier(id);
        controller = new GenericController<Identifier, IdentifiableTestEntity, IdentifiableTestResource>(crudService, Identifier.class, IdentifiableTestEntity.class, IdentifiableTestResource.class) {
            @Override
            public Collection<Link> constructCustomResourceLinks(IdentifiableTestEntity resource) {
                return Collections.emptySet();
            }

            @Override
            public IdentifiableTestEntity mapResourceToType(IdentifiableTestResource representation) {
                return e;
            }
        };

        controller.initializePages();

        // fake out the servlet response so that the URI builder will work.
        RequestAttributes ra = new ServletRequestAttributes(new MockHttpServletRequest());
        RequestContextHolder.setRequestAttributes(ra);
    }

    @Test
    public void testCreateBadEntity() {
        IdentifiableTestResource r = new IdentifiableTestResource(e);

        when(crudService.create(e)).thenThrow(new ConstraintViolationException(new HashSet<ConstraintViolation<?>>()));

        try {
            controller.create(r);
            fail();
        } catch (ConstraintViolationException ex) {
        } catch (Exception ex) {
            fail();
        }
    }

    @Test
    public void testCreateGoodEntity() {
        IdentifiableTestResource resource = new IdentifiableTestResource(e);
        when(crudService.create(e)).thenReturn(e);

        ResponseEntity<String> mav = controller.create(resource);
        assertEquals(HttpStatus.CREATED, mav.getStatusCode());
        assertTrue(mav.getHeaders().getFirst(HttpHeaders.LOCATION).
                endsWith(id.getIdentifier()));
    }
}

@@ -702,16 +702,96 @@
 .unit;%0A%0A
+import ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException;%0A
 import c
@@ -1329,16 +1329,39 @@
 ashSet;%0A
+import java.util.UUID;%0A
 import j
@@ -4253,11 +4253,815 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testDeleteEntity() %7B%0A        ResponseEntity%3CString%3E response = null;%0A        try %7B%0A            response = controller.delete(UUID.randomUUID().toString());%0A        %7D catch (InstantiationException %7C IllegalAccessException e) %7B%0A            fail();%0A        %7D%0A        assertEquals(HttpStatus.OK, response.getStatusCode());%0A    %7D%0A%0A    @Test%0A    public void testDeleteInvalidEntity() %7B%0A        String id = UUID.randomUUID().toString();%0A        Identifier identifier = new Identifier();%0A        identifier.setIdentifier(id);%0A        doThrow(new EntityNotFoundException(%22not found%22)).when(crudService).delete(identifier);%0A%0A        try %7B%0A            controller.delete(id);%0A        %7D catch (EntityNotFoundException e) %7B%0A        %7D catch (Exception e) %7B%0A            fail();%0A        %7D%0A    %7D
 %0A%7D%0A

Add tests for deleting entities.