Skip to content

Commit

Permalink
JAMES-2138 Webadmin default content type should be JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Sep 9, 2017
1 parent b421854 commit 4817caa
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 131 deletions.
Expand Up @@ -22,6 +22,7 @@
import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
import static org.apache.james.webadmin.Constants.JSON_CONTENT_TYPE;
import static org.apache.james.webadmin.Constants.SEPARATOR;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -161,6 +162,7 @@ public void getUsersShouldDisplayUsers() throws Exception {
.get(UserRoutes.USERS)
.then()
.statusCode(200)
.contentType(JSON_CONTENT_TYPE)
.body(is("[{\"username\":\"username@domain\"}]"));
}

Expand Down Expand Up @@ -203,6 +205,7 @@ public void getCurrentVersionShouldReturnNullForCurrentVersionAsBeginning() thro
.get(VERSION)
.then()
.statusCode(200)
.contentType(JSON_CONTENT_TYPE)
.body(is("{\"version\":null}"));
}

Expand All @@ -214,6 +217,7 @@ public void getLatestVersionShouldReturnTheConfiguredLatestVersion() throws Exce
.get(VERSION_LATEST)
.then()
.statusCode(200)
.contentType(JSON_CONTENT_TYPE)
.body(is("{\"version\":" + CassandraSchemaVersionManager.MAX_VERSION + "}"));
}

Expand All @@ -233,6 +237,7 @@ public void postShouldDoMigrationAndUpdateCurrentVersion() throws Exception {
.get(VERSION)
.then()
.statusCode(200)
.contentType(JSON_CONTENT_TYPE)
.body(is("{\"version\":" + CassandraSchemaVersionManager.MAX_VERSION + "}"));
}

Expand All @@ -251,6 +256,7 @@ public void postShouldDoMigrationAndUpdateToTheLatestVersion() throws Exception
.get(VERSION)
.then()
.statusCode(200)
.contentType(JSON_CONTENT_TYPE)
.body(is("{\"version\":" + CassandraSchemaVersionManager.MAX_VERSION + "}"));
}

Expand Down
Expand Up @@ -34,7 +34,6 @@

public class CassandraMigrationRoutes implements Routes {


private static final Logger LOGGER = LoggerFactory.getLogger(CassandraMigrationRoutes.class);

public static final String VERSION_BASE = "/cassandra/version";
Expand Down
Expand Up @@ -24,13 +24,14 @@
import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION;
import static org.hamcrest.CoreMatchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand All @@ -41,18 +42,17 @@
import org.apache.james.webadmin.WebAdminServer;
import org.apache.james.webadmin.service.CassandraMigrationService;
import org.apache.james.webadmin.utils.JsonTransformer;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.http.ContentType;

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

public class CassandraMigrationRoutesTest {

private static final Integer LATEST_VERSION = 3;
Expand Down Expand Up @@ -101,20 +101,33 @@ public void tearDown() {
public void getShouldReturnTheCurrentVersion() throws Exception {
when(schemaVersionDAO.getCurrentSchemaVersion()).thenReturn(CompletableFuture.completedFuture(Optional.of(CURRENT_VERSION)));

when()
.get()
.then()
.statusCode(200)
.body(is("{\"version\":2}"));
Integer version =
when()
.get()
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.extract()
.jsonPath()
.getInt("version");

assertThat(version).isEqualTo(CURRENT_VERSION);
}

@Test
public void getShouldReturnTheLatestVersionWhenSetUpTheLatestVersion() throws Exception {
when()
.get("/latest")
.then()
.statusCode(200)
.body(is("{\"version\":" + LATEST_VERSION + "}"));

Integer version =
when()
.get("/latest")
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.extract()
.jsonPath()
.getInt("version");

assertThat(version).isEqualTo(LATEST_VERSION);
}

@Ignore
Expand Down
Expand Up @@ -83,6 +83,7 @@ public void configure(HierarchicalConfiguration config) throws ConfigurationExce
configureCORS();
configureMetrics();
service.before(authenticationFilter);
service.before((request, response) -> response.type(Constants.JSON_CONTENT_TYPE));
configureMDC();
routesList.forEach(routes -> routes.define(service));
service.awaitInitialization();
Expand Down
Expand Up @@ -26,15 +26,15 @@
import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
import static org.apache.james.webadmin.Constants.SEPARATOR;
import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.apache.james.dnsservice.api.DNSService;
import org.apache.james.domainlist.api.DomainList;
Expand All @@ -51,6 +51,7 @@
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.http.ContentType;

import de.bechte.junit.runners.context.HierarchicalContextRunner;

@RunWith(HierarchicalContextRunner.class)
Expand Down Expand Up @@ -96,11 +97,18 @@ public void setUp() throws Exception {

@Test
public void getDomainsShouldBeEmptyByDefault() {
given()
.get()
.then()
.statusCode(200)
.body(is("[]"));
List<String> domains =
given()
.get()
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.extract()
.body()
.jsonPath()
.getList(".");

assertThat(domains).isEmpty();
}

@Test
Expand Down Expand Up @@ -132,11 +140,18 @@ public void getDomainsShouldDisplayAddedDomains() {
with()
.put(DOMAIN);

when()
.get()
.then()
.statusCode(200)
.body(containsString(DOMAIN));
List<String> domains =
when()
.get()
.then()
.contentType(ContentType.JSON)
.statusCode(200)
.extract()
.body()
.jsonPath()
.getList(".");

assertThat(domains).containsExactly(DOMAIN);
}

@Test
Expand Down Expand Up @@ -186,11 +201,18 @@ public void deleteShouldRemoveTheGivenDomain() {
.then()
.statusCode(204);

when()
.get()
.then()
.statusCode(200)
.body(is("[]"));
List<String> domains =
when()
.get()
.then()
.contentType(ContentType.JSON)
.statusCode(200)
.extract()
.body()
.jsonPath()
.getList(".");

assertThat(domains).isEmpty();
}

@Test
Expand Down
Expand Up @@ -25,14 +25,15 @@
import static com.jayway.restassured.config.EncoderConfig.encoderConfig;
import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import org.apache.james.domainlist.api.DomainList;
import org.apache.james.metrics.logger.DefaultMetricFactory;
Expand All @@ -48,9 +49,11 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.common.collect.ImmutableMap;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.http.ContentType;

import de.bechte.junit.runners.context.HierarchicalContextRunner;

@RunWith(HierarchicalContextRunner.class)
Expand Down Expand Up @@ -96,11 +99,18 @@ public void setUp() throws Exception {

@Test
public void getUsersShouldBeEmptyByDefault() {
when()
.get()
.then()
.statusCode(200)
.body(is("[]"));
List<Map<String, String>> users =
when()
.get()
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.extract()
.body()
.jsonPath()
.getList(".");

assertThat(users).isEmpty();
}

@Test
Expand Down Expand Up @@ -155,13 +165,20 @@ public void postShouldReturnRequireNonNullPassword() {
public void postShouldAddTheUser() {
with()
.body("{\"password\":\"password\"}")
.put(USERNAME);

when()
.get()
.then()
.statusCode(200)
.body(equalTo("[{\"username\":\"" + USERNAME + "\"}]"));
.put(USERNAME);

List<Map<String, String>> users =
when()
.get()
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.extract()
.body()
.jsonPath()
.getList(".");

assertThat(users).containsExactly(ImmutableMap.of("username", USERNAME));
}

@Test
Expand All @@ -180,11 +197,18 @@ public void postingTwoTimesShouldBeAllowed() {
.statusCode(204);

// Then
when()
.get()
.then()
.statusCode(200)
.body(equalTo("[{\"username\":\"" + USERNAME + "\"}]"));
List<Map<String, String>> users =
when()
.get()
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.extract()
.body()
.jsonPath()
.getList(".");

assertThat(users).containsExactly(ImmutableMap.of("username", USERNAME));
}

@Test
Expand Down Expand Up @@ -269,11 +293,18 @@ public void deleteShouldRemoveAssociatedUser() {
.statusCode(204);

// Then
when()
.get()
.then()
.statusCode(200)
.body(equalTo("[]"));
List<Map<String, String>> users =
when()
.get()
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.extract()
.body()
.jsonPath()
.getList(".");

assertThat(users).isEmpty();
}

@Test
Expand Down

0 comments on commit 4817caa

Please sign in to comment.