001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.mint;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.mockito.Matchers.isA;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.when;
025import static org.mockito.Mockito.verify;
026
027import org.junit.Test;
028
029import org.apache.http.HttpResponse;
030import org.apache.http.client.HttpClient;
031import org.apache.http.entity.ByteArrayEntity;
032import org.apache.http.client.methods.HttpUriRequest;
033
034/**
035 * @author escowles
036 * @since 2014-05-15
037 */
038public class HttpPidMinterTest {
039
040    @Test
041    public void testMintPid() throws Exception {
042        final HttpPidMinter testMinter = new HttpPidMinter(
043            "http://localhost/minter","POST", "", "", ".*/", "");
044
045        final HttpClient mockClient = mock(HttpClient.class);
046        final HttpResponse mockResponse = mock(HttpResponse.class);
047        final ByteArrayEntity entity = new ByteArrayEntity("/foo/bar/baz".getBytes());
048        testMinter.client = mockClient;
049
050        when(mockClient.execute(isA(HttpUriRequest.class))).thenReturn(mockResponse);
051        when(mockResponse.getEntity()).thenReturn(entity);
052
053        final String pid = testMinter.get();
054        verify(mockClient).execute(isA(HttpUriRequest.class));
055        assertEquals( pid, "baz" );
056    }
057
058    @Test
059    public void testMintPidNullHttpMethod() throws Exception {
060        final HttpPidMinter testMinter = new HttpPidMinter(
061                "http://localhost/minter", null, "", "", ".*/", "");
062
063        final HttpClient mockClient = mock(HttpClient.class);
064        final HttpResponse mockResponse = mock(HttpResponse.class);
065        final ByteArrayEntity entity = new ByteArrayEntity("/foo/bar/baz".getBytes());
066        testMinter.client = mockClient;
067
068        when(mockClient.execute(isA(HttpUriRequest.class))).thenReturn(mockResponse);
069        when(mockResponse.getEntity()).thenReturn(entity);
070
071        final String pid = testMinter.get();
072        verify(mockClient).execute(isA(HttpUriRequest.class));
073        assertEquals( pid, "baz" );
074    }
075
076    @Test
077    public void testMintPidXPath() throws Exception {
078        final HttpPidMinter testMinter = new HttpPidMinter(
079            "http://localhost/minter","POST", "", "", "", "/test/id");
080
081        final HttpClient mockClient = mock(HttpClient.class);
082        final HttpResponse mockResponse = mock(HttpResponse.class);
083        final ByteArrayEntity entity = new ByteArrayEntity("<test><id>baz</id></test>".getBytes());
084        testMinter.client = mockClient;
085
086        when(mockClient.execute(isA(HttpUriRequest.class))).thenReturn(mockResponse);
087        when(mockResponse.getEntity()).thenReturn(entity);
088
089        final String pid = testMinter.get();
090        verify(mockClient).execute(isA(HttpUriRequest.class));
091        assertEquals(pid, "baz");
092    }
093
094    @Test
095    public void testHttpClient() {
096        final HttpPidMinter testMinter = new HttpPidMinter(
097            "http://localhost/minter","POST", "user", "pass", "", "");
098        final HttpClient client = testMinter.buildClient();
099        assertNotNull(client);
100    }
101
102}