Navigation Menu

Skip to content

Commit

Permalink
Add HttpPitMinter IT
Browse files Browse the repository at this point in the history
  • Loading branch information
osmandin authored and Andrew Woods committed Nov 17, 2014
1 parent 8aff44a commit cd2d11c
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 9 deletions.
22 changes: 22 additions & 0 deletions fcrepo-kernel-impl/pom.xml
Expand Up @@ -75,6 +75,21 @@
</dependency>

<!-- test gear -->
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -116,6 +131,9 @@

<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -140,6 +158,10 @@
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Expand Up @@ -89,15 +89,16 @@ public HttpPidMinter( final String url, final String method, final String userna
checkArgument( !isBlank(url), "Minter URL must be specified!" );

this.url = url;
this.method = method;
this.method = (method == null ? "post" : method);
this.username = username;
this.password = password;
this.regex = regex;
if ( xpath != null ) {
if ( !isBlank(xpath) ) {
try {
this.xpath = XPathFactory.newInstance().newXPath().compile(xpath);
} catch ( XPathException ex ) {
log.warn("Error parsing xpath ({}): {}", xpath, ex );
throw new IllegalArgumentException("Error parsing xpath" + xpath, ex);
}
}
this.client = buildClient();
Expand All @@ -123,12 +124,13 @@ protected HttpClient buildClient() {
* Instantiate a request object based on the method variable.
**/
private HttpUriRequest minterRequest() {
if ( method != null && method.equalsIgnoreCase("GET") ) {
return new HttpGet(url);
} else if ( method != null && method.equalsIgnoreCase("PUT") ) {
return new HttpPut(url);
} else {
return new HttpPost(url);
switch (method) {
case "GET": case "get":
return new HttpGet(url);
case "PUT": case "put":
return new HttpPut(url);
default:
return new HttpPost(url);
}
}

Expand Down
@@ -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 {
}

}
@@ -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();
}

}

Expand Up @@ -53,6 +53,24 @@ public void testMintPid() throws Exception {
assertEquals( pid, "baz" );
}

@Test
public void testMintPidNullHttpMethod() throws Exception {
final HttpPidMinter testMinter = new HttpPidMinter(
"http://localhost/minter", null, "", "", ".*/", "");

final HttpClient mockClient = mock(HttpClient.class);
final HttpResponse mockResponse = mock(HttpResponse.class);
final ByteArrayEntity entity = new ByteArrayEntity("/foo/bar/baz".getBytes());
testMinter.client = mockClient;

when(mockClient.execute(isA(HttpUriRequest.class))).thenReturn(mockResponse);
when(mockResponse.getEntity()).thenReturn(entity);

final String pid = testMinter.mintPid();
verify(mockClient).execute(isA(HttpUriRequest.class));
assertEquals( pid, "baz" );
}

@Test
public void testMintPidXPath() throws Exception {
final HttpPidMinter testMinter = new HttpPidMinter(
Expand All @@ -68,7 +86,7 @@ public void testMintPidXPath() throws Exception {

final String pid = testMinter.mintPid();
verify(mockClient).execute(isA(HttpUriRequest.class));
assertEquals( pid, "baz" );
assertEquals(pid, "baz");
}

@Test
Expand Down
16 changes: 16 additions & 0 deletions fcrepo-kernel-impl/src/test/resources/test-container.xml
@@ -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>

0 comments on commit cd2d11c

Please sign in to comment.