package io.corbel.resources.rem.resmi;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import io.corbel.resources.rem.dao.RestorDao;
import io.corbel.resources.rem.model.RestorObject;
import io.corbel.resources.rem.model.RestorResourceUri;
import io.corbel.resources.rem.request.RequestParameters;
import io.corbel.resources.rem.request.ResourceId;
import io.corbel.resources.rem.restor.RestorGetRem;

import java.io.InputStream;
import java.util.Arrays;
import java.util.Optional;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.http.MediaType;

/**
 * @author Alberto J. Rubio
 */
public class RestorGetRemTest {

    private RestorDao daoMock;
    private RequestParameters requestParameters;
    private InputStream entity;

    private RestorGetRem getRem;

    @Before
    public void setup() {
        daoMock = Mockito.mock(RestorDao.class);
        requestParameters = Mockito.mock(RequestParameters.class);
        entity = Mockito.mock(InputStream.class);
        getRem = new RestorGetRem(daoMock);
    }

    @Test
    public void testGetOkResource() {
        when(requestParameters.getAcceptedMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_XML));
        when(requestParameters.getRequestedDomain()).thenReturn("RequestedDomain");
        String testEtag = "etag";
        when(daoMock.getObject(new RestorResourceUri("RequestedDomain", MediaType.APPLICATION_XML.toString(), "test", "resourceId")))
                .thenReturn(new RestorObject(MediaType.APPLICATION_XML.toString(), entity, 100L, testEtag));
        Response response = getRem.resource("test", new ResourceId("resourceId"), requestParameters, Optional.empty());
        String responseContentType = response.getMetadata().getFirst("Content-Type").toString();
        assertThat(responseContentType).isEqualTo(MediaType.APPLICATION_XML_VALUE);
        assertThat(response.getStatus()).isEqualTo(200);
        assertThat(response.getHeaderString(HttpHeaders.ETAG)).isEqualTo(testEtag);
        assertThat(response.getHeaderString(HttpHeaders.CONTENT_LENGTH)).isEqualTo("100");
    }

    @Test
    public void testGetNotExistsResource() {
        when(requestParameters.getAcceptedMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_XML));
        Response response = getRem.resource("test", new ResourceId("notExistsId"), requestParameters, Optional.empty());
        assertThat(response.getStatus()).isEqualTo(404);
    }

}

@@ -94,16 +94,56 @@
 rtThat;%0A
+import static org.mockito.Matchers.any;%0A
 import s
@@ -635,18 +635,133 @@
 ore.
-Response;%0A
+MultivaluedMap;%0Aimport javax.ws.rs.core.Response;%0A%0Aimport org.glassfish.jersey.internal.util.collection.MultivaluedStringMap;
 %0Aimp
@@ -1586,24 +1586,264 @@
 edDomain%22);%0A
+        MultivaluedMap multivaluedMap = Mockito.mock(MultivaluedMap.class);%0A        when(requestParameters.getHeaders()).thenReturn(multivaluedMap);%0A        when(multivaluedMap.getFirst(HttpHeaders.CONTENT_DISPOSITION)).thenReturn(%22any%22);%0A%0A
         Stri
@@ -2636,24 +2636,120 @@
 lTo(%22100%22);%0A
+        assertThat(response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION)).isEqualTo(%22any%22);%0A
     %7D%0A%0A    @

fix restor test