001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.client;
017
018import static org.fcrepo.client.TestUtils.baseUrl;
019import static org.fcrepo.client.TestUtils.rdfXml;
020import static org.fcrepo.client.TestUtils.setField;
021import static org.fcrepo.client.TestUtils.RDF_XML;
022import static org.junit.Assert.assertEquals;
023import static org.mockito.Matchers.any;
024import static org.mockito.Mockito.when;
025import static java.net.URI.create;
026
027import java.io.IOException;
028import java.net.URI;
029
030import org.apache.commons.io.IOUtils;
031import org.apache.http.Header;
032import org.apache.http.StatusLine;
033import org.apache.http.client.methods.CloseableHttpResponse;
034import org.apache.http.client.methods.HttpUriRequest;
035import org.apache.http.entity.ByteArrayEntity;
036import org.apache.http.impl.client.CloseableHttpClient;
037import org.apache.http.message.BasicHeader;
038import org.junit.Test;
039import org.junit.runner.RunWith;
040import org.mockito.Mock;
041import org.mockito.runners.MockitoJUnitRunner;
042
043/**
044 * @author acoburn
045 */
046@RunWith(MockitoJUnitRunner.class)
047public class FcrepoClientAuthTest {
048
049    private FcrepoClient testClient;
050
051    @Mock
052    private CloseableHttpClient mockHttpclient;
053
054    @Mock
055    private CloseableHttpResponse mockResponse;
056
057    @Mock
058    private StatusLine mockStatus;
059
060    @Test
061    public void testAuthNoHost() throws IOException, FcrepoOperationFailedException {
062        final int status = 200;
063        final URI uri = create(baseUrl);
064        final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes());
065
066        testClient = new FcrepoClient("user", "pass", null, true);
067        setField(testClient, "httpclient", mockHttpclient);
068        entity.setContentType(RDF_XML);
069        doSetupMockRequest(RDF_XML, entity, status);
070
071        final FcrepoResponse response = testClient.get(uri, RDF_XML, null);
072
073        assertEquals(response.getUrl(), uri);
074        assertEquals(response.getStatusCode(), status);
075        assertEquals(response.getContentType(), RDF_XML);
076        assertEquals(response.getLocation(), null);
077        assertEquals(IOUtils.toString(response.getBody()), rdfXml);
078    }
079
080    @Test
081    public void testAuthWithHost() throws IOException, FcrepoOperationFailedException {
082        final int status = 200;
083        final URI uri = create(baseUrl);
084        final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes());
085
086        testClient = new FcrepoClient("user", "pass", "localhost", true);
087        setField(testClient, "httpclient", mockHttpclient);
088        entity.setContentType(RDF_XML);
089        doSetupMockRequest(RDF_XML, entity, status);
090
091        final FcrepoResponse response = testClient.get(uri, RDF_XML, null);
092
093        assertEquals(response.getUrl(), uri);
094        assertEquals(response.getStatusCode(), status);
095        assertEquals(response.getContentType(), RDF_XML);
096        assertEquals(response.getLocation(), null);
097        assertEquals(IOUtils.toString(response.getBody()), rdfXml);
098    }
099
100    @Test
101    public void testAuthNoPassword() throws IOException, FcrepoOperationFailedException {
102        final int status = 200;
103        final URI uri = create(baseUrl);
104        final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes());
105
106        testClient = new FcrepoClient("user", null, null, true);
107        setField(testClient, "httpclient", mockHttpclient);
108        entity.setContentType(RDF_XML);
109        doSetupMockRequest(RDF_XML, entity, status);
110
111        final FcrepoResponse response = testClient.get(uri, RDF_XML, null);
112
113        assertEquals(response.getUrl(), uri);
114        assertEquals(response.getStatusCode(), status);
115        assertEquals(response.getContentType(), RDF_XML);
116        assertEquals(response.getLocation(), null);
117        assertEquals(IOUtils.toString(response.getBody()), rdfXml);
118    }
119
120    private void doSetupMockRequest(final String contentType, final ByteArrayEntity entity, final int status)
121            throws IOException {
122        final Header contentTypeHeader = new BasicHeader("Content-Type", contentType);
123        final Header[] linkHeaders = new Header[]{};
124
125        when(mockHttpclient.execute(any(HttpUriRequest.class))).thenReturn(mockResponse);
126        when(mockResponse.getFirstHeader("Location")).thenReturn(null);
127        when(mockResponse.getFirstHeader("Content-Type")).thenReturn(contentTypeHeader);
128        when(mockResponse.getHeaders("Link")).thenReturn(linkHeaders);
129        when(mockResponse.getEntity()).thenReturn(entity);
130        when(mockResponse.getStatusLine()).thenReturn(mockStatus);
131        when(mockStatus.getStatusCode()).thenReturn(status);
132    }
133}