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.integration.mint;
019
020import org.fcrepo.mint.HttpPidMinter;
021import org.junit.Test;
022import org.junit.runner.RunWith;
023import org.springframework.test.context.ContextConfiguration;
024import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
025
026import javax.inject.Inject;
027import static java.lang.Integer.parseInt;
028import static org.junit.Assert.assertEquals;
029
030/**
031 * <p>HttpPidMinterIT class.</p>
032 *
033 * @author osmandin
034 * @author ajs6f
035 */
036@RunWith(SpringJUnit4ClassRunner.class)
037@ContextConfiguration("/test-container.xml")
038public class HttpPidMinterIT{
039
040    private static String PREFIX = "http://localhost:";
041
042    private static int getPort() {
043        return parseInt(System.getProperty("fcrepo.dynamic.test.port", "8080"));
044    }
045
046    @Inject
047    private ContainerWrapper containerWrapper;
048
049    private void addHandler(final String data1, final String path) {
050        containerWrapper.addHandler(data1, path);
051    }
052
053    @Test
054    public void shouldMintPid() {
055        final String res = "/res1";
056        final String server = PREFIX + getPort() + res;
057        addHandler("abc", res);
058        final HttpPidMinter minter = new HttpPidMinter(server, "POST", "", "", ".*/", null);
059        final String pid = minter.get();
060        assertEquals(pid, "abc");
061    }
062
063    @Test
064    public void shouldMintPid2() {
065        final String res = "/res2";
066        final String server = PREFIX + getPort() + res;
067        addHandler("abc", res);
068        final HttpPidMinter minter = new HttpPidMinter(server, "POST", "", "", " ", null);
069        final String pid = minter.get();
070        assertEquals(pid, "abc");
071    }
072
073    @Test
074    public void shouldMintPid3() {
075        final String res = "/res3";
076        final String server = PREFIX + getPort() + res;
077        addHandler("abc", res);
078        final HttpPidMinter minter = new HttpPidMinter(server, "WHATEVER", "", "", " ", null);
079        final String pid = minter.get();
080        assertEquals(pid, "abc");
081    }
082
083    @Test (expected = IllegalArgumentException.class)
084    public void shouldNotAccept1() {
085        new HttpPidMinter(null, "POST", "", "", ".*/", "");
086    }
087
088    @Test (expected = IllegalArgumentException.class)
089    public void shouldNotAccept2() {
090        new HttpPidMinter(null, "POST", "", "", ".*/", " ");
091    }
092
093    @Test (expected = IllegalArgumentException.class)
094    public void shouldNotAccept3() {
095        new HttpPidMinter("http://test", "POST", "", "", ".*/", "\\wrongxpath");
096    }
097
098    @Test
099    public void shouldMintPidWithGET() {
100        final String res = "/getres";
101        final String server = PREFIX + getPort() + res;
102        addHandler("abc", res);
103        final HttpPidMinter minter = new HttpPidMinter(server, "GET", "", "", ".*/", null);
104        final String pid = minter.get();
105        assertEquals(pid, "abc");
106    }
107
108    @Test
109    public void shouldMintPidWithPUT() {
110        final String res = "/putres";
111        final String server = PREFIX + getPort() + res;
112        addHandler("abc", res);
113        final HttpPidMinter minter = new HttpPidMinter(server, "PUT", "", "", ".*/", null);
114        final String pid = minter.get();
115        assertEquals(pid, "abc");
116    }
117
118    @Test
119    public void shouldMintPidWithNullMethod() {
120        final String res = "/res4";
121        final String server = PREFIX + getPort() + res;
122        addHandler("abc", res);
123        final HttpPidMinter minter = new HttpPidMinter(server, null, "", "", ".*/", null);
124        final String pid = minter.get();
125        assertEquals(pid, "abc");
126    }
127
128    @Test
129    public void shouldMintPidXml() {
130        final String res = "/xml1";
131        final String server = PREFIX + getPort() + res;
132        addHandler("<test><id>baz</id></test>", res);
133        final HttpPidMinter minter = new HttpPidMinter(server, "POST"
134                , "", "", "", "/test/id");
135        final String pid = minter.get();
136        assertEquals(pid, "baz");
137    }
138
139    @Test
140    public void shouldRunWithNoAuth() {
141        final String res = "/res5";
142        final String server = PREFIX + getPort() + res;
143        addHandler("abc", res);
144        final HttpPidMinter minter = new HttpPidMinter(server, "POST",
145                "fedoraAdmin", "secret", ".*/", null);
146        final String pid = minter.get();
147        assertEquals(pid, "abc");
148    }
149
150    @Test (expected = RuntimeException.class)
151    public void shouldMintPidXmlInvalid() {
152        final String res = "/xml2";
153        final String server = PREFIX + getPort() + res;
154        addHandler("<test><id>baz</id></tet>", res);
155        final HttpPidMinter minter = new HttpPidMinter(server, "POST"
156                , "", "", "", "/test/id");
157        minter.get();
158    }
159
160}
161