|
| 1 | +package grails.test.mongodb |
| 2 | + |
| 3 | +import com.mongodb.client.MongoClient |
| 4 | +import grails.config.Config |
| 5 | +import groovy.transform.CompileStatic |
| 6 | +import org.grails.config.PropertySourcesConfig |
| 7 | +import org.grails.datastore.mapping.core.DatastoreUtils |
| 8 | +import org.grails.datastore.mapping.core.Session |
| 9 | +import org.grails.datastore.mapping.core.exceptions.ConfigurationException |
| 10 | +import org.grails.datastore.mapping.model.MappingContext |
| 11 | +import org.grails.datastore.mapping.mongo.MongoDatastore |
| 12 | +import org.springframework.boot.env.PropertySourceLoader |
| 13 | +import org.springframework.core.env.PropertyResolver |
| 14 | +import org.springframework.core.env.PropertySource |
| 15 | +import org.springframework.core.io.DefaultResourceLoader |
| 16 | +import org.springframework.core.io.Resource |
| 17 | +import org.springframework.core.io.ResourceLoader |
| 18 | +import org.springframework.core.io.support.SpringFactoriesLoader |
| 19 | +import org.springframework.transaction.support.TransactionSynchronizationManager |
| 20 | +import spock.lang.AutoCleanup |
| 21 | +import spock.lang.Shared |
| 22 | +import spock.lang.Specification |
| 23 | + |
| 24 | +/** |
| 25 | + * Base class for MongoDB tests |
| 26 | + * |
| 27 | + * @author Álvaro Sánchez-Mariscal |
| 28 | + * @since 6.0.1 |
| 29 | + */ |
| 30 | +@CompileStatic |
| 31 | +abstract class MongoSpec extends Specification { |
| 32 | + |
| 33 | + @Shared |
| 34 | + @AutoCleanup |
| 35 | + MongoDatastore mongoDatastore |
| 36 | + |
| 37 | + @Shared |
| 38 | + Session mongoSession |
| 39 | + |
| 40 | + /** |
| 41 | + * @return Obtains the mapping context |
| 42 | + */ |
| 43 | + MappingContext getMappingContext() { |
| 44 | + mongoDatastore.getMappingContext() |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return The default mongo client |
| 49 | + */ |
| 50 | + MongoClient createMongoClient() { |
| 51 | + return null |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @return The domain classes |
| 56 | + */ |
| 57 | + protected List<Class> getDomainClasses() { [] } |
| 58 | + |
| 59 | + void setupSpec() { |
| 60 | + List<PropertySourceLoader> propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, getClass().getClassLoader()); |
| 61 | + ResourceLoader resourceLoader = new DefaultResourceLoader() |
| 62 | + |
| 63 | + List<PropertySource> propertySources = [] |
| 64 | + |
| 65 | + PropertySourceLoader ymlLoader = propertySourceLoaders.find { it.getFileExtensions().toList().contains("yml") } |
| 66 | + if (ymlLoader) { |
| 67 | + propertySources.addAll(load(resourceLoader, ymlLoader, "application.yml")) |
| 68 | + } |
| 69 | + PropertySourceLoader groovyLoader = propertySourceLoaders.find { it.getFileExtensions().toList().contains("groovy") } |
| 70 | + if (groovyLoader) { |
| 71 | + propertySources.addAll(load(resourceLoader, groovyLoader, "application.groovy")) |
| 72 | + } |
| 73 | + |
| 74 | + Map<String, Object> mapPropertySource = propertySources |
| 75 | + .findAll { it.getSource() } |
| 76 | + .collectEntries { it.getSource() as Map } |
| 77 | + |
| 78 | + Config config = new PropertySourcesConfig(mapPropertySource) |
| 79 | + |
| 80 | + List<Class> domainClasses = getDomainClasses() |
| 81 | + if (!domainClasses) { |
| 82 | + def packageToScan = getPackageToScan(config) |
| 83 | + MongoClient mongoClient = createMongoClient() |
| 84 | + def pkg = Package.getPackage(packageToScan) |
| 85 | + if(pkg == null) { |
| 86 | + throw new ConfigurationException("Package to scan [$packageToScan] cannot be found on the classpath") |
| 87 | + } |
| 88 | + |
| 89 | + if (mongoClient) { |
| 90 | + mongoDatastore = new MongoDatastore(mongoClient, config, pkg) |
| 91 | + } else { |
| 92 | + mongoDatastore = new MongoDatastore((PropertyResolver) config, pkg) |
| 93 | + } |
| 94 | + } |
| 95 | + else { |
| 96 | + MongoClient mongoClient = createMongoClient() |
| 97 | + if (mongoClient) { |
| 98 | + mongoDatastore = new MongoDatastore(mongoClient, config, domainClasses as Class[]) |
| 99 | + } else { |
| 100 | + mongoDatastore = new MongoDatastore((PropertyResolver) config, domainClasses as Class[]) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + void setup() { |
| 106 | + boolean existing = mongoDatastore.hasCurrentSession() |
| 107 | + mongoSession = existing ? mongoDatastore.currentSession : DatastoreUtils.bindSession(mongoDatastore.connect()) |
| 108 | + } |
| 109 | + |
| 110 | + void cleanup() { |
| 111 | + if (!mongoDatastore.hasCurrentSession()) { |
| 112 | + TransactionSynchronizationManager.unbindResource(mongoDatastore) |
| 113 | + DatastoreUtils.closeSessionOrRegisterDeferredClose(mongoSession, mongoDatastore) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return Obtain the mongo client |
| 119 | + */ |
| 120 | + MongoClient getMongoClient() { |
| 121 | + mongoDatastore.getConnectionSources().defaultConnectionSource.source |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Obtains the default package to scan |
| 126 | + * |
| 127 | + * @param config The configuration |
| 128 | + * @return The package to scan |
| 129 | + */ |
| 130 | + protected String getPackageToScan(Config config) { |
| 131 | + config.getProperty('grails.codegen.defaultPackage', getClass().package.name) |
| 132 | + } |
| 133 | + |
| 134 | + private List<PropertySource> load(ResourceLoader resourceLoader, PropertySourceLoader loader, String filename) { |
| 135 | + if (canLoadFileExtension(loader, filename)) { |
| 136 | + Resource appYml = resourceLoader.getResource(filename) |
| 137 | + return loader.load(appYml.getDescription(), appYml) as List<PropertySource> |
| 138 | + } else { |
| 139 | + return Collections.emptyList() |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private boolean canLoadFileExtension(PropertySourceLoader loader, String name) { |
| 144 | + return Arrays |
| 145 | + .stream(loader.fileExtensions) |
| 146 | + .map { String extension -> extension.toLowerCase() } |
| 147 | + .anyMatch { String extension -> name.toLowerCase().endsWith(extension) } |
| 148 | + } |
| 149 | +} |
0 commit comments