Exercise repo, getAllByCustomerId, unit test (+code inspection)

This commit is contained in:
Bossanyi Tibor 2020-05-11 21:26:44 +02:00
parent 7c23ec7fa4
commit a6627a31e7
10 changed files with 42 additions and 87 deletions

View File

@ -49,7 +49,8 @@ test:
stage: test stage: test
image: openjdk:latest image: openjdk:latest
script: script:
- ./gradlew check -Pargs='spring.profiles.active=test' - export spring_profiles_active=test
- ./gradlew check
#deploy: #deploy:
# stage: deploy # stage: deploy

View File

@ -4,8 +4,7 @@ import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication @SpringBootApplication
class ApiApplication { class ApiApplication
}
fun main(args: Array<String>) { fun main(args: Array<String>) {
SpringApplication.run(ApiApplication::class.java, *args) SpringApplication.run(ApiApplication::class.java, *args)

View File

@ -2,10 +2,8 @@ package com.aitrainer.api.controller
import com.aitrainer.api.model.Customer import com.aitrainer.api.model.Customer
import com.aitrainer.api.repository.CustomerRepository import com.aitrainer.api.repository.CustomerRepository
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.*
import java.util.*
import javax.validation.Valid import javax.validation.Valid
@RestController @RestController

View File

@ -1,19 +1,19 @@
package com.aitrainer.api.controller package com.aitrainer.api.controller
import com.aitrainer.api.model.Customer
import com.aitrainer.api.model.Exercises import com.aitrainer.api.model.Exercises
import com.aitrainer.api.repository.ExercisesRepository import com.aitrainer.api.repository.ExercisesRepository
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.*
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import javax.validation.Valid import javax.validation.Valid
class ExerciesController(private val exercisesRepository: ExercisesRepository) { class ExerciesController(private val exercisesRepository: ExercisesRepository) {
@GetMapping("/exercises/customer/{id}")
fun getAllExersicesByCustomerId(customerId: Long): List<Exercises> =
exercisesRepository.getAllByCustomerId(customerId)
@PostMapping("/exercises") @PostMapping("/exercises")
fun createNewCustomer(@Valid @RequestBody exercises: Exercises): Exercises = fun createNewExercise(@Valid @RequestBody exercises: Exercises): Exercises =
exercisesRepository.save(exercises) exercisesRepository.save(exercises)
@PutMapping("/exercises/{id}") @PutMapping("/exercises/{id}")
@ -21,9 +21,9 @@ class ExerciesController(private val exercisesRepository: ExercisesRepository) {
@Valid @RequestBody newExercises: Exercises): ResponseEntity<Exercises> { @Valid @RequestBody newExercises: Exercises): ResponseEntity<Exercises> {
return exercisesRepository.findById(exerciseId).map { existingExercises -> return exercisesRepository.findById(exerciseId).map { existingExercises ->
val updatedExercises: Exercises = existingExercises.copy( val updatedExercises: Exercises = existingExercises.copy(
exercise_type_id = newExercises.exercise_type_id, exerciseTypeId = newExercises.exerciseTypeId,
customer_id = newExercises.customer_id, customerId = newExercises.customerId,
dateTimeExercise = newExercises.dateTimeExercise, datetimeExercise = newExercises.datetimeExercise,
quantity = newExercises.quantity, quantity = newExercises.quantity,
restTime = newExercises.restTime restTime = newExercises.restTime
) )

View File

@ -2,10 +2,8 @@ package com.aitrainer.api.controller
import com.aitrainer.api.model.ExerciseType import com.aitrainer.api.model.ExerciseType
import com.aitrainer.api.repository.ExerciseTypeRepository import com.aitrainer.api.repository.ExerciseTypeRepository
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.*
import java.util.*
import javax.validation.Valid import javax.validation.Valid
@RestController @RestController

View File

@ -6,15 +6,16 @@ import javax.persistence.Entity
import javax.persistence.GeneratedValue import javax.persistence.GeneratedValue
import javax.persistence.GenerationType import javax.persistence.GenerationType
import javax.persistence.Id import javax.persistence.Id
import javax.validation.constraints.Null
@Entity @Entity
data class Exercises ( data class Exercises (
@get: NonNull var exercise_type_id: Long = 0, @get: NonNull var exerciseTypeId: Long = 0,
@get: NonNull var customer_id: Long = 0, @get: NonNull var customerId: Long = 0,
@get: NonNull var dateTimeExercise: Date? = null, @get: NonNull var datetimeExercise: Date? = null,
@get: NonNull var quantity: Int = 0, @get: NonNull var quantity: Int = 0,
var restTime: Int = 0, // in seconds @get: Null var restTime: Integer, // in seconds
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val exercise_id: Long = 0 val exerciseId: Long = 0
) )

View File

@ -5,4 +5,6 @@ import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
interface ExercisesRepository : JpaRepository<Exercises, Long> interface ExercisesRepository : JpaRepository<Exercises, Long> {
fun getAllByCustomerId( customerId: Long ):List<Exercises>
}

View File

@ -3,8 +3,6 @@ package com.aitrainer.api.test
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import java.sql.* import java.sql.*
import kotlin.test.assertFails
import java.util.* import java.util.*
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -14,9 +12,9 @@ class AitrainerDBTest(configuration: ApplicationConfiguration) {
@Autowired @Autowired
private lateinit var username: String; private lateinit var username: String
private lateinit var password: String; private lateinit var password: String
private lateinit var datasourceUrl: String; private lateinit var datasourceUrl: String
private lateinit var conn: Connection private lateinit var conn: Connection
private val conf: ApplicationConfiguration = configuration private val conf: ApplicationConfiguration = configuration
@ -34,38 +32,7 @@ class AitrainerDBTest(configuration: ApplicationConfiguration) {
} }
private fun executeDBQueries() {
var stmt: Statement? = null
var resultset: ResultSet? = null
try {
stmt = conn.createStatement()
val sql: String = "CREATE DATABASE aitrainer_test;"
if ( stmt.execute( sql ) ) {
}
//while (resultset!!.next()) {
// println(resultset.getString("Database"))
//}
} catch (ex: SQLException) {
throw Exception(ex)
} finally {
// release resources
if (resultset != null) {
try {
resultset.close()
} catch (sqlEx: SQLException) {
}
}
if (stmt != null) {
try {
stmt.close()
} catch (sqlEx: SQLException) {
throw Exception(sqlEx)
}
}
conn.close()
}
}
/** /**
* This method makes a connection to MySQL Server * This method makes a connection to MySQL Server
* In this example, MySQL Server is running in the local host (so 127.0.0.1) * In this example, MySQL Server is running in the local host (so 127.0.0.1)
@ -75,16 +42,8 @@ class AitrainerDBTest(configuration: ApplicationConfiguration) {
val connectionProps = Properties() val connectionProps = Properties()
connectionProps["user"] = this.username connectionProps["user"] = this.username
connectionProps["password"] = this.password connectionProps["password"] = this.password
//try {
Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance() Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance()
this.conn = DriverManager.getConnection(this.datasourceUrl, connectionProps) this.conn = DriverManager.getConnection(this.datasourceUrl, connectionProps)
//} catch (ex: SQLException) {
// handle any errors
// ex.printStackTrace()
//} catch (ex: Exception) {
// handle any errors
// ex.printStackTrace()
//}
} }
} }

View File

@ -6,7 +6,7 @@ import org.springframework.stereotype.Component
@Component @Component
@ConfigurationProperties(prefix = "spring.datasource") @ConfigurationProperties(prefix = "spring.datasource")
class ApplicationConfiguration { class ApplicationConfiguration {
open var username: String? = null var username: String? = null
open var password: String? = null var password: String? = null
open var url: String? = null var url: String? = null
} }

View File

@ -5,54 +5,51 @@ import com.aitrainer.api.repository.CustomerRepository
import com.aitrainer.api.model.Customer import com.aitrainer.api.model.Customer
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
import kotlin.test.assertNull import org.springframework.beans.factory.annotation.Autowired
import kotlin.test.fail
import org.springframework.beans.factory.annotation.Autowired;
@SpringBootTest @SpringBootTest
class CustomerTests { class CustomerTests {
@Autowired @Autowired
lateinit private var customerRepository: CustomerRepository private lateinit var customerRepository: CustomerRepository
private val customerId: Long = 4 private val customerId: Long = 4
private var inserted_id: Long? = null private var insertedId: Long? = null
@Test @Test
fun testGet() { fun testGet() {
val id: Long = 7; val id: Long = 7
val customer: Customer = customerRepository.findById( id ).orElse(null); val customer: Customer = customerRepository.findById( id ).orElse(null)
assertEquals( customer.name, "Átlag 18 éves fiú") assertEquals( customer.name, "Átlag 18 éves fiú")
} }
@Test @Test
fun testInsert() { fun testInsert() {
val newCustomer = Customer("Bossanyi", "Tibor", "", 48, "m"); val newCustomer = Customer("Bossanyi", "Tibor", "", 48, "m")
val savedCustomer: Customer = customerRepository.save(newCustomer) val savedCustomer: Customer = customerRepository.save(newCustomer)
assertEquals(savedCustomer.age, 48) assertEquals(savedCustomer.age, 48)
this.inserted_id = savedCustomer.customer_id; this.insertedId = savedCustomer.customer_id
val customer: Customer = customerRepository.findById( savedCustomer.customer_id ).orElse(null); val customer: Customer = customerRepository.findById( savedCustomer.customer_id ).orElse(null)
assertEquals( customer.firstname, "Tibor") assertEquals( customer.firstname, "Tibor")
this.testUpdate(savedCustomer.customer_id); this.testUpdate(savedCustomer.customer_id)
} }
fun testUpdate( customerId: Long ) { fun testUpdate( customerId: Long ) {
var id: Long? = customerId; val id: Long? = customerId
assertNotNull(id) assertNotNull(id)
var updatedCustomer: Customer = customerRepository.findById( id ).orElse(null); val updatedCustomer: Customer = customerRepository.findById( id ).orElse(null)
assertNotNull(updatedCustomer) assertNotNull(updatedCustomer)
updatedCustomer.firstname ="Tiborka" updatedCustomer.firstname ="Tiborka"
val customer: Customer = customerRepository.save( updatedCustomer ); val customer: Customer = customerRepository.save( updatedCustomer )
assertEquals( customer.firstname, "Tiborka") assertEquals( customer.firstname, "Tiborka")
customerRepository.delete(updatedCustomer); customerRepository.delete(updatedCustomer)
} }
} }