Skip to content

Commit

Permalink
Minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Jun 26, 2015
1 parent f13ca21 commit eb56a98
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 77 deletions.
Expand Up @@ -15,21 +15,21 @@
*/
package org.fcrepo.jms.headers;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.stream.Collectors.joining;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.util.Set;
import java.util.stream.Collectors;

import javax.jms.JMSException;
import javax.jms.Message;

import org.apache.commons.lang.StringUtils;
import org.fcrepo.jms.observer.JMSEventMessageFactory;
import org.fcrepo.kernel.observer.FedoraEvent;
import org.fcrepo.kernel.utils.EventType;

import org.slf4j.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -83,7 +83,7 @@ public Message getMessage(final FedoraEvent jcrEvent,
// extract baseURL and userAgent from event UserData
try {
final String userdata = jcrEvent.getUserData();
if (!StringUtils.isBlank(userdata)) {
if (!isNullOrEmpty(userdata)) {
final ObjectMapper mapper = new ObjectMapper();
final JsonNode json = mapper.readTree(userdata);
String url = json.get("baseURL").asText();
Expand Down Expand Up @@ -120,7 +120,7 @@ private static String getEventURIs(final Set<Integer> types) {
.map(EventType::valueOf)
.map(EventType::toString)
.map(REPOSITORY_NAMESPACE::concat)
.collect(Collectors.joining(","));
.collect(joining(","));

LOGGER.debug("Constructed event type URIs: {}", uris);
return uris;
Expand Down
Expand Up @@ -16,7 +16,6 @@
package org.fcrepo.integration.jms.observer;

import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.any;
import static com.jayway.awaitility.Awaitility.await;
import static com.jayway.awaitility.Duration.ONE_SECOND;
import static javax.jcr.observation.Event.NODE_ADDED;
Expand All @@ -38,8 +37,6 @@
import java.io.ByteArrayInputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;

import javax.inject.Inject;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
Expand Down Expand Up @@ -70,8 +67,6 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.common.base.Predicate;


/**
* <p>
Expand Down Expand Up @@ -196,24 +191,14 @@ public void testMetadataEvents() throws RepositoryException {
}

private void awaitMessageOrFail(final String id, final String eventType, final String property) {
await().pollInterval(ONE_SECOND).until(new Callable<Boolean>() {

@Override
public Boolean call() {
return any(messages, new Predicate<Message>() {

@Override
public boolean apply(final Message message) {
try {
return getPath(message).equals(id) && getEventTypes(message).contains(eventType)
&& (property == null || getProperties(message).contains(property));
} catch (final JMSException e) {
throw propagate(e);
}
}
});
await().pollInterval(ONE_SECOND).until(() -> messages.stream().anyMatch(msg -> {
try {
return getPath(msg).equals(id) && getEventTypes(msg).contains(eventType)
&& (property == null || getProperties(msg).contains(property));
} catch (final JMSException e) {
throw propagate(e);
}
});
}));
}

@Test(timeout = TIMEOUT)
Expand Down Expand Up @@ -259,7 +244,7 @@ public void acquireConnection() throws JMSException {
@After
public void releaseConnection() throws JMSException {
// ignore any remaining or queued messages
consumer.setMessageListener(new NoopListener());
consumer.setMessageListener(msg -> { });
// and shut the listening machinery down
LOGGER.debug(this.getClass().getName() + " releasing JMS connection.");
consumer.close();
Expand Down

This file was deleted.

Expand Up @@ -15,6 +15,7 @@
*/
package org.fcrepo.jms.headers;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Collections.singleton;
import static javax.jcr.observation.Event.NODE_ADDED;
import static org.fcrepo.jms.headers.DefaultMessageFactory.BASE_URL_HEADER_NAME;
Expand All @@ -29,29 +30,30 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doThrow;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;

import java.util.Set;

import javax.jcr.RepositoryException;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.commons.lang.StringUtils;
import org.fcrepo.kernel.observer.FedoraEvent;
import org.fcrepo.kernel.utils.EventType;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

/**
* <p>DefaultMessageFactoryTest class.</p>
*
* @author ajs6f
*/
@RunWith(MockitoJUnitRunner.class)
public class DefaultMessageFactoryTest {

@Mock
Expand All @@ -64,46 +66,43 @@ public class DefaultMessageFactoryTest {

@Before
public void setUp() throws JMSException {
initMocks(this);
when(mockSession.createMessage()).thenReturn(
new ActiveMQObjectMessage());
when(mockSession.createMessage()).thenReturn(new ActiveMQObjectMessage());
testDefaultMessageFactory = new DefaultMessageFactory();
}

@Test
public void testBuildMessage() throws RepositoryException, JMSException {
public void testBuildMessage() throws JMSException {
final String testPath = "/path/to/resource";
final String userAgent = "Test UserAgent (Like Mozilla)";
final Message msg = doTestBuildMessage("base-url", "Test UserAgent", testPath);
assertEquals("Got wrong identifier in message!", testPath, msg.getStringProperty(IDENTIFIER_HEADER_NAME));
}

@Test (expected = Exception.class)
public void testBuildMessageException() throws RepositoryException, JMSException {
public void testBuildMessageException() throws JMSException {
doThrow(Exception.class).when(mockEvent).getUserData();
testDefaultMessageFactory.getMessage(mockEvent, mockSession);
}

@Test
public void testBuildMessageNullUrl() throws RepositoryException, JMSException {
public void testBuildMessageNullUrl() throws JMSException {
final String testPath = "/path/to/resource";
final Message msg = doTestBuildMessage(null, null, testPath);
assertEquals("Got wrong identifier in message!", testPath, msg.getStringProperty(IDENTIFIER_HEADER_NAME));
}
@Test
public void testBuildMessageContent() throws RepositoryException, JMSException {
public void testBuildMessageContent() throws JMSException {
final String testPath = "/path/to/resource";
final Message msg = doTestBuildMessage("base-url/", "Test UserAgent", testPath + "/" + JCR_CONTENT);
assertEquals("Got wrong identifier in message!", testPath, msg.getStringProperty(IDENTIFIER_HEADER_NAME));
}

private Message doTestBuildMessage(final String baseUrl, final String userAgent, final String id)
throws RepositoryException, JMSException {
throws JMSException {
final Long testDate = 46647758568747L;
when(mockEvent.getDate()).thenReturn(testDate);

String url = null;
if (!StringUtils.isBlank(baseUrl) || !StringUtils.isBlank(userAgent)) {
if (!isNullOrEmpty(baseUrl) || !isNullOrEmpty(userAgent)) {
url = "{\"baseURL\":\"" + baseUrl + "\",\"userAgent\":\"" + userAgent + "\"}";
}
when(mockEvent.getUserData()).thenReturn(url);
Expand All @@ -121,7 +120,7 @@ private Message doTestBuildMessage(final String baseUrl, final String userAgent,
final Message msg = testDefaultMessageFactory.getMessage(mockEvent, mockSession);

String trimmedBaseUrl = baseUrl;
while (!StringUtils.isBlank(trimmedBaseUrl) && trimmedBaseUrl.endsWith("/")) {
while (!isNullOrEmpty(trimmedBaseUrl) && trimmedBaseUrl.endsWith("/")) {
trimmedBaseUrl = trimmedBaseUrl.substring(0, trimmedBaseUrl.length() - 1);
}

Expand Down
Expand Up @@ -21,22 +21,24 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.springframework.test.util.ReflectionTestUtils.setField;

import java.io.IOException;

import javax.jcr.RepositoryException;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;

import org.apache.activemq.ActiveMQConnectionFactory;

import org.fcrepo.kernel.observer.FedoraEvent;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.google.common.eventbus.EventBus;

Expand All @@ -45,6 +47,7 @@
*
* @author awoods
*/
@RunWith(MockitoJUnitRunner.class)
public class JMSTopicPublisherTest {

private JMSTopicPublisher testJMSTopicPublisher;
Expand All @@ -70,7 +73,6 @@ public class JMSTopicPublisherTest {
@Before
public void setUp() {
testJMSTopicPublisher = new JMSTopicPublisher();
initMocks(this);
setField(testJMSTopicPublisher, "eventFactory", mockEventFactory);
setField(testJMSTopicPublisher, "producer", mockProducer);
setField(testJMSTopicPublisher, "connectionFactory", mockConnections);
Expand All @@ -87,7 +89,7 @@ public void testAcquireConnections() throws JMSException {
}

@Test
public void testPublishJCREvent() throws RepositoryException, IOException, JMSException {
public void testPublishJCREvent() throws IOException, JMSException {
final Message mockMsg = mock(Message.class);
final FedoraEvent mockEvent = mock(FedoraEvent.class);
when(mockEventFactory.getMessage(eq(mockEvent), any(javax.jms.Session.class))).thenReturn(mockMsg);
Expand Down

0 comments on commit eb56a98

Please sign in to comment.