Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
321 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...l-impl/src/test/java/org/fcrepo/integration/kernel/impl/identifiers/ContainerWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright 2014 DuraSpace, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.fcrepo.integration.kernel.impl.identifiers; | ||
|
||
|
||
import org.glassfish.grizzly.PortRange; | ||
import org.glassfish.grizzly.http.server.HttpHandler; | ||
import org.glassfish.grizzly.http.server.HttpServer; | ||
import org.glassfish.grizzly.http.server.NetworkListener; | ||
import org.glassfish.grizzly.http.server.Request; | ||
import org.glassfish.grizzly.http.server.Response; | ||
import org.slf4j.Logger; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.annotation.PreDestroy; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
/** | ||
* <p>ContainerWrapper class.</p> | ||
* | ||
* @author osmandin | ||
*/ | ||
public class ContainerWrapper implements ApplicationContextAware { | ||
|
||
private static final Logger logger = getLogger(ContainerWrapper.class); | ||
|
||
private String configLocation; | ||
|
||
public void setConfigLocation(final String configLocation) { | ||
this.configLocation = configLocation.replaceFirst("^classpath:", "/"); | ||
} | ||
|
||
private int port; | ||
|
||
public void setPort(final int port) { | ||
this.port = port; | ||
} | ||
|
||
private HttpServer server; | ||
|
||
@PostConstruct | ||
public void start() throws Exception { | ||
server = new HttpServer(); | ||
final NetworkListener listener = new NetworkListener("grizzly", "localhost", new PortRange(port)); | ||
server.addListener(listener); | ||
|
||
try { | ||
server.start(); | ||
logger.info("Test server running on {}", port); | ||
} catch (Throwable e) { | ||
logger.error("Error with test server", e); | ||
} | ||
} | ||
|
||
public void addHandler(final String data, final String path) { | ||
final HttpHandler httpHandler = new HttpHandler() { | ||
|
||
public void service(final Request request, final Response response) | ||
throws Exception { | ||
response.getWriter().write(data); | ||
} | ||
}; | ||
|
||
server.getServerConfiguration().addHttpHandler(httpHandler, "/" + path); | ||
} | ||
|
||
@PreDestroy | ||
public void stop() { | ||
server.shutdownNow(); | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(final ApplicationContext applicationContext) | ||
throws BeansException { | ||
} | ||
|
||
} |
160 changes: 160 additions & 0 deletions
160
...el-impl/src/test/java/org/fcrepo/integration/kernel/impl/identifiers/HttpPidMinterIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* Copyright 2014 DuraSpace, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.fcrepo.integration.kernel.impl.identifiers; | ||
|
||
import org.fcrepo.kernel.impl.identifiers.HttpPidMinter; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import javax.inject.Inject; | ||
import java.io.IOException; | ||
|
||
import static java.lang.Integer.parseInt; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* <p>HttpPidMinterIT class.</p> | ||
* | ||
* @author osmandin | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration("/test-container.xml") | ||
public class HttpPidMinterIT{ | ||
|
||
private static String PREFIX = "http://localhost:"; | ||
|
||
private int getPort() { | ||
return parseInt(System.getProperty("test.port", "8080")); | ||
} | ||
|
||
@Inject | ||
private ContainerWrapper containerWrapper; | ||
|
||
private void addHandler(final String data1, final String path) { | ||
containerWrapper.addHandler(data1, path); | ||
} | ||
|
||
@Test | ||
public void shouldMintPid() throws IOException { | ||
final String res = "/res1"; | ||
final String server = PREFIX + getPort() + res; | ||
addHandler("abc", res); | ||
final HttpPidMinter minter = new HttpPidMinter(server, "POST", "", "", ".*/", null); | ||
final String pid = minter.mintPid(); | ||
assertEquals(pid, "abc"); | ||
} | ||
|
||
@Test | ||
public void shouldMintPid2() throws IOException { | ||
final String res = "/res2"; | ||
final String server = PREFIX + getPort() + res; | ||
addHandler("abc", res); | ||
final HttpPidMinter minter = new HttpPidMinter(server, "POST", "", "", " ", null); | ||
final String pid = minter.mintPid(); | ||
assertEquals(pid, "abc"); | ||
} | ||
|
||
@Test | ||
public void shouldMintPid3() throws IOException { | ||
final String res = "/res3"; | ||
final String server = PREFIX + getPort() + res; | ||
addHandler("abc", res); | ||
final HttpPidMinter minter = new HttpPidMinter(server, "WHATEVER", "", "", " ", null); | ||
final String pid = minter.mintPid(); | ||
assertEquals(pid, "abc"); | ||
} | ||
|
||
@Test (expected = IllegalArgumentException.class) | ||
public void shouldNotAccept1() throws IOException { | ||
new HttpPidMinter(null, "POST", "", "", ".*/", ""); | ||
} | ||
|
||
@Test (expected = IllegalArgumentException.class) | ||
public void shouldNotAccept2() throws IOException { | ||
new HttpPidMinter(null, "POST", "", "", ".*/", " "); | ||
} | ||
|
||
@Test (expected = IllegalArgumentException.class) | ||
public void shouldNotAccept3() throws IOException { | ||
new HttpPidMinter("http://test", "POST", "", "", ".*/", "\\wrongxpath"); | ||
} | ||
|
||
@Test | ||
public void shouldMintPidWithGET() throws IOException { | ||
final String res = "/getres"; | ||
final String server = PREFIX + getPort() + res; | ||
addHandler("abc", res); | ||
final HttpPidMinter minter = new HttpPidMinter(server, "GET", "", "", ".*/", null); | ||
final String pid = minter.mintPid(); | ||
assertEquals(pid, "abc"); | ||
} | ||
|
||
@Test | ||
public void shouldMintPidWithPUT() throws IOException { | ||
final String res = "/putres"; | ||
final String server = PREFIX + getPort() + res; | ||
addHandler("abc", res); | ||
final HttpPidMinter minter = new HttpPidMinter(server, "PUT", "", "", ".*/", null); | ||
final String pid = minter.mintPid(); | ||
assertEquals(pid, "abc"); | ||
} | ||
|
||
@Test | ||
public void shouldMintPidWithNullMethod() throws IOException { | ||
final String res = "/res4"; | ||
final String server = PREFIX + getPort() + res; | ||
addHandler("abc", res); | ||
final HttpPidMinter minter = new HttpPidMinter(server, null, "", "", ".*/", null); | ||
final String pid = minter.mintPid(); | ||
assertEquals(pid, "abc"); | ||
} | ||
|
||
@Test | ||
public void shouldMintPidXml() throws IOException { | ||
final String res = "/xml1"; | ||
final String server = PREFIX + getPort() + res; | ||
addHandler("<test><id>baz</id></test>", res); | ||
final HttpPidMinter minter = new HttpPidMinter(server, "POST" | ||
, "", "", "", "/test/id"); | ||
final String pid = minter.mintPid(); | ||
assertEquals(pid, "baz"); | ||
} | ||
|
||
@Test | ||
public void shouldRunWithNoAuth() throws IOException { | ||
final String res = "/res5"; | ||
final String server = PREFIX + getPort() + res; | ||
addHandler("abc", res); | ||
final HttpPidMinter minter = new HttpPidMinter(server, "POST", | ||
"fedoraAdmin", "secret", ".*/", null); | ||
final String pid = minter.mintPid(); | ||
assertEquals(pid, "abc"); | ||
} | ||
|
||
@Test (expected = RuntimeException.class) | ||
public void shouldMintPidXmlInvalid() throws IOException { | ||
final String res = "/xml2"; | ||
final String server = PREFIX + getPort() + res; | ||
addHandler("<test><id>baz</id></tet>", res); | ||
final HttpPidMinter minter = new HttpPidMinter(server, "POST" | ||
, "", "", "", "/test/id"); | ||
minter.mintPid(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:context="http://www.springframework.org/schema/context" | ||
xmlns:p="http://www.springframework.org/schema/p" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | ||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> | ||
|
||
<context:property-placeholder/> | ||
|
||
<bean id="containerWrapper" | ||
class="org.fcrepo.integration.kernel.impl.identifiers.ContainerWrapper" | ||
init-method="start" destroy-method="stop" p:port="${test.port:8080}" | ||
p:configLocation="classpath:web.xml"/> | ||
|
||
</beans> |