Skip to content

Commit

Permalink
refactor: Added unit tests for MongoDB registry
Browse files Browse the repository at this point in the history
  • Loading branch information
aelamrani committed Jun 9, 2015
1 parent 36c597e commit 3d999fb
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public Set<Api> listAll() {
@Consumes(MediaType.APPLICATION_JSON)
public Response create(final Api api) {
if (getRegistry().createApi(api)) {
return Response.status(HttpStatusCode.CREATED_201).header(LOCATION, "/rest/apis" +
api.getContextPath()).entity(api).build();
return Response.status(HttpStatusCode.CREATED_201).header(LOCATION, "/rest/apis/" +
api.getName()).entity(api).build();
} else {
return Response.status(HttpStatusCode.BAD_REQUEST_400).build();
}
Expand Down
4 changes: 2 additions & 2 deletions admin/console/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/
import {ComponentAnnotation as Component, ViewAnnotation as View, bootstrap} from 'angular2/angular2';
import {GraviteeIo} from 'gravitee-io';
import {GraviteeIO} from 'gravitee-io';

@Component({
selector: 'main'
})

@View({
directives: [GraviteeIo],
directives: [GraviteeIO],
template: `
<gravitee-io></gravitee-io>
`
Expand Down
4 changes: 2 additions & 2 deletions admin/console/src/gravitee-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import {ComponentAnnotation as Component, ViewAnnotation as View} from 'angular2
templateUrl: 'gravitee-io.html'
})

export class GraviteeIo {
export class GraviteeIO {

constructor() {
console.info('GraviteeIo Component Mounted Successfully');
console.info('GraviteeIO Component Mounted Successfully');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* @author Azize Elamrani (azize dot elamrani at gmail dot com)
*/
public final class PropertiesUtils {
public class PropertiesUtils {

private PropertiesUtils() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ public boolean reloadApi(final String name) {

@Override
public boolean statusApi(final String name) {
final Api api = findApiByName(name);
final Api api = FluentIterable.from(apis).firstMatch(new Predicate<Api>() {
@Override
public boolean apply(final Api input) {
return input.getName().equals(name);
}
}).orNull();
if (api == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.core.spring;

import io.gravitee.gateway.api.Registry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.platforms.jetty.spring;

import io.gravitee.common.component.LifecycleComponent;
Expand Down
49 changes: 49 additions & 0 deletions gateway/registry/mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
<name>Gravitee IO - API Management - MongoDB</name>

<properties>
<embed.mongo.version>1.47.3</embed.mongo.version>
<embedmongo-maven-plugin.version>0.1.12</embedmongo-maven-plugin.version>
<mongo.version>3.0.1</mongo.version>
<powermock.version>1.6.2</powermock.version>
</properties>

<dependencies>
Expand All @@ -56,5 +59,51 @@
<artifactId>mongo-java-driver</artifactId>
<version>${mongo.version}</version>
</dependency>

<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>${embed.mongo.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.github.joelittlejohn.embedmongo</groupId>
<artifactId>embedmongo-maven-plugin</artifactId>
<version>${embedmongo-maven-plugin.version}</version>
<executions>
<execution>
<id>start</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public DBObject convertTo(final Api api) {
if (!Strings.isNullOrEmpty(api.getVersion())) {
dbObject.put("version", api.getVersion());
}
if (api.isEnabled()) {
dbObject.put("enabled", api.isEnabled());
}
dbObject.put("enabled", api.isEnabled());
if (api.getTargetURI() != null) {
dbObject.put("target", api.getTargetURI().getPath());
}
Expand All @@ -53,6 +51,9 @@ public DBObject convertTo(final Api api) {

@Override
public Api convertFrom(final DBObject dbobject) {
if (dbobject == null) {
return null;
}
final Api api = new Api();
final Object name = dbobject.get("name");
if (name != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.registry.mongodb;

import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.util.JSON;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory;
import io.gravitee.common.utils.PropertiesUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.bson.Document;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;
import java.util.Properties;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

/**
* @author Azize Elamrani (azize dot elamrani at gmail dot com)
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
@PrepareForTest(PropertiesUtils.class)
public abstract class AbstractMongoDBTest {

private static Logger LOG = LoggerFactory.getLogger(AbstractMongoDBTest.class);
private static String DATABASE_NAME = "gravitee-test";

private MongodForTestsFactory factory;
private MongoClient mongoClient;
private MongoDatabase mongoDatabase;

protected abstract String getJsonDataSetResourceName();

@Before
public void setup() throws Exception {
mockStatic(PropertiesUtils.class);

factory = MongodForTestsFactory.with(Version.Main.DEVELOPMENT);
mongoClient = factory.newMongo();
LOG.info("Creating database '{}'...", DATABASE_NAME);
mongoDatabase = mongoClient.getDatabase(DATABASE_NAME);

final ServerAddress mongoAddress = mongoClient.getAddress();
when(PropertiesUtils.getProperty(any(Properties.class), eq("gravitee.io.mongodb.host")))
.thenReturn(mongoAddress.getHost());
when(PropertiesUtils.getProperty(any(Properties.class), eq("gravitee.io.mongodb.database")))
.thenReturn(mongoDatabase.getName());
when(PropertiesUtils.getPropertyAsInteger(any(Properties.class), eq("gravitee.io.mongodb.port")))
.thenReturn(mongoAddress.getPort());

final File file = new File(AbstractMongoDBTest.class.getResource(getJsonDataSetResourceName()).toURI());

final String collectionName = FilenameUtils.getBaseName(file.getName());
LOG.info("Creating collection '{}'...", collectionName);
final MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
final List<DBObject> dbObjects = (List<DBObject>) JSON.parse(FileUtils.readFileToString(file));
for (final DBObject dbObject : dbObjects) {
final Document document = new Document();
for (final String key : dbObject.keySet()) {
document.put(key, dbObject.get(key));
}
collection.insertOne(document);
}
}

@After
public void teardown() throws Exception {
if (factory != null) {
factory.shutdown();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.registry.mongodb;

import io.gravitee.gateway.api.Registry;
import io.gravitee.model.Api;
import org.junit.Before;
import org.junit.Test;

import java.net.URI;

import static org.junit.Assert.*;

/**
* @author Azize Elamrani (azize dot elamrani at gmail dot com)
*/
public class MongoDBRegistryTest extends AbstractMongoDBTest {

private static final String APIS_DATA = "/data/apis.json";

private Registry registry;

@Override
protected String getJsonDataSetResourceName() {
return APIS_DATA;
}

@Before
public void init() {
registry = new MongoDBRegistry(this.getClass().getResource(APIS_DATA).getPath());
}

@Test
public void shouldListAll() {
assertEquals(1, registry.listAll().size());
}

@Test
public void shouldCreateApi() {
final String name = "api-users";
final Api api = new Api();
api.setName(name);
api.setPublicURI(URI.create("http://localhost:8082/users"));
api.setTargetURI(URI.create("http://localhost:8083/app/users"));
assertTrue(registry.createApi(api));
assertEquals(1, registry.listAll().size());
registry.reloadApi(name);
assertEquals(2, registry.listAll().size());
}

@Test
public void shouldNotCreateApi() {
final String name = "api-users";
final Api api = new Api();
api.setName(name);
assertFalse(registry.createApi(api));
assertEquals(1, registry.listAll().size());
registry.reloadApi(name);
assertEquals(1, registry.listAll().size());
}

@Test
public void shouldStartAndStopApi() {
final String name = "api-test";
// stop API
assertTrue(registry.statusApi(name));
assertTrue(registry.stopApi(name));
assertTrue(registry.statusApi(name));
assertTrue(registry.reloadAll());
assertFalse(registry.statusApi(name));

// start API
assertTrue(registry.startApi(name));
assertFalse(registry.statusApi(name));
assertTrue(registry.reloadAll());
assertTrue(registry.statusApi(name));
}

@Test
public void shouldFindMatchingApi() {
assertNotNull(registry.findMatchingApi("/test"));
assertNull(registry.findMatchingApi("/myapi"));
}
}
Loading

0 comments on commit 3d999fb

Please sign in to comment.