Skip to content

Commit

Permalink
update webhooks to use injectable sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jun 14, 2013
1 parent d4c1914 commit a908b46
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
Expand Up @@ -31,13 +31,16 @@
import org.fcrepo.AbstractResource;
import org.fcrepo.messaging.legacy.LegacyMethod;
import org.fcrepo.observer.FedoraEvent;
import org.fcrepo.session.InjectedSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.google.common.eventbus.Subscribe;

@Path("/fcr:webhooks")
@Scope("prototype")
@Component
public class FedoraWebhooks extends AbstractResource {

Expand All @@ -55,10 +58,14 @@ public class FedoraWebhooks extends AbstractResource {

protected static HttpClient client;


@InjectedSession
protected Session session;

/**
* For use with non-mutating methods.
*/
private static Session readOnlySession;
private Session readOnlySession;

static {
connectionManager.setMaxTotal(Integer.MAX_VALUE);
Expand Down Expand Up @@ -88,7 +95,7 @@ public void initialize() throws
* @param event
* @throws RepositoryException
*/
public static void runHooks(final Node resource, final FedoraEvent event)
public void runHooks(final Node resource, final FedoraEvent event)
throws RepositoryException {
if (resource == null) {
LOGGER.warn("resource node is null; event path is {}", event.getPath());
Expand Down Expand Up @@ -128,7 +135,7 @@ public static void runHooks(final Node resource, final FedoraEvent event)
public Response showWebhooks() throws RepositoryException {

final NodeIterator webhooksIterator =
readOnlySession.getRootNode().getNodes("webhook:*");
session.getRootNode().getNodes("webhook:*");
final StringBuilder str = new StringBuilder();

while (webhooksIterator.hasNext()) {
Expand All @@ -147,8 +154,6 @@ public Response registerWebhook(@PathParam("id")
final String id, @FormParam("callbackUrl")
final String callbackUrl) throws RepositoryException {

final Session session = getAuthenticatedSession();

final Node n =
jcrTools.findOrCreateChild(session.getRootNode(), "webhook:" +
id, "webhook:callback");
Expand All @@ -165,9 +170,6 @@ public Response registerWebhook(@PathParam("id")
@Path("{id}")
public Response registerWebhook(@PathParam("id")
final String id) throws RepositoryException {

final Session session = getAuthenticatedSession();

final Node n =
jcrTools.findOrCreateChild(session.getRootNode(), "webhook:" +
id, WEBHOOK_JCR_TYPE);
Expand Down Expand Up @@ -196,6 +198,15 @@ public void onEvent(final FedoraEvent event) {
}
}

public void setSession(final Session session) {
this.session = session;
}


public void setReadOnlySessionSession(final Session session) {
this.readOnlySession = session;
}

@PostConstruct
public final void getSession() {
try {
Expand Down
@@ -1,24 +1,18 @@
package org.fcrepo.webhooks;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.SecurityContext;

import org.fcrepo.AbstractResource;
import org.fcrepo.observer.FedoraEvent;
import org.fcrepo.session.SessionFactory;
import org.fcrepo.test.util.TestHelpers;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.jcr.api.Repository;
Expand All @@ -37,8 +31,7 @@ public class FedoraWebhooksTest {
@Before
public void setUp() throws Exception {
testObj = new FedoraWebhooks();
SessionFactory mockSessions = mock(SessionFactory.class);
mockSession = mock(Session.class);
mockSession = TestHelpers.mockSession(testObj);
mockRoot = mock(Node.class);
when(mockSession.getRootNode()).thenReturn(mockRoot);
Repository mockRepo = mock(Repository.class);
Expand All @@ -48,10 +41,8 @@ public void setUp() throws Exception {
NodeTypeManager mockNT = mock(NodeTypeManager.class);
when(mockWS.getNodeTypeManager()).thenReturn(mockNT);
when(mockSession.getWorkspace()).thenReturn(mockWS);
when(mockSessions.getSession()).thenReturn(mockSession);
when(mockSessions.getSession(any(SecurityContext.class), any(HttpServletRequest.class)))
.thenReturn(mockSession);
testObj.setSessionFactory(mockSessions);
testObj.setSession(mockSession);
testObj.setReadOnlySessionSession(mockSession);
}

@Test
Expand All @@ -64,6 +55,17 @@ public void testInitialize() throws Exception {

@Test
public void testShowWebhooks() throws Exception {

NodeIterator mockNodes = mock(NodeIterator.class);

Node mockHook = mock(Node.class);
Property mockProp = mock(Property.class);
when(mockHook.getProperty(FedoraWebhooks.WEBHOOK_CALLBACK_PROPERTY)).thenReturn(mockProp);

when(mockNodes.hasNext()).thenReturn(true, false);
when(mockNodes.nextNode()).thenReturn(mockHook).thenThrow(IndexOutOfBoundsException.class);
when(mockRoot.getNodes(FedoraWebhooks.WEBHOOK_SEARCH)).thenReturn(mockNodes);

testObj.showWebhooks();
}

Expand Down
2 changes: 1 addition & 1 deletion fcrepo-webhooks/src/test/resources/spring-test/rest.xml
Expand Up @@ -15,7 +15,7 @@

<context:annotation-config/>

<context:component-scan base-package="org.fcrepo.services, org.fcrepo.api, org.fcrepo.webhooks, org.fcrepo.integration.webhooks, org.fcrepo.serialization, org.fcrepo.exceptionhandlers"/>
<context:component-scan base-package="org.fcrepo.services, org.fcrepo.session, org.fcrepo.api, org.fcrepo.webhooks, org.fcrepo.integration.webhooks, org.fcrepo.serialization, org.fcrepo.exceptionhandlers"/>

<bean class="org.fcrepo.session.SessionFactory" />

Expand Down
Expand Up @@ -7,14 +7,12 @@

<context:property-placeholder/>

<!-- show stack traces for easier debugging -->
<bean id="wildcardExceptionmapper" class="org.fcrepo.exceptionhandlers.WildcardExceptionMapper" >
<property name="showStackTrace" value="true" />
</bean>


<context:property-placeholder/>

<bean id="containerWrapper" class="org.fcrepo.test.util.ContainerWrapper" init-method="start" destroy-method="stop" >
<property name="port" value="${test.port:8080}"/>
<property name="configLocation" value="classpath:web.xml" />
</bean>

</beans>
2 changes: 1 addition & 1 deletion fcrepo-webhooks/src/test/resources/web.xml
Expand Up @@ -38,7 +38,7 @@
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.fcrepo.api, org.fcrepo.webhooks, org.fcrepo.integration.webhooks, org.fcrepo.exceptionhandlers</param-value>
<param-value>org.fcrepo.api, org.fcrepo.session, org.fcrepo.webhooks, org.fcrepo.integration.webhooks, org.fcrepo.exceptionhandlers</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
Expand Down

0 comments on commit a908b46

Please sign in to comment.