Skip to content

Commit

Permalink
#322 rename connectionList
Browse files Browse the repository at this point in the history
  • Loading branch information
mgramin committed May 18, 2019
1 parent a2b796b commit c1a3350
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 51 deletions.
12 changes: 3 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
-->
Expand Down Expand Up @@ -189,11 +189,6 @@
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser</artifactId>
Expand Down Expand Up @@ -228,9 +223,8 @@
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ interface Endpoint {

fun name(): String

@Deprecated("Move to properties")
fun dialect(): String

@Deprecated("Move to SQLResourceType")
fun getDataSource(): DataSource

fun properties(): Map<String, Any>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ import org.springframework.stereotype.Service
@Service
@Configuration
@ConfigurationProperties(prefix = "conf")
open class DbConnectionList(val connections: List<SimpleEndpoint>) {
open class EndpointList(val endpoints: List<SimpleEndpoint>) {

fun getConnectionByName(name: String) = connections.first { v -> v.name().equals(name, ignoreCase = true) }
fun getConnectionByName(name: String) = endpoints.first { v -> v.name().equals(name, ignoreCase = true) }

fun getConnectionsByMask(name: String) = connections.filter { v -> v.name().matches(name.toRegex()) }
fun getConnectionsByMask(name: String) = endpoints.filter { v -> v.name().matches(name.toRegex()) }

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ package com.github.mgramin.sqlboot.model.connection

import com.fasterxml.jackson.annotation.JsonIgnore
import org.apache.tomcat.jdbc.pool.DataSource
import org.json.JSONObject
import org.springframework.core.io.Resource
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken


/**
* @author Maksim Gramin ([email protected])
Expand All @@ -37,7 +39,6 @@ import org.springframework.core.io.Resource
open class SimpleEndpoint(
var name: String? = null,
@JsonIgnore var baseFolder: Resource? = null,
var url: String? = null,
var user: String? = null,
@JsonIgnore var password: String? = null,
var driverClassName: String? = null,
Expand All @@ -52,7 +53,9 @@ open class SimpleEndpoint(

private var dataSource: DataSource? = null

override fun properties() = JSONObject(properties).toMap()
override fun properties(): Map<String, Any> {
return Gson().fromJson(properties, object : TypeToken<Map<String, Any>>() {}.type)
}


@JsonIgnore
Expand All @@ -64,8 +67,8 @@ open class SimpleEndpoint(
if (driverClassName != null) {
dataSourceNew.driverClassName = driverClassName
}
if (url != null) {
dataSourceNew.url = url
if (properties()["url"] != null) {
dataSourceNew.url = properties()["url"].toString()
}
if (user != null) {
dataSourceNew.username = user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import com.github.mgramin.sqlboot.model.resourcetype.Metadata
import com.github.mgramin.sqlboot.model.resourcetype.ResourceType
import com.github.mgramin.sqlboot.model.uri.Uri
import com.github.mgramin.sqlboot.template.generator.TemplateGenerator
import org.json.JSONObject
import reactor.core.publisher.Flux

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.github.mgramin.sqlboot.rest.controllers

import com.github.mgramin.sqlboot.exceptions.BootException
import com.github.mgramin.sqlboot.model.connection.DbConnectionList
import com.github.mgramin.sqlboot.model.connection.EndpointList
import com.github.mgramin.sqlboot.model.dialect.DbDialectList
import com.github.mgramin.sqlboot.model.resourcetype.impl.FsResourceType
import com.github.mgramin.sqlboot.model.uri.Uri
Expand Down Expand Up @@ -57,7 +57,7 @@ import javax.servlet.http.HttpServletRequest
class ApiController {

@Autowired
private lateinit var dbConnectionList: DbConnectionList
private lateinit var endpointList: EndpointList

@Autowired
private lateinit var dbDialectList: DbDialectList
Expand All @@ -66,7 +66,7 @@ class ApiController {
@RequestMapping(value = ["/api/{connectionName}/types"])
fun types(@PathVariable connectionName: String): String {
val jsonArray = JsonArray()
FsResourceType(dbConnectionList.getConnectionsByMask(connectionName), emptyList())
FsResourceType(endpointList.getConnectionsByMask(connectionName), emptyList())
.resourceTypes()
.forEach { jsonArray.add(it.toJson()) }
return jsonArray.toString()
Expand All @@ -75,7 +75,7 @@ class ApiController {
@RequestMapping(value = ["/api/{connectionName}/types/{typeMask}"])
fun typesByMask(@PathVariable connectionName: String, @PathVariable typeMask: String): String {
val jsonArray = JsonArray()
FsResourceType(dbConnectionList.getConnectionsByMask(connectionName), emptyList())
FsResourceType(endpointList.getConnectionsByMask(connectionName), emptyList())
.resourceTypes()
.filter { it.name().matches(wildcardToRegex(typeMask)) }
.forEach { jsonArray.add(it.toJson()) }
Expand All @@ -91,7 +91,7 @@ class ApiController {
val jsonArray = JsonArray()
val uri = SqlPlaceholdersWrapper(
DbUri("$connection/$type"))
FsResourceType(listOf(dbConnectionList.getConnectionByName(uri.connection())), emptyList())
FsResourceType(listOf(endpointList.getConnectionByName(uri.connection())), emptyList())
.resourceTypes()
.asSequence()
.filter { v -> v.name().equals(uri.type(), ignoreCase = true) }
Expand All @@ -109,7 +109,7 @@ class ApiController {
val jsonArray = JsonArray()
val uri = SqlPlaceholdersWrapper(
DbUri("$connection/$type/$path"))
FsResourceType(listOf(dbConnectionList.getConnectionByName(uri.connection())), emptyList())
FsResourceType(listOf(endpointList.getConnectionByName(uri.connection())), emptyList())
.resourceTypes()
.asSequence()
.filter { v -> v.name().equals(uri.type(), ignoreCase = true) }
Expand All @@ -134,7 +134,7 @@ class ApiController {
getListResponseEntityHeaders(SqlPlaceholdersWrapper(DbUri("$connection/$type/$path")))

private fun getListResponseEntityHeaders(uri: Uri): ResponseEntity<List<Map<String, Any>>> {
val connections = dbConnectionList.getConnectionsByMask(uri.connection())
val connections = endpointList.getConnectionsByMask(uri.connection())
try {
val headers = FsResourceType(connections, dbDialectList.dialects)
.read(uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.github.mgramin.sqlboot.rest.controllers

import com.github.mgramin.sqlboot.model.connection.Endpoint
import com.github.mgramin.sqlboot.model.connection.DbConnectionList
import com.github.mgramin.sqlboot.model.connection.EndpointList
import com.github.mgramin.sqlboot.model.connection.SimpleEndpoint
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
Expand All @@ -49,17 +49,17 @@ import reactor.core.scheduler.Schedulers
@ComponentScan(basePackages = ["com.github.mgramin.sqlboot.model.resource_type"])
@EnableAutoConfiguration
@CrossOrigin
class DbConnectionsController @Autowired constructor(private val dbConnectionList: DbConnectionList) {
class DbConnectionsController @Autowired constructor(private val endpointList: EndpointList) {

val allDbConnections: List<SimpleEndpoint>
@RequestMapping(value = ["/connections"])
get() = dbConnectionList.connections
@RequestMapping(value = ["/endpoints"])
get() = endpointList.endpoints

@GetMapping(value = ["/connections/health"], produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
@GetMapping(value = ["/endpoints/health"], produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
@ResponseBody
internal fun health(): Flux<Endpoint> {
return dbConnectionList
.connections
return endpointList
.endpoints
.toFlux()
.parallel()
.runOn(Schedulers.elastic())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.github.mgramin.sqlboot.rest.controllers

import com.fasterxml.jackson.core.JsonProcessingException
import com.github.mgramin.sqlboot.model.connection.DbConnectionList
import com.github.mgramin.sqlboot.model.connection.EndpointList
import com.github.mgramin.sqlboot.model.resourcetype.impl.FsResourceType
import io.swagger.models.Info
import io.swagger.models.ModelImpl
Expand Down Expand Up @@ -56,7 +56,7 @@ import javax.servlet.http.HttpServletRequest
class SwaggerController {

@Autowired
private lateinit var dbConnectionList: DbConnectionList
private lateinit var endpointList: EndpointList


@RequestMapping(method = [RequestMethod.GET, RequestMethod.POST], path = ["/api"], produces = [MediaType.APPLICATION_JSON_VALUE])
Expand Down Expand Up @@ -92,7 +92,7 @@ class SwaggerController {

private fun getSwaggerDescription(request: HttpServletRequest, connectionName: String): Swagger {
val fsResourceTypes = FsResourceType(
listOf(dbConnectionList.getConnectionByName(connectionName)), emptyList())
listOf(endpointList.getConnectionByName(connectionName)), emptyList())
val resourceTypes = fsResourceTypes.resourceTypes()
val swagger = Swagger()

Expand All @@ -102,9 +102,9 @@ class SwaggerController {
swagger.info = Info().version("v1").title("API specification")
swagger.schemes = Arrays.asList(Scheme.HTTP, Scheme.HTTPS)

swagger.path("/connections",
swagger.path("/endpoints",
Path().get(Operation()
.tag("connections")
.tag("endpoints")
.response(200,
Response()
.description("Ok")
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ conf:
driverClassName: org.h2.Driver
properties: >
{
"url": "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';"
"color": "red",
"description": "Production demo db",
"css_class": "fas fa-fw fa-2x fa-bicycle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@ import org.springframework.test.context.junit.jupiter.SpringExtension

@ExtendWith(SpringExtension::class)
@EnableAutoConfiguration
@ContextConfiguration(classes = [DbConnectionList::class], initializers = [ConfigFileApplicationContextInitializer::class])
@ContextConfiguration(classes = [EndpointList::class], initializers = [ConfigFileApplicationContextInitializer::class])
internal class EndpointListTest {

@Autowired
lateinit var dbConnectionList: DbConnectionList
lateinit var endpointList: EndpointList

@ParameterizedTest
@ValueSource(strings = ["test", "dev", "prod"])
fun getConnectionByName(connectionName: String) {
assertEquals(connectionName, dbConnectionList.getConnectionByName(connectionName).name())
assertEquals(connectionName, endpointList.getConnectionByName(connectionName).name())
}

@ParameterizedTest
@ValueSource(strings = ["test", "dev", "prod"])
fun getConnectionsByMask(connectionMask: String) {
assertEquals(connectionMask, dbConnectionList.getConnectionsByMask(connectionMask).first().name())
assertEquals(connectionMask, endpointList.getConnectionsByMask(connectionMask).first().name())
}

@Test
fun getConnections() {
assertEquals(4, dbConnectionList.connections.count())
assertEquals(4, endpointList.endpoints.count())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,14 @@ class FsResourceTypeTest {
dbMd.name = "unit_test_db_md"
dbMd.dialect = "h2"
dbMd.baseFolder = FileSystemResource("conf/h2/md/database")
dbMd.url = "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';"
dbMd.paginationQueryTemplate = "${'$'}{query} offset ${'$'}{uri.pageSize()*(uri.pageNumber()-1)} limit ${'$'}{uri.pageSize()}"
dbMd.properties = """{ "url": "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';" }"""

dbSql.name = "unit_test_db_sql"
dbSql.dialect = "h2"
dbSql.baseFolder = FileSystemResource("conf/h2/sql/database")
dbSql.url = "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';"
dbSql.paginationQueryTemplate = "${'$'}{query} offset ${'$'}{uri.pageSize()*(uri.pageNumber()-1)} limit ${'$'}{uri.pageSize()}"
dbSql.paginationQueryTemplate = "${'$'}{query} offset ${'$'}{uri.pageSize()*(uri.pageNumber()-1)} limit ${'$'}{uri.pageSize()}"
dbSql.properties = """{ "url": "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';" }"""
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class SqlResourceTypeTest {
db.name = "unit_test_db"
db.dialect = "h2"
db.baseFolder = FileSystemResource("conf/h2/database")
db.url = "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';"
db.paginationQueryTemplate = "${'$'}{query} offset ${'$'}{uri.pageSize()*(uri.pageNumber()-1)} limit ${'$'}{uri.pageSize()}"
db.properties = """{ "url": "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';" }"""
}

@Test
Expand Down
10 changes: 5 additions & 5 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ conf:
paginationQueryTemplate: >
${query} offset ${uri.pageSize()*(uri.pageNumber()-1)} limit ${uri.pageSize()}
connections:
endpoints:
- name: h2
dialect: h2
baseFolder: file:conf/h2/md/database
url: jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';
driverClassName: org.h2.Driver
properties: >
{
"url": "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';",
"visible": false,
"description": "Embedded db for unit tests only"
}
- name: dev
dialect: h2
baseFolder: file:conf/h2/md/database
url: jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';
driverClassName: org.h2.Driver
properties: >
{
"url": "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';",
"visible": false,
"color": "green",
"description": "Develop demo db",
Expand All @@ -31,10 +31,10 @@ conf:
- name: test
dialect: h2
baseFolder: file:conf/h2/md/database
url: jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';
driverClassName: org.h2.Driver
properties: >
{
"url": "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';",
"visible": false,
"color": "orange",
"description": "Testing demo db",
Expand All @@ -43,10 +43,10 @@ conf:
- name: prod
dialect: h2
baseFolder: file:conf/h2/md/database
url: jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';
driverClassName: org.h2.Driver
properties: >
{
"url": "jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';",
"visible": false,
"color": "red",
"description": "Production demo db",
Expand Down

0 comments on commit c1a3350

Please sign in to comment.