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 */
016
017package org.fcrepo.client;
018
019import static java.net.URI.create;
020import static org.fcrepo.client.TestUtils.RDF_XML;
021import static org.fcrepo.client.TestUtils.baseUrl;
022import static org.fcrepo.client.TestUtils.rdfXml;
023import static org.fcrepo.client.TestUtils.setField;
024import static org.junit.Assert.assertEquals;
025import static org.mockito.Matchers.any;
026import static org.mockito.Mockito.when;
027
028import java.io.IOException;
029import java.net.URI;
030
031import org.apache.commons.io.IOUtils;
032import org.apache.http.Header;
033import org.apache.http.StatusLine;
034import org.apache.http.client.methods.CloseableHttpResponse;
035import org.apache.http.client.methods.HttpUriRequest;
036import org.apache.http.entity.ByteArrayEntity;
037import org.apache.http.impl.client.CloseableHttpClient;
038import org.apache.http.message.BasicHeader;
039import org.junit.Test;
040import org.junit.runner.RunWith;
041import org.mockito.Mock;
042import org.mockito.runners.MockitoJUnitRunner;
043
044/**
045 * @author acoburn
046 */
047@RunWith(MockitoJUnitRunner.class)
048public class FcrepoClientAuthTest {
049
050    private FcrepoClient testClient;
051
052    @Mock
053    private CloseableHttpClient mockHttpclient;
054
055    @Mock
056    private CloseableHttpResponse mockResponse;
057
058    @Mock
059    private StatusLine mockStatus;
060
061    @Test
062    public void testAuthNoHost() throws IOException, FcrepoOperationFailedException {
063        final int status = 200;
064        final URI uri = create(baseUrl);
065        final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes());
066
067        testClient = FcrepoClient.client()
068                .credentials("user", "pass")
069                .throwExceptionOnFailure()
070                .build();
071        setField(testClient, "httpclient", mockHttpclient);
072        entity.setContentType(RDF_XML);
073        doSetupMockRequest(RDF_XML, entity, status);
074
075        final FcrepoResponse response = testClient.get(uri)
076                .accept(RDF_XML)
077                .perform();
078
079        assertEquals(response.getUrl(), uri);
080        assertEquals(response.getStatusCode(), status);
081        assertEquals(response.getContentType(), RDF_XML);
082        assertEquals(response.getLocation(), null);
083        assertEquals(IOUtils.toString(response.getBody()), rdfXml);
084    }
085
086    @Test
087    public void testAuthWithHost() throws IOException, FcrepoOperationFailedException {
088        final int status = 200;
089        final URI uri = create(baseUrl);
090        final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes());
091
092        testClient = FcrepoClient.client()
093                .credentials("user", "pass")
094                .authScope("localhost")
095                .throwExceptionOnFailure()
096                .build();
097        setField(testClient, "httpclient", mockHttpclient);
098        entity.setContentType(RDF_XML);
099        doSetupMockRequest(RDF_XML, entity, status);
100
101        final FcrepoResponse response = testClient.get(uri)
102                .accept(RDF_XML)
103                .perform();
104
105        assertEquals(response.getUrl(), uri);
106        assertEquals(response.getStatusCode(), status);
107        assertEquals(response.getContentType(), RDF_XML);
108        assertEquals(response.getLocation(), null);
109        assertEquals(IOUtils.toString(response.getBody()), rdfXml);
110    }
111
112    @Test
113    public void testAuthNoPassword() throws IOException, FcrepoOperationFailedException {
114        final int status = 200;
115        final URI uri = create(baseUrl);
116        final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes());
117
118        testClient = FcrepoClient.client()
119                .credentials("user", null)
120                .throwExceptionOnFailure()
121                .build();
122        setField(testClient, "httpclient", mockHttpclient);
123        entity.setContentType(RDF_XML);
124        doSetupMockRequest(RDF_XML, entity, status);
125
126        final FcrepoResponse response = testClient.get(uri)
127                .accept(RDF_XML)
128                .perform();
129
130        assertEquals(response.getUrl(), uri);
131        assertEquals(response.getStatusCode(), status);
132        assertEquals(response.getContentType(), RDF_XML);
133        assertEquals(response.getLocation(), null);
134        assertEquals(IOUtils.toString(response.getBody()), rdfXml);
135    }
136
137    private void doSetupMockRequest(final String contentType, final ByteArrayEntity entity, final int status)
138            throws IOException {
139        final Header contentTypeHeader = new BasicHeader("Content-Type", contentType);
140        final Header[] headers = new Header[] { contentTypeHeader };
141
142        when(mockHttpclient.execute(any(HttpUriRequest.class))).thenReturn(mockResponse);
143        when(mockResponse.getAllHeaders()).thenReturn(headers);
144        when(mockResponse.getEntity()).thenReturn(entity);
145        when(mockResponse.getStatusLine()).thenReturn(mockStatus);
146        when(mockStatus.getStatusCode()).thenReturn(status);
147    }
148}