-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Added unit tests for MongoDB registry
- Loading branch information
Showing
15 changed files
with
321 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
gateway/core/src/main/java/io/gravitee/gateway/core/spring/CoreConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ms/jetty/src/main/java/io/gravitee/gateway/platforms/jetty/spring/JettyConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...istry/mongodb/src/test/java/io/gravitee/gateway/registry/mongodb/AbstractMongoDBTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...istry/mongodb/src/test/java/io/gravitee/gateway/registry/mongodb/MongoDBRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
Oops, something went wrong.