Skip to content

Commit

Permalink
we don't need sealed classes for errors now
Browse files Browse the repository at this point in the history
  • Loading branch information
tonilopezmr committed Mar 14, 2019
1 parent 7a05a3e commit 2ae0408
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 56 deletions.
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ root = true
end_of_line = lf
insert_final_newline = true

[*.{java,kt,kts,scala,rs,xml}]
[*.{java, kt, kts, scala, rs, xml}]
indent_size = 2
continuation_indent_size=2
continuation_indent_size = 2
insert_final_newline = false
max_line_length=100
max_line_length = 100
30 changes: 15 additions & 15 deletions admin-server/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'org.springframework.boot' version '2.1.2.RELEASE'
id 'org.jetbrains.kotlin.jvm' version '1.2.71'
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
id 'org.springframework.boot' version '2.1.2.RELEASE'
id 'org.jetbrains.kotlin.jvm' version '1.2.71'
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
}

apply plugin: 'io.spring.dependency-management'
Expand All @@ -11,29 +11,29 @@ version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation 'de.codecentric:spring-boot-admin-starter-server:2.1.2'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'

}

compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}

compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ class ActuatorSecurityConfig : WebSecurityConfigurerAdapter() {
successHandler.setDefaultTargetUrl("/")

http.authorizeRequests()
.antMatchers("/assets/**").permitAll()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").successHandler(successHandler).and()
.logout().logoutUrl("/logout").and()
.httpBasic().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
"/instances",
"/actuator/**"
)
.antMatchers("/assets/**").permitAll()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").successHandler(successHandler).and()
.logout().logoutUrl("/logout").and()
.httpBasic().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
"/instances",
"/actuator/**"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.karumi.springbootkotlin.authentication.domain.usecase
import arrow.core.Option
import arrow.core.Try
import arrow.core.failure
import com.karumi.springbootkotlin.developers.domain.AlreadyRegistered
import com.karumi.springbootkotlin.developers.domain.Developer
import com.karumi.springbootkotlin.developers.domain.DeveloperError
import com.karumi.springbootkotlin.developers.storage.DeveloperDao
import org.springframework.stereotype.Component

Expand All @@ -21,7 +21,7 @@ class RegisterDeveloper(
it.fold({
developerDao.create(developer)
}, {
DeveloperError.AlreadyRegistered.failure()
AlreadyRegistered.failure()
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ data class Developer(
val id: UUID? = null
)

sealed class DeveloperError(message: String) : RuntimeException(message) {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
object StorageError : DeveloperError("Internal storage error")
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
object StorageError : RuntimeException("Internal storage error")

@ResponseStatus(HttpStatus.NOT_FOUND)
object NotFound : DeveloperError("Developer not found")
@ResponseStatus(HttpStatus.NOT_FOUND)
object NotFound : RuntimeException("Developer not found")

@ResponseStatus(HttpStatus.BAD_REQUEST)
object NotKarumier : DeveloperError("Developer isn't karumier")
@ResponseStatus(HttpStatus.BAD_REQUEST)
object NotKarumier : RuntimeException("Developer isn't karumier")

@ResponseStatus(HttpStatus.CONFLICT)
object AlreadyRegistered : DeveloperError("Developer already registered")
}
@ResponseStatus(HttpStatus.CONFLICT)
object AlreadyRegistered : RuntimeException("Developer already registered")
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import arrow.core.failure
import arrow.core.success
import com.karumi.springbootkotlin.common.mapException
import com.karumi.springbootkotlin.developers.domain.Developer
import com.karumi.springbootkotlin.developers.domain.DeveloperError
import com.karumi.springbootkotlin.developers.domain.DeveloperValidator
import com.karumi.springbootkotlin.developers.domain.NotKarumier
import com.karumi.springbootkotlin.developers.domain.StorageError
import com.karumi.springbootkotlin.developers.storage.DeveloperDao
import org.springframework.stereotype.Component

Expand All @@ -19,13 +20,13 @@ class CreateKarumiDeveloper(
validKarumiDeveloper(developer)
.flatMap {
developerDao.create(it)
.mapException { DeveloperError.StorageError }
.mapException { StorageError }
}

private fun validKarumiDeveloper(developer: Developer): Try<Developer> =
if (DeveloperValidator.isKarumiDeveloper(developer)) {
developer.success()
} else {
DeveloperError.NotKarumier.failure()
NotKarumier.failure()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import arrow.core.Try
import com.karumi.springbootkotlin.common.mapException
import com.karumi.springbootkotlin.common.toTry
import com.karumi.springbootkotlin.developers.domain.Developer
import com.karumi.springbootkotlin.developers.domain.DeveloperError
import com.karumi.springbootkotlin.developers.domain.NotFound
import com.karumi.springbootkotlin.developers.domain.StorageError
import com.karumi.springbootkotlin.developers.storage.DeveloperDao
import org.springframework.stereotype.Component
import java.util.UUID
Expand All @@ -15,7 +16,7 @@ class GetDeveloper(
) {

operator fun invoke(developerId: UUID): Try<Developer> =
developerDao.getById(developerId)
.mapException { DeveloperError.StorageError }
.flatMap { devOption -> devOption.toTry { DeveloperError.NotFound } }
developerDao.getById(developerId)
.mapException { StorageError }
.flatMap { devOption -> devOption.toTry { NotFound } }
}
3 changes: 3 additions & 0 deletions src/main/requests/Actuator.http
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ Authorization: Bearer {{token}}
GET http://{{host}}:{{port}}/actuator/metrics/jvm.memory.used
Accept: application/json
Authorization: Bearer {{token}}

###

GET http://{{host}}:{{port}}/heapdump
Accept: application/json
Authorization: Bearer {{token}}

###

GET http://{{host}}:{{port}}/httptrace
Accept: application/json
Authorization: Bearer {{token}}

###
1 change: 0 additions & 1 deletion src/main/requests/DeveloperController.http
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

POST http://{{host}}:{{port}}/developer
Content-Type: application/json
Authorization: Bearer {{token}}
Expand Down
4 changes: 0 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
server.port=5000
spring.boot.admin.client.url=http://localhost:8080

spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin-password

spring.h2.console.enabled=true
spring.h2.console.path=/h2

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.main.allow-bean-definition-overriding=true
6 changes: 5 additions & 1 deletion src/test/resources/ExpectedDeveloper.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{"id":"c04ecc99-d7ba-460c-be78-6995805c5175","username":"Unknown","email":"email@karumi.com"}
{
"id": "c04ecc99-d7ba-460c-be78-6995805c5175",
"username": "Unknown",
"email": "email@karumi.com"
}

0 comments on commit 2ae0408

Please sign in to comment.