Skip to content

Commit

Permalink
Merge 2cbe958 into 6d1886b
Browse files Browse the repository at this point in the history
  • Loading branch information
BenNzewi committed Apr 9, 2019
2 parents 6d1886b + 2cbe958 commit e6d3fbc
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## [Unreleased]

## [2.0.0-M2] - 2019-04-08

### Changed
- Update framework-api to 4.0.0-M5
- Update framework to 6.0.0-M10
- Update event-store to 2.0.0-M10
- Update framework-generators to 2.0.0-M8

## [2.0.0-M1] - 2019-03-25

### Changed
Expand Down
73 changes: 73 additions & 0 deletions example-context/example-service/example-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,79 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-client-all</artifactId>
<version>${wildfly.version}</version>
<exclusions>
<exclusion>
<groupId>org.jboss</groupId>
<artifactId>jboss-ejb-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss</groupId>
<artifactId>jboss-remote-naming</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-river</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.remoting</groupId>
<artifactId>jboss-remoting</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.sasl</groupId>
<artifactId>jboss-sasl</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.xnio</groupId>
<artifactId>xnio-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.xnio</groupId>
<artifactId>xnio-nio</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</exclusion>
<exclusion>
<groupId>org.jgroups</groupId>
<artifactId>jgroups</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-core-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-commons</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-hqclient-protocol</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>uk.gov.justice.services</groupId>
<artifactId>jmx</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
import static com.jayway.jsonassert.JsonAssert.with;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static java.util.Optional.ofNullable;
import static java.util.UUID.fromString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopMediaTypes.CONTEXT_NAME;

import uk.gov.justice.services.event.buffer.core.repository.subscription.Subscription;
import uk.gov.justice.services.example.cakeshop.it.helpers.ApiResponse;
import uk.gov.justice.services.example.cakeshop.it.helpers.CakeShopRepositoryManager;
import uk.gov.justice.services.example.cakeshop.it.helpers.CommandFactory;
import uk.gov.justice.services.example.cakeshop.it.helpers.EventFactory;
import uk.gov.justice.services.example.cakeshop.it.helpers.EventFinder;
import uk.gov.justice.services.example.cakeshop.it.helpers.Querier;
import uk.gov.justice.services.example.cakeshop.it.helpers.RestEasyClientFactory;
import uk.gov.justice.services.test.utils.core.messaging.Poller;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package uk.gov.justice.services.example.cakeshop.it;

import uk.gov.justice.services.example.cakeshop.it.helpers.MBeanHelper;
import uk.gov.justice.services.jmx.ShutteringMBean;

import java.io.IOException;

import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class JmxMBeanIT {

private static final String SHUTTERING_DOMAIN = "shuttering";
private static final String SHUTTERING = "Shuttering";

private MBeanHelper mBeanHelper;
private JMXConnector jmxConnector;
private ShutteringMBean shutteringMBean;

@Before
public void before() throws IOException {
mBeanHelper = new MBeanHelper();
jmxConnector = mBeanHelper.getJMXConnector();
}

@After
public void cleanup() {
//invoke unshuttering - Always ensure unshutter is invoked as we cannot guarantee order of execution for other Cakeshop IT's
shutteringMBean.doUnshutteringRequested();
}

@Test
public void shouldInvokeShuttering() throws Exception {
shutteringMBean = getMbeanProxy(SHUTTERING_DOMAIN, ShutteringMBean.class, SHUTTERING);
shutteringMBean.doShutteringRequested();
}

@Test
public void shouldInvokeUnShuttering() throws Exception {
shutteringMBean = getMbeanProxy(SHUTTERING_DOMAIN, ShutteringMBean.class, SHUTTERING);
shutteringMBean.doUnshutteringRequested();
}

private <T> T getMbeanProxy(final String domain, final Class<T> mBeanInterface, final String mBeanClassName) throws IOException, MalformedObjectNameException, IntrospectionException, InstanceNotFoundException, ReflectionException {
final MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

final ObjectName objectName = new ObjectName(domain, "type", mBeanClassName);

mBeanHelper.getMbeanDomains(connection);

mBeanHelper.getMbeanOperations(objectName, connection);

return mBeanHelper.getMbeanProxy(connection, objectName, mBeanInterface);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package uk.gov.justice.services.example.cakeshop.it;

import static com.jayway.awaitility.Awaitility.await;
import static com.jayway.jsonassert.JsonAssert.with;
import static java.lang.String.format;
import static java.util.UUID.randomUUID;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static javax.ws.rs.core.Response.Status.OK;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.slf4j.LoggerFactory.getLogger;
import static uk.gov.justice.services.test.utils.core.matchers.HttpStatusCodeMatcher.isStatus;

import uk.gov.justice.services.example.cakeshop.it.helpers.ApiResponse;
import uk.gov.justice.services.example.cakeshop.it.helpers.CakeShopRepositoryManager;
import uk.gov.justice.services.example.cakeshop.it.helpers.CommandSender;
import uk.gov.justice.services.example.cakeshop.it.helpers.EventFactory;
import uk.gov.justice.services.example.cakeshop.it.helpers.MBeanHelper;
import uk.gov.justice.services.example.cakeshop.it.helpers.Querier;
import uk.gov.justice.services.example.cakeshop.it.helpers.RestEasyClientFactory;
import uk.gov.justice.services.jmx.Shuttering;
import uk.gov.justice.services.jmx.ShutteringMBean;

import java.io.IOException;

import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;

public class ShutteringIT {

private static final CakeShopRepositoryManager CAKE_SHOP_REPOSITORY_MANAGER = new CakeShopRepositoryManager();

private static final Logger logger = getLogger(ShutteringIT.class);
private static final String MARBLE_CAKE = "Marble cake";
private static final String CARROT_CAKE = "Carrot cake";

private final EventFactory eventFactory = new EventFactory();

private Client client;
private Querier querier;
private CommandSender commandSender;

private MBeanHelper mBeanHelper;

@BeforeClass
public static void beforeClass() throws Exception {
CAKE_SHOP_REPOSITORY_MANAGER.initialise();
}

@Before
public void before() {
client = new RestEasyClientFactory().createResteasyClient();
querier = new Querier(client);
commandSender = new CommandSender(client, eventFactory);
mBeanHelper = new MBeanHelper();
}

@After
public void cleanup() throws MalformedObjectNameException, IntrospectionException, ReflectionException, InstanceNotFoundException, IOException {
client.close();

//invoke unshuttering - Always ensure unshutter is invoked as we cannot guarantee order of execution for other Cakeeshop IT's
invokeShuttering(false);
}

@Test
public void shouldNotReturnRecipesAfterShuttering() throws MalformedObjectNameException, IntrospectionException, ReflectionException, InstanceNotFoundException, IOException {
//invoke shuttering
invokeShuttering(true);

//add 2 recipes
final String recipeId = addRecipe(MARBLE_CAKE);
final String recipeId2 = addRecipe(CARROT_CAKE);

//check recipes have not been added due to shuttering
verifyRecipeAdded(recipeId, recipeId2, null, null, false, NOT_FOUND);
}

@Test
public void shouldQueryForRecipesAfterUnShuttering() throws MalformedObjectNameException, IntrospectionException, ReflectionException, InstanceNotFoundException, IOException {
//invoke shuttering
invokeShuttering(true);

//add more recipes
final String recipeId = addRecipe(MARBLE_CAKE);
final String recipeId2 = addRecipe(CARROT_CAKE);

//check recipes have not been added due to shuttering
verifyRecipeAdded(recipeId, recipeId2, null, null, false, NOT_FOUND);

//invoke unshuttering
invokeShuttering(false);

////check new recipes have been added successfully after unshuttering
verifyRecipeAdded(recipeId, recipeId2, MARBLE_CAKE, CARROT_CAKE,true, OK);
}

private void verifyRecipeAdded(final String recipeId,
final String recipeId2,
final String recipeName,
final String recipeName2,
final boolean checkRecipeName,
final Status status) {
await().until(() -> {
if(checkRecipeName) {
final ApiResponse response = querier.recipesQueryResult();
assertThat(response.httpCode(), isStatus(status));
logger.info(format("Response: %s", response.httpCode()));

with(response.body())
.assertThat("$.recipes[?(@.id=='" + recipeId + "')].name", hasItem(recipeName))
.assertThat("$.recipes[?(@.id=='" + recipeId2 + "')].name", hasItem(recipeName2));
} else {
final ApiResponse response = querier.queryForRecipe(recipeId);
logger.info(format("Response: %s", response.httpCode()));
assertThat(response.httpCode(), isStatus(status));
}
});
}

private void invokeShuttering(final boolean isShutteringRequired) throws IOException, MalformedObjectNameException, IntrospectionException, InstanceNotFoundException, ReflectionException {
try(JMXConnector jmxConnector = mBeanHelper.getJMXConnector()){

final MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

final ObjectName objectName = new ObjectName("shuttering", "type", Shuttering.class.getSimpleName());

mBeanHelper.getMbeanOperations(objectName, connection);

if(isShutteringRequired) {
mBeanHelper.getMbeanProxy(connection, objectName, ShutteringMBean.class).doShutteringRequested();
} else {
mBeanHelper.getMbeanProxy(connection, objectName, ShutteringMBean.class).doUnshutteringRequested();
}
}
}

private String addRecipe(final String cakeName) {
final String recipeId = randomUUID().toString();
commandSender.addRecipe(recipeId, cakeName);
return recipeId;
}
}
Loading

0 comments on commit e6d3fbc

Please sign in to comment.