Exercise repo, getAllByCustomerId, unit test (+code inspection)
This commit is contained in:
parent
7c23ec7fa4
commit
a6627a31e7
@ -49,7 +49,8 @@ test:
|
||||
stage: test
|
||||
image: openjdk:latest
|
||||
script:
|
||||
- ./gradlew check -Pargs='spring.profiles.active=test'
|
||||
- export spring_profiles_active=test
|
||||
- ./gradlew check
|
||||
|
||||
#deploy:
|
||||
# stage: deploy
|
||||
|
@ -4,8 +4,7 @@ import org.springframework.boot.SpringApplication
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
|
||||
@SpringBootApplication
|
||||
class ApiApplication {
|
||||
}
|
||||
class ApiApplication
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
SpringApplication.run(ApiApplication::class.java, *args)
|
||||
|
@ -2,10 +2,8 @@ package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.model.Customer
|
||||
import com.aitrainer.api.repository.CustomerRepository
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import java.util.*
|
||||
import javax.validation.Valid
|
||||
|
||||
@RestController
|
||||
|
@ -1,19 +1,19 @@
|
||||
package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.model.Customer
|
||||
import com.aitrainer.api.model.Exercises
|
||||
import com.aitrainer.api.repository.ExercisesRepository
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.PutMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import javax.validation.Valid
|
||||
|
||||
class ExerciesController(private val exercisesRepository: ExercisesRepository) {
|
||||
|
||||
@GetMapping("/exercises/customer/{id}")
|
||||
fun getAllExersicesByCustomerId(customerId: Long): List<Exercises> =
|
||||
exercisesRepository.getAllByCustomerId(customerId)
|
||||
|
||||
@PostMapping("/exercises")
|
||||
fun createNewCustomer(@Valid @RequestBody exercises: Exercises): Exercises =
|
||||
fun createNewExercise(@Valid @RequestBody exercises: Exercises): Exercises =
|
||||
exercisesRepository.save(exercises)
|
||||
|
||||
@PutMapping("/exercises/{id}")
|
||||
@ -21,9 +21,9 @@ class ExerciesController(private val exercisesRepository: ExercisesRepository) {
|
||||
@Valid @RequestBody newExercises: Exercises): ResponseEntity<Exercises> {
|
||||
return exercisesRepository.findById(exerciseId).map { existingExercises ->
|
||||
val updatedExercises: Exercises = existingExercises.copy(
|
||||
exercise_type_id = newExercises.exercise_type_id,
|
||||
customer_id = newExercises.customer_id,
|
||||
dateTimeExercise = newExercises.dateTimeExercise,
|
||||
exerciseTypeId = newExercises.exerciseTypeId,
|
||||
customerId = newExercises.customerId,
|
||||
datetimeExercise = newExercises.datetimeExercise,
|
||||
quantity = newExercises.quantity,
|
||||
restTime = newExercises.restTime
|
||||
)
|
||||
|
@ -2,10 +2,8 @@ package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.model.ExerciseType
|
||||
import com.aitrainer.api.repository.ExerciseTypeRepository
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import java.util.*
|
||||
import javax.validation.Valid
|
||||
|
||||
@RestController
|
||||
|
@ -6,15 +6,16 @@ import javax.persistence.Entity
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.GenerationType
|
||||
import javax.persistence.Id
|
||||
import javax.validation.constraints.Null
|
||||
|
||||
@Entity
|
||||
data class Exercises (
|
||||
@get: NonNull var exercise_type_id: Long = 0,
|
||||
@get: NonNull var customer_id: Long = 0,
|
||||
@get: NonNull var dateTimeExercise: Date? = null,
|
||||
@get: NonNull var quantity: Int = 0,
|
||||
var restTime: Int = 0, // in seconds
|
||||
@get: NonNull var exerciseTypeId: Long = 0,
|
||||
@get: NonNull var customerId: Long = 0,
|
||||
@get: NonNull var datetimeExercise: Date? = null,
|
||||
@get: NonNull var quantity: Int = 0,
|
||||
@get: Null var restTime: Integer, // in seconds
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
val exercise_id: Long = 0
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
val exerciseId: Long = 0
|
||||
)
|
@ -5,4 +5,6 @@ import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface ExercisesRepository : JpaRepository<Exercises, Long>
|
||||
interface ExercisesRepository : JpaRepository<Exercises, Long> {
|
||||
fun getAllByCustomerId( customerId: Long ):List<Exercises>
|
||||
}
|
@ -3,8 +3,6 @@ package com.aitrainer.api.test
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
|
||||
import java.sql.*
|
||||
import kotlin.test.assertFails
|
||||
|
||||
import java.util.*
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
@ -14,9 +12,9 @@ class AitrainerDBTest(configuration: ApplicationConfiguration) {
|
||||
|
||||
|
||||
@Autowired
|
||||
private lateinit var username: String;
|
||||
private lateinit var password: String;
|
||||
private lateinit var datasourceUrl: String;
|
||||
private lateinit var username: String
|
||||
private lateinit var password: String
|
||||
private lateinit var datasourceUrl: String
|
||||
private lateinit var conn: Connection
|
||||
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
|
||||
* 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()
|
||||
connectionProps["user"] = this.username
|
||||
connectionProps["password"] = this.password
|
||||
//try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance()
|
||||
this.conn = DriverManager.getConnection(this.datasourceUrl, connectionProps)
|
||||
//} catch (ex: SQLException) {
|
||||
// handle any errors
|
||||
// ex.printStackTrace()
|
||||
//} catch (ex: Exception) {
|
||||
// handle any errors
|
||||
// ex.printStackTrace()
|
||||
//}
|
||||
}
|
||||
|
||||
}
|
@ -6,7 +6,7 @@ import org.springframework.stereotype.Component
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "spring.datasource")
|
||||
class ApplicationConfiguration {
|
||||
open var username: String? = null
|
||||
open var password: String? = null
|
||||
open var url: String? = null
|
||||
var username: String? = null
|
||||
var password: String? = null
|
||||
var url: String? = null
|
||||
}
|
@ -5,54 +5,51 @@ import com.aitrainer.api.repository.CustomerRepository
|
||||
import com.aitrainer.api.model.Customer
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.fail
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
|
||||
@SpringBootTest
|
||||
class CustomerTests {
|
||||
|
||||
@Autowired
|
||||
lateinit private var customerRepository: CustomerRepository
|
||||
private lateinit var customerRepository: CustomerRepository
|
||||
private val customerId: Long = 4
|
||||
private var inserted_id: Long? = null
|
||||
private var insertedId: Long? = null
|
||||
|
||||
@Test
|
||||
fun testGet() {
|
||||
val id: Long = 7;
|
||||
val customer: Customer = customerRepository.findById( id ).orElse(null);
|
||||
val id: Long = 7
|
||||
val customer: Customer = customerRepository.findById( id ).orElse(null)
|
||||
assertEquals( customer.name, "Átlag 18 éves fiú")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsert() {
|
||||
val newCustomer = Customer("Bossanyi", "Tibor", "", 48, "m");
|
||||
val newCustomer = Customer("Bossanyi", "Tibor", "", 48, "m")
|
||||
val savedCustomer: Customer = customerRepository.save(newCustomer)
|
||||
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")
|
||||
|
||||
this.testUpdate(savedCustomer.customer_id);
|
||||
this.testUpdate(savedCustomer.customer_id)
|
||||
}
|
||||
|
||||
fun testUpdate( customerId: Long ) {
|
||||
var id: Long? = customerId;
|
||||
val id: Long? = customerId
|
||||
assertNotNull(id)
|
||||
|
||||
var updatedCustomer: Customer = customerRepository.findById( id ).orElse(null);
|
||||
val updatedCustomer: Customer = customerRepository.findById( id ).orElse(null)
|
||||
|
||||
assertNotNull(updatedCustomer)
|
||||
updatedCustomer.firstname ="Tiborka"
|
||||
|
||||
val customer: Customer = customerRepository.save( updatedCustomer );
|
||||
val customer: Customer = customerRepository.save( updatedCustomer )
|
||||
assertEquals( customer.firstname, "Tiborka")
|
||||
|
||||
customerRepository.delete(updatedCustomer);
|
||||
customerRepository.delete(updatedCustomer)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user