Skip to content

Commit

Permalink
Issue #111: Fully test JMXProxyResource.
Browse files Browse the repository at this point in the history
  • Loading branch information
mk23 committed Apr 16, 2016
1 parent 07410f4 commit a2600c1
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 24 deletions.
18 changes: 2 additions & 16 deletions src/main/java/com/github/mk23/jmxproxy/JMXProxyResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,6 @@ private Response getJMXHost(
final ConnectionCredentials auth
) {
Host host = manager.getHost(hostName + ":" + port, auth);
if (host == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}

return Response.ok(full ? host.setLimit(limit) : host.getMBeans()).build();
}

Expand All @@ -491,12 +487,7 @@ private Response getJMXHost(
final int limit,
final ConnectionCredentials auth
) {
Host host = manager.getHost(hostName + ":" + port, auth);
if (host == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}

MBean mbean = host.getMBean(mbeanName);
MBean mbean = manager.getHost(hostName + ":" + port, auth).getMBean(mbeanName);
if (mbean == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
Expand All @@ -512,12 +503,7 @@ private Response getJMXHost(
final int limit,
final ConnectionCredentials auth
) {
Host host = manager.getHost(hostName + ":" + port, auth);
if (host == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}

MBean mbean = host.getMBean(mbeanName);
MBean mbean = manager.getHost(hostName + ":" + port, auth).getMBean(mbeanName);
if (mbean == null || !mbean.hasAttribute(attribute)) {
return Response.status(Response.Status.NOT_FOUND).build();
}
Expand Down
175 changes: 167 additions & 8 deletions src/test/java/com/github/mk23/jmxproxy/tests/JMXProxyResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import com.github.mk23.jmxproxy.jmx.ConnectionCredentials;
import com.github.mk23.jmxproxy.jmx.ConnectionManager;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.dropwizard.testing.FixtureHelpers;
import io.dropwizard.testing.junit.ResourceTestRule;
import io.dropwizard.util.Duration;

Expand Down Expand Up @@ -91,6 +95,22 @@ public void shutdownManager() {
manager.stop();
}

/* Config tests */
@Test
public void checkValidConfig() throws Exception {
manager.getConfiguration()
.setCleanInterval(Duration.milliseconds(12))
.setCacheDuration(Duration.seconds(23))
.setAccessDuration(Duration.seconds(404276))
.setHistorySize(11)
.setAllowedEndpoints(Arrays.asList("localhost:1100", "remotehost:2211"));

ObjectMapper om = new ObjectMapper();
String expected = om.writeValueAsString(om.readValue(FixtureHelpers.fixture("fixtures/web_config.json"), JsonNode.class));
String acquired = resources.client().target("/config").request().get(String.class);
assertTrue(expected.equals(acquired));
}

/* Whitelist tests */
@Test
public void checkValidHostNoWhitelist() throws Exception {
Expand All @@ -115,15 +135,29 @@ public void checkInvalidHostWhitelist() throws Exception {

/* Auth tests */
@Test
public void checkValidAuthJson() throws Exception {
public void checkValidAuthJsonHost() throws Exception {
assumeNotNull(passwdFile);

List result = resources.client().target("/" + validHost).request().post(Entity.json(validAuth), List.class);
assertTrue(result.contains(validMBean));
}
@Test
public void checkValidAuthJsonMBean() throws Exception {
assumeNotNull(passwdFile);

List result = resources.client().target("/" + validHost + "/" + validMBean).request().post(Entity.json(validAuth), List.class);
assertTrue(result.contains(validAttribute));
}
@Test
public void checkValidAuthForm() throws Exception {
public void checkValidAuthJsonAttribute() throws Exception {
assumeNotNull(passwdFile);

int result = resources.client().target("/" + validHost + "/" + validMBean + "/" + validAttribute).request().post(Entity.json(validAuth), Integer.class);
assertTrue(result == validValue);
}

@Test
public void checkValidAuthFormHost() throws Exception {
assumeNotNull(passwdFile);

Form creds = new Form()
Expand All @@ -132,6 +166,26 @@ public void checkValidAuthForm() throws Exception {
List result = resources.client().target("/" + validHost).request().post(Entity.form(creds), List.class);
assertTrue(result.contains(validMBean));
}
@Test
public void checkValidAuthFormMBean() throws Exception {
assumeNotNull(passwdFile);

Form creds = new Form()
.param("username", validAuth.getUsername())
.param("password", validAuth.getPassword());
List result = resources.client().target("/" + validHost + "/" + validMBean).request().post(Entity.form(creds), List.class);
assertTrue(result.contains(validAttribute));
}
@Test
public void checkValidAuthFormAttribute() throws Exception {
assumeNotNull(passwdFile);

Form creds = new Form()
.param("username", validAuth.getUsername())
.param("password", validAuth.getPassword());
int result = resources.client().target("/" + validHost + "/" + validMBean + "/" + validAttribute).request().post(Entity.form(creds), Integer.class);
assertTrue(result == validValue);
}

@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthGet() {
Expand All @@ -141,7 +195,7 @@ public void checkInvalidAuthGet() {
}

@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthJson() {
public void checkInvalidAuthJsonHost() {
assumeNotNull(passwdFile);

final ConnectionCredentials invalidAuth = new ConnectionCredentials(
Expand All @@ -150,46 +204,136 @@ public void checkInvalidAuthJson() {
);
resources.client().target("/" + validHost).request().post(Entity.json(invalidAuth), List.class);
}
@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthJsonMBean() {
assumeNotNull(passwdFile);

final ConnectionCredentials invalidAuth = new ConnectionCredentials(
UUID.randomUUID().toString(),
UUID.randomUUID().toString()
);
resources.client().target("/" + validHost + "/" + validMBean).request().post(Entity.json(invalidAuth), List.class);
}
@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthJsonAttribute() {
assumeNotNull(passwdFile);

final ConnectionCredentials invalidAuth = new ConnectionCredentials(
UUID.randomUUID().toString(),
UUID.randomUUID().toString()
);
resources.client().target("/" + validHost + "/" + validMBean + "/" + validAttribute).request().post(Entity.json(invalidAuth), Integer.class);
}

@Test(expected=ClientErrorException.class)
public void checkInvalidAuthJsonNull() {
public void checkInvalidAuthJsonNullHost() {
assumeNotNull(passwdFile);

resources.client().target("/" + validHost).request().post(Entity.json(null), List.class);
}
@Test(expected=ClientErrorException.class)
public void checkInvalidAuthJsonNullMBean() {
assumeNotNull(passwdFile);

resources.client().target("/" + validHost + "/" + validMBean).request().post(Entity.json(null), List.class);
}
@Test(expected=ClientErrorException.class)
public void checkInvalidAuthJsonEmpty() {
public void checkInvalidAuthJsonNullAttribute() {
assumeNotNull(passwdFile);

resources.client().target("/" + validHost + "/" + validMBean + "/" + validAttribute).request().post(Entity.json(null), Integer.class);
}

@Test(expected=ClientErrorException.class)
public void checkInvalidAuthJsonEmptyHost() {
assumeNotNull(passwdFile);

resources.client().target("/" + validHost).request().post(Entity.json(new HashMap()), List.class);
}
@Test(expected=ClientErrorException.class)
public void checkInvalidAuthJsonEmptyMBean() {
assumeNotNull(passwdFile);

resources.client().target("/" + validHost + "/" + validMBean).request().post(Entity.json(new HashMap()), List.class);
}
@Test(expected=ClientErrorException.class)
public void checkInvalidAuthJsonEmptyAttribute() {
assumeNotNull(passwdFile);

resources.client().target("/" + validHost + "/" + validMBean + "/" + validAttribute).request().post(Entity.json(new HashMap()), Integer.class);
}

@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthForm() {
public void checkInvalidAuthFormHost() {
assumeNotNull(passwdFile);

Form invalidAuth = new Form()
.param("username", UUID.randomUUID().toString())
.param("password", UUID.randomUUID().toString());
resources.client().target("/" + validHost).request().post(Entity.form(invalidAuth), List.class);
}
@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthFormMBean() {
assumeNotNull(passwdFile);

Form invalidAuth = new Form()
.param("username", UUID.randomUUID().toString())
.param("password", UUID.randomUUID().toString());
resources.client().target("/" + validHost + "/" + validMBean).request().post(Entity.form(invalidAuth), List.class);
}
@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthFormNull() {
public void checkInvalidAuthFormAttribute() {
assumeNotNull(passwdFile);

Form invalidAuth = new Form()
.param("username", UUID.randomUUID().toString())
.param("password", UUID.randomUUID().toString());
resources.client().target("/" + validHost + "/" + validMBean + "/" + validAttribute).request().post(Entity.form(invalidAuth), Integer.class);
}

@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthFormNullHost() {
assumeNotNull(passwdFile);

Form invalidAuth = null;
resources.client().target("/" + validHost).request().post(Entity.form(invalidAuth), List.class);
}
@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthFormNullMBean() {
assumeNotNull(passwdFile);

Form invalidAuth = null;
resources.client().target("/" + validHost + "/" + validMBean).request().post(Entity.form(invalidAuth), List.class);
}
@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthFormNullAttribute() {
assumeNotNull(passwdFile);

Form invalidAuth = null;
resources.client().target("/" + validHost + "/" + validMBean + "/" + validAttribute).request().post(Entity.form(invalidAuth), Integer.class);
}

@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthFormEmpty() {
public void checkInvalidAuthFormEmptyHost() {
assumeNotNull(passwdFile);

Form invalidAuth = new Form();
resources.client().target("/" + validHost).request().post(Entity.form(invalidAuth), List.class);
}
@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthFormEmptyMBean() {
assumeNotNull(passwdFile);

Form invalidAuth = new Form();
resources.client().target("/" + validHost + "/" + validMBean).request().post(Entity.form(invalidAuth), List.class);
}
@Test(expected=NotAuthorizedException.class)
public void checkInvalidAuthFormEmptyAttribute() {
assumeNotNull(passwdFile);

Form invalidAuth = new Form();
resources.client().target("/" + validHost + "/" + validMBean + "/" + validAttribute).request().post(Entity.form(invalidAuth), Integer.class);
}

/* Cache tests */
@Test
Expand Down Expand Up @@ -324,6 +468,11 @@ public void checkInvalidMBean() throws Exception {
requestWithAuth("/" + validHost + "/" + invalidMBean, List.class);
}

@Test(expected=NotFoundException.class)
public void checkValidMBeanInvalidHost() throws Exception {
requestWithAuth("/" + invalidHost + "/" + validMBean, List.class);
}

@Test
public void checkValidMBeanFullTrue() throws Exception {
Map result = requestWithAuth("/" + validHost + "/" + validMBean + "?full=true", Map.class);
Expand Down Expand Up @@ -414,6 +563,16 @@ public void checkInvalidAttribute() throws Exception {
requestWithAuth("/" + validHost + "/" + validMBean + "/" + invalidAttribute, String.class);
}

@Test(expected=NotFoundException.class)
public void checkValidAttributeInvalidHost() throws Exception {
requestWithAuth("/" + invalidHost + "/" + validMBean + "/" + validAttribute, String.class);
}

@Test(expected=NotFoundException.class)
public void checkValidAttributeInvalidMBean() throws Exception {
requestWithAuth("/" + validHost + "/" + invalidMBean + "/" + validAttribute, String.class);
}

@Test(expected=NotFoundException.class)
public void checkValidAttributeInvalidLimit() throws Exception {
requestWithAuth("/" + validHost + "/" + validMBean + "/" + validAttribute + "?limit=invalid", String.class);
Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/fixtures/web_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"clean_interval": 12,
"cache_duration": 23000,
"access_duration": 404276000,
"history_size": 11,
"allowed_endpoints": [
"localhost:1100",
"remotehost:2211"
]
}

0 comments on commit a2600c1

Please sign in to comment.