Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start exam implementation: with the notate method, driven by test #5

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ version = '0.0.1-SNAPSHOT'
subprojects {
apply plugin: 'kotlin'
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: "org.jetbrains.kotlin.plugin.allopen"

junitPlatform {
filters {
Expand All @@ -35,9 +36,18 @@ subprojects {
dependencies {
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compile("org.jetbrains.kotlin:kotlin-reflect")
testCompile('org.jetbrains.spek:spek-api:1.1.5') {
exclude group: 'org.jetbrains.kotlin'
}
compile 'io.arrow-kt:arrow-core:0.7.2'
compile 'io.arrow-kt:arrow-syntax:0.7.2'
compile 'io.arrow-kt:arrow-typeclasses:0.7.2'
compile 'io.arrow-kt:arrow-data:0.7.2'
compile 'io.arrow-kt:arrow-instances-core:0.7.2'
compile 'io.arrow-kt:arrow-instances-data:0.7.2'
kapt 'io.arrow-kt:arrow-annotations-processor:0.7.2'

testCompile('org.jetbrains.spek:spek-api:1.1.5')
{
exclude group: 'org.jetbrains.kotlin'
}
testRuntime('org.jetbrains.spek:spek-junit-platform-engine:1.1.5') {
exclude group: 'org.junit.platform'
exclude group: 'org.jetbrains.kotlin'
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip
File renamed without changes.
14 changes: 14 additions & 0 deletions interactors/src/main/kotlin/entities/Exam.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.students.results.entities

import arrow.core.Either
import services.Grade

data class Exam(val id: Long) {
fun validateGrade(notation: Grade): Either<InvalidNotationForThisExamException, Unit> =
when (notation) {
in (0.toBigDecimal()..20.toBigDecimal()) -> Either.right(Unit)
else -> Either.left(InvalidNotationForThisExamException("This notation, $notation, is not in bound"))
}
}

class InvalidNotationForThisExamException(msg: String?) : Exception(msg)
22 changes: 22 additions & 0 deletions interactors/src/main/kotlin/entities/Student.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.students.results.entities

import arrow.core.Either
import services.Grade
import java.math.BigDecimal

typealias Grades = Map<Exam, Grade>

data class Student(val id: Long, val grades: Grades = emptyMap()) {
fun getNotation(exam: Exam): Either<NotEvaluatedException, BigDecimal> =
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops ! get grade !

when {
grades.containsKey(exam) -> Either.right(grades.getValue(exam))
else -> Either.left(NotEvaluatedException())
}

fun notate(exam: Exam, note: BigDecimal): Either<InvalidNotationForThisExamException, Student> =
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops should be grade not notate

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a Grade not a BigDecimal

exam.validateGrade(note).map {
copy(grades = grades + mapOf(exam to note))
}
}

class NotEvaluatedException(msg: String? = null, throwable: Throwable? = null) : Exception(msg, throwable)
6 changes: 6 additions & 0 deletions interactors/src/main/kotlin/interactors/ExamsInteractor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.students.results.interactors

import com.students.results.repository.ExamsRepository
import com.students.results.services.Exams

class ExamsInteractor(private val examsRepository: ExamsRepository) : Exams
21 changes: 21 additions & 0 deletions interactors/src/main/kotlin/interactors/StudentsInteractor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.students.results.interactors

import arrow.core.Either
import arrow.core.flatMap
import com.students.results.repository.ExamsRepository
import com.students.results.repository.StudentsRepository
import com.students.results.services.GradeExamException
import com.students.results.services.Students
import com.students.results.services.requests.GradeExam

class StudentsInteractor(private val studentsRepository: StudentsRepository, private val examsRepository: ExamsRepository) : Students {

override fun grade(gradeExam: GradeExam): Either<GradeExamException, Unit> =
gradeExam.run {
studentsRepository.findStudentById(studentId).flatMap { student ->
examsRepository.findExamById(examId).flatMap { exam ->
student.notate(exam, grade).flatMap { studentsRepository.save(it) }
}
}.mapLeft { GradeExamException(throwable = it) }
}
}
9 changes: 9 additions & 0 deletions interactors/src/main/kotlin/repository/ExamsRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.students.results.repository

import arrow.core.Either
import com.students.results.entities.Exam

interface ExamsRepository {

fun findExamById(examId: Long): Either<NotFoundException, Exam>
}
8 changes: 8 additions & 0 deletions interactors/src/main/kotlin/repository/Repositories.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.students.results.repository


sealed class RepositoryException(msg: String? = null, throwable: Throwable? = null) : Exception(msg, throwable)

class NotFoundException(msg: String? = null, throwable: Throwable? = null) : RepositoryException(msg, throwable)
class NotWrittenException(msg: String? = null, throwable: Throwable? = null) : RepositoryException(msg, throwable)
class RepositoryNotAvailableException(msg: String? = null, throwable: Throwable? = null) : RepositoryException(msg, throwable)
10 changes: 10 additions & 0 deletions interactors/src/main/kotlin/repository/StudentsRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.students.results.repository

import arrow.core.Either
import com.students.results.entities.Student

interface StudentsRepository {

fun findStudentById(studentId: Long): Either<RepositoryException, Student>
fun save(student: Student): Either<RepositoryException, Unit>
}
6 changes: 6 additions & 0 deletions interactors/src/main/kotlin/services/Exams.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.students.results.services

interface Exams {
}

class GradeExamException(message: String? = null, throwable: Throwable? = null) : Exception(message, throwable)
8 changes: 8 additions & 0 deletions interactors/src/main/kotlin/services/Services.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package services

import java.math.BigDecimal

typealias Grade = BigDecimal
sealed class ServicesException(msg: String? = null, throwable: Throwable? = null) : Exception(msg, throwable)

class NotFoundException(msg: String? = null, throwable: Throwable? = null) : ServicesException(msg, throwable)
10 changes: 10 additions & 0 deletions interactors/src/main/kotlin/services/Students.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.students.results.services

import arrow.core.Either
import com.students.results.services.requests.GradeExam

interface Students {

fun grade(gradeExam: GradeExam): Either<GradeExamException, Unit>

}
5 changes: 5 additions & 0 deletions interactors/src/main/kotlin/services/requests/GradeExam.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.students.results.services.requests

import services.Grade

data class GradeExam(val examId: Long, val studentId: Long, val grade: Grade)
35 changes: 35 additions & 0 deletions interactors/src/test/kotlin/entities/StudentTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package entities

import com.students.results.entities.Exam
import com.students.results.entities.Student
import org.amshove.kluent.shouldBe
import org.amshove.kluent.shouldEqual
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it

class StudentTest : Spek({

given("a student") {
val student = Student(id = 40L)
val exam = Exam(id = 50L)

it("getNot without grade should returns NotEvaluatedException") {
student.getNotation(exam).isLeft() shouldBe true
}
it("grade an exam with 20 and get notation should return 20") {
student.notate(exam, "20".toBigDecimal()).apply {
isRight() shouldBe true
map {
it.getNotation(exam).apply {
isRight() shouldBe true
map { it shouldEqual "20".toBigDecimal() }
}
}
}
}
it("grade less than zero should fail to register grade") {
student.notate(exam, "-1".toBigDecimal()).isLeft() shouldBe true
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.students.results.interactors

import org.jetbrains.spek.api.Spek

class ExamsInteractorTest : Spek({

})
56 changes: 56 additions & 0 deletions interactors/src/test/kotlin/interactors/StudentsInteractorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.students.results.interactors

import arrow.core.Either
import com.students.results.entities.Exam
import com.students.results.entities.Student
import com.students.results.repository.ExamsRepository
import com.students.results.repository.StudentsRepository
import com.students.results.services.requests.GradeExam
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import org.amshove.kluent.shouldBe
import org.amshove.kluent.shouldEqual
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it


class StudentsInteractorTest : Spek({

val student = Student(id = 10L)
given("a student interactor") {

val studentSlot = slot<Student>()
val studentRepository = mockk<StudentsRepository>() {
every { findStudentById(3) } returns Either.right(student)
every { save(student = capture(studentSlot)) } returns Either.right(Unit)
}
val exam = Exam(id = 5)
val examsRepository = mockk<ExamsRepository>().apply {
every { findExamById(5) } returns Either.right(exam)
}
val studentsInteractor = StudentsInteractor(studentRepository, examsRepository)
studentsInteractor.notate20().apply {
it("should retrieve student by his id") {
verify { studentRepository.findStudentById(3) }
}
it("should retrieve exam by id") {
verify { examsRepository.findExamById(5) }
}
it("should save student to the repository with the expected notation of 20 for this exam") {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grade not notation

studentSlot.captured.getNotation(exam).apply {
isRight() shouldBe true
map { it shouldEqual "20".toBigDecimal() }
}
}
}
}
})

private fun StudentsInteractor.notate20() = grade(GradeExam(
examId = 5,
studentId = 3,
grade = "20".toBigDecimal()
))
18 changes: 16 additions & 2 deletions persistence/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@

apply plugin: 'kotlin-spring'

dependencies {
compile project(':business-rules')
compile project(':interactors')
compile group: 'org.springframework.data', name: 'spring-data-redis', version: '2.0.7.RELEASE'
compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.5.0'
compile group: 'redis.clients', name: 'jedis', version: '2.9.0'

testCompile 'it.ozimov:embedded-redis:0.7.2'
testCompile group: 'org.springframework', name: 'spring-test', version: '5.0.6.RELEASE'
testImplementation(
'org.junit.jupiter:junit-jupiter-api:5.1.0'
)
testRuntimeOnly(
'org.junit.jupiter:junit-jupiter-engine:5.1.0'
)
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.students.results.redis.configuration

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories

@Configuration
@EnableRedisRepositories
class RedisConfiguration {

@Bean
fun connectionFactory() = JedisConnectionFactory()

@Bean
fun redisTemplate(jedisConnectionFactory: JedisConnectionFactory) = RedisTemplate<Any, Any>().apply {
connectionFactory = jedisConnectionFactory
}
}
12 changes: 12 additions & 0 deletions persistence/src/main/kotlin/repository/impl/ExamsRepositoryImpl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package repository.impl

import arrow.core.Either
import com.students.results.entities.Exam
import com.students.results.repository.ExamsRepository
import com.students.results.repository.NotFoundException

class ExamsRepositoryImpl : ExamsRepository {
override fun findExamById(examId: Long): Either<NotFoundException, Exam> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package repository.impl

import arrow.core.Either
import com.students.results.entities.Student
import com.students.results.repository.RepositoryException
import com.students.results.repository.StudentsRepository

class StudentsRepositoryImpl : StudentsRepository {

override fun findStudentById(studentId: Long): Either<RepositoryException, Student> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun save(student: Student): Either<RepositoryException, Unit> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package embeded.redis.startup

import com.students.results.redis.configuration.RedisConfiguration
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.*
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.context.junit4.SpringRunner
import redis.embedded.RedisServer

@ExtendWith(SpringExtension::class)
@ContextConfiguration(classes = [RedisConfiguration::class, RedisEmbededConfiguration::class])
@TestInstance(Lifecycle.PER_CLASS)
abstract class RedisTestConfiguration protected constructor() {
@Autowired
private lateinit var redisServer: RedisServer

@BeforeAll
fun beforeStart() {
redisServer.start()
}

@AfterAll
fun afterStart() {
redisServer.stop()
}
}

class EmbededRedisTest : RedisTestConfiguration() {


@Test
fun runATEst() {
println("I am running")
}

}
Loading