diff --git a/pom.xml b/pom.xml index 116e79c5..4d11271d 100644 --- a/pom.xml +++ b/pom.xml @@ -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. --> @@ -189,11 +189,6 @@ h2 - - com.google.code.gson - gson - - io.swagger swagger-parser @@ -228,9 +223,8 @@ - org.json - json - 20171018 + com.google.code.gson + gson diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/Endpoint.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/Endpoint.kt index f7b61e5e..29994708 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/Endpoint.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/Endpoint.kt @@ -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 diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/DbConnectionList.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/EndpointList.kt similarity index 84% rename from src/main/kotlin/com/github/mgramin/sqlboot/model/connection/DbConnectionList.kt rename to src/main/kotlin/com/github/mgramin/sqlboot/model/connection/EndpointList.kt index 85dab896..27927a88 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/DbConnectionList.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/EndpointList.kt @@ -36,10 +36,10 @@ import org.springframework.stereotype.Service @Service @Configuration @ConfigurationProperties(prefix = "conf") -open class DbConnectionList(val connections: List) { +open class EndpointList(val endpoints: List) { - 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()) } } diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/SimpleEndpoint.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/SimpleEndpoint.kt index 33045fcb..dd8d3c09 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/SimpleEndpoint.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/connection/SimpleEndpoint.kt @@ -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 (mgramin@gmail.com) @@ -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, @@ -52,7 +53,9 @@ open class SimpleEndpoint( private var dataSource: DataSource? = null - override fun properties() = JSONObject(properties).toMap() + override fun properties(): Map { + return Gson().fromJson(properties, object : TypeToken>() {}.type) + } @JsonIgnore @@ -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 diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/wrappers/body/BodyWrapper.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/wrappers/body/BodyWrapper.kt index b495eba6..761d9406 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/wrappers/body/BodyWrapper.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/wrappers/body/BodyWrapper.kt @@ -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 /** diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/ApiController.kt b/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/ApiController.kt index 4b979530..1202522b 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/ApiController.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/ApiController.kt @@ -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 @@ -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 @@ -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() @@ -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()) } @@ -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) } @@ -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) } @@ -134,7 +134,7 @@ class ApiController { getListResponseEntityHeaders(SqlPlaceholdersWrapper(DbUri("$connection/$type/$path"))) private fun getListResponseEntityHeaders(uri: Uri): ResponseEntity>> { - val connections = dbConnectionList.getConnectionsByMask(uri.connection()) + val connections = endpointList.getConnectionsByMask(uri.connection()) try { val headers = FsResourceType(connections, dbDialectList.dialects) .read(uri) diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/DbConnectionsController.kt b/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/DbConnectionsController.kt index 069befd0..604bee90 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/DbConnectionsController.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/DbConnectionsController.kt @@ -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 @@ -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 - @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 { - return dbConnectionList - .connections + return endpointList + .endpoints .toFlux() .parallel() .runOn(Schedulers.elastic()) diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/SwaggerController.kt b/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/SwaggerController.kt index a69ea782..1a8fafac 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/SwaggerController.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/SwaggerController.kt @@ -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 @@ -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]) @@ -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() @@ -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") diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4aabb83d..722b7891 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -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" diff --git a/src/test/kotlin/com/github/mgramin/sqlboot/model/connection/EndpointListTest.kt b/src/test/kotlin/com/github/mgramin/sqlboot/model/connection/EndpointListTest.kt index d2f8eb4c..b98b31fa 100644 --- a/src/test/kotlin/com/github/mgramin/sqlboot/model/connection/EndpointListTest.kt +++ b/src/test/kotlin/com/github/mgramin/sqlboot/model/connection/EndpointListTest.kt @@ -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()) } } \ No newline at end of file diff --git a/src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/FsResourceTypeTest.kt b/src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/FsResourceTypeTest.kt index b10783e4..cf81b45b 100644 --- a/src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/FsResourceTypeTest.kt +++ b/src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/FsResourceTypeTest.kt @@ -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 diff --git a/src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/SqlResourceTypeTest.kt b/src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/SqlResourceTypeTest.kt index 6a6f53ef..979420b1 100644 --- a/src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/SqlResourceTypeTest.kt +++ b/src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/SqlResourceTypeTest.kt @@ -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 diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index d0a426f6..171fd4fc 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -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", @@ -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", @@ -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",