From 5c05d729b83dfc367bf19e8559569030f6e400b3 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Thu, 6 Oct 2022 22:43:31 +0200 Subject: fix(web/server): Limit exception mapper to WebApplicationException This change updates the Quarkus-based web server to limit the default exception mapper to just WebApplicationException. Other exceptions should be considered internal server errors and must not be shared with users. --- .../server/rest/error/GenericExceptionMapper.kt | 45 ---------------------- .../rest/error/WebApplicationExceptionMapper.kt | 45 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 45 deletions(-) delete mode 100644 opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/error/GenericExceptionMapper.kt create mode 100644 opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/error/WebApplicationExceptionMapper.kt (limited to 'opendc-web/opendc-web-server') diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/error/GenericExceptionMapper.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/error/GenericExceptionMapper.kt deleted file mode 100644 index d8df72e0..00000000 --- a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/error/GenericExceptionMapper.kt +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2022 AtLarge Research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 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, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.opendc.web.server.rest.error - -import org.opendc.web.proto.ProtocolError -import javax.ws.rs.WebApplicationException -import javax.ws.rs.core.MediaType -import javax.ws.rs.core.Response -import javax.ws.rs.ext.ExceptionMapper -import javax.ws.rs.ext.Provider - -/** - * Helper class to transform an exception into an JSON error response. - */ -@Provider -class GenericExceptionMapper : ExceptionMapper { - override fun toResponse(exception: Exception): Response { - val code = if (exception is WebApplicationException) exception.response.status else 500 - - return Response.status(code) - .entity(ProtocolError(code, exception.message ?: "Unknown error")) - .type(MediaType.APPLICATION_JSON) - .build() - } -} diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/error/WebApplicationExceptionMapper.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/error/WebApplicationExceptionMapper.kt new file mode 100644 index 00000000..aa046abf --- /dev/null +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/error/WebApplicationExceptionMapper.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 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, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.error + +import org.opendc.web.proto.ProtocolError +import javax.ws.rs.WebApplicationException +import javax.ws.rs.core.MediaType +import javax.ws.rs.core.Response +import javax.ws.rs.ext.ExceptionMapper +import javax.ws.rs.ext.Provider + +/** + * Helper class to transform a [WebApplicationException] into an JSON error response. + */ +@Provider +class WebApplicationExceptionMapper : ExceptionMapper { + override fun toResponse(exception: WebApplicationException): Response { + val code = exception.response.status + + return Response.status(code) + .entity(ProtocolError(code, exception.message ?: "Unknown error")) + .type(MediaType.APPLICATION_JSON) + .build() + } +} -- cgit v1.2.3 From 5b8dfc78496452bd23fab59e3ead84a8941da779 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Thu, 6 Oct 2022 22:42:31 +0200 Subject: feat(web/server): Add support for accounting simulation time This change updates the Quarkus-based web server to add support for tracking and limiting the simulation minutes used by the user in order to prevent misuse of shared resources. --- .../main/kotlin/org/opendc/web/server/model/Job.kt | 11 +- .../org/opendc/web/server/model/UserAccounting.kt | 81 ++++++++ .../opendc/web/server/repository/JobRepository.kt | 3 +- .../server/repository/UserAccountingRepository.kt | 88 +++++++++ .../opendc/web/server/rest/runner/JobResource.kt | 2 +- .../org/opendc/web/server/service/JobService.kt | 22 ++- .../opendc/web/server/service/RunnerConversions.kt | 2 +- .../opendc/web/server/service/ScenarioService.kt | 11 +- .../web/server/service/UserAccountingService.kt | 107 +++++++++++ .../web/server/util/runner/QuarkusJobManager.kt | 15 +- .../main/resources/db/migration/V1.0.0__core.sql | 22 ++- .../web/server/rest/runner/JobResourceTest.kt | 10 +- .../server/service/UserAccountingServiceTest.kt | 203 +++++++++++++++++++++ 13 files changed, 551 insertions(+), 26 deletions(-) create mode 100644 opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/model/UserAccounting.kt create mode 100644 opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/repository/UserAccountingRepository.kt create mode 100644 opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt create mode 100644 opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/service/UserAccountingServiceTest.kt (limited to 'opendc-web/opendc-web-server') diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/model/Job.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/model/Job.kt index c07e07f0..84a71acf 100644 --- a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/model/Job.kt +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/model/Job.kt @@ -55,7 +55,7 @@ import javax.persistence.Table name = "Job.updateOne", query = """ UPDATE Job j - SET j.state = :newState, j.updatedAt = :updatedAt, j.results = :results + SET j.state = :newState, j.updatedAt = :updatedAt, j.runtime = :runtime, j.results = :results WHERE j.id = :id AND j.state = :oldState """ ) @@ -66,6 +66,9 @@ class Job( @GeneratedValue(strategy = GenerationType.AUTO) val id: Long, + @Column(name = "created_by", nullable = false, updatable = false) + val createdBy: String, + @OneToOne(optional = false, mappedBy = "job", fetch = FetchType.EAGER) @JoinColumn(name = "scenario_id", nullable = false) val scenario: Scenario, @@ -91,6 +94,12 @@ class Job( @Column(nullable = false) var state: JobState = JobState.PENDING + /** + * The runtime of the job (in seconds). + */ + @Column(nullable = false) + var runtime: Int = 0 + /** * Experiment results in JSON */ diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/model/UserAccounting.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/model/UserAccounting.kt new file mode 100644 index 00000000..5b813044 --- /dev/null +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/model/UserAccounting.kt @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 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, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.model + +import java.time.LocalDate +import javax.persistence.Column +import javax.persistence.Entity +import javax.persistence.Id +import javax.persistence.NamedQueries +import javax.persistence.NamedQuery +import javax.persistence.Table + +/** + * Entity to track the number of simulation minutes used by a user. + */ +@Entity +@Table(name = "user_accounting") +@NamedQueries( + value = [ + NamedQuery( + name = "UserAccounting.consumeBudget", + query = """ + UPDATE UserAccounting a + SET a.simulationTime = a.simulationTime + :seconds + WHERE a.userId = :userId AND a.periodEnd = :periodEnd + """ + ), + NamedQuery( + name = "UserAccounting.resetBudget", + query = """ + UPDATE UserAccounting a + SET a.periodEnd = :periodEnd, a.simulationTime = :seconds + WHERE a.userId = :userId AND a.periodEnd = :oldPeriodEnd + """ + ) + ] +) +class UserAccounting( + @Id + @Column(name = "user_id", nullable = false) + val userId: String, + + /** + * The end of the accounting period. + */ + @Column(name = "period_end", nullable = false) + var periodEnd: LocalDate, + + /** + * The number of simulation seconds to be used per accounting period. + */ + @Column(name = "simulation_time_budget", nullable = false) + var simulationTimeBudget: Int +) { + /** + * The number of simulation seconds used in this period. This number should reset once the accounting period has + * been reached. + */ + @Column(name = "simulation_time", nullable = false) + var simulationTime: Int = 0 +} diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/repository/JobRepository.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/repository/JobRepository.kt index 5fee07a3..e9bf0af0 100644 --- a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/repository/JobRepository.kt +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/repository/JobRepository.kt @@ -79,12 +79,13 @@ class JobRepository @Inject constructor(private val em: EntityManager) { * @param results The results to possible set. * @return `true` when the update succeeded`, `false` when there was a conflict. */ - fun updateOne(job: Job, newState: JobState, time: Instant, results: Map?): Boolean { + fun updateOne(job: Job, newState: JobState, time: Instant, runtime: Int, results: Map?): Boolean { val count = em.createNamedQuery("Job.updateOne") .setParameter("id", job.id) .setParameter("oldState", job.state) .setParameter("newState", newState) .setParameter("updatedAt", Instant.now()) + .setParameter("runtime", runtime) .setParameter("results", results) .executeUpdate() em.refresh(job) diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/repository/UserAccountingRepository.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/repository/UserAccountingRepository.kt new file mode 100644 index 00000000..f0265d3d --- /dev/null +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/repository/UserAccountingRepository.kt @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 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, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.repository + +import org.opendc.web.server.model.UserAccounting +import java.time.LocalDate +import javax.enterprise.context.ApplicationScoped +import javax.inject.Inject +import javax.persistence.EntityManager + +/** + * A repository to manage [UserAccounting] entities. + */ +@ApplicationScoped +class UserAccountingRepository @Inject constructor(private val em: EntityManager) { + /** + * Find the [UserAccounting] object for the specified [userId]. + * + * @param userId The unique identifier of the user. + * @return The [UserAccounting] object or `null` if it does not exist. + */ + fun findForUser(userId: String): UserAccounting? { + return em.find(UserAccounting::class.java, userId) + } + + /** + * Save the specified [UserAccounting] object to the database. + */ + fun save(accounting: UserAccounting) { + em.persist(accounting) + } + + /** + * Atomically consume the budget for the specified [UserAccounting] object. + * + * @param accounting The [UserAccounting] object to update atomically. + * @param seconds The number of seconds to consume from the user. + * @return `true` when the update succeeded`, `false` when there was a conflict. + */ + fun consumeBudget(accounting: UserAccounting, seconds: Int): Boolean { + val count = em.createNamedQuery("UserAccounting.consumeBudget") + .setParameter("userId", accounting.userId) + .setParameter("periodEnd", accounting.periodEnd) + .setParameter("seconds", seconds) + .executeUpdate() + em.refresh(accounting) + return count > 0 + } + + /** + * Atomically reset the budget for the specified [UserAccounting] object. + * + * @param accounting The [UserAccounting] object to update atomically. + * @param periodEnd The new end period for the budget. + * @param seconds The number of seconds that have already been consumed. + * @return `true` when the update succeeded`, `false` when there was a conflict. + */ + fun resetBudget(accounting: UserAccounting, periodEnd: LocalDate, seconds: Int): Boolean { + val count = em.createNamedQuery("UserAccounting.resetBudget") + .setParameter("userId", accounting.userId) + .setParameter("oldPeriodEnd", accounting.periodEnd) + .setParameter("periodEnd", periodEnd) + .setParameter("seconds", seconds) + .executeUpdate() + em.refresh(accounting) + return count > 0 + } +} diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/runner/JobResource.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/runner/JobResource.kt index 4aa2f6a1..d0432360 100644 --- a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/runner/JobResource.kt +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/runner/JobResource.kt @@ -65,7 +65,7 @@ class JobResource @Inject constructor(private val jobService: JobService) { @Transactional fun update(@PathParam("job") id: Long, @Valid update: Job.Update): Job { return try { - jobService.updateState(id, update.state, update.results) + jobService.updateState(id, update.state, update.runtime, update.results) ?: throw WebApplicationException("Job not found", 404) } catch (e: IllegalArgumentException) { throw WebApplicationException(e, 400) diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/JobService.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/JobService.kt index 6b49e8b6..a0ebd4f4 100644 --- a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/JobService.kt +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/JobService.kt @@ -33,7 +33,10 @@ import javax.inject.Inject * Service for managing [Job]s. */ @ApplicationScoped -class JobService @Inject constructor(private val repository: JobRepository) { +class JobService @Inject constructor( + private val repository: JobRepository, + private val accountingService: UserAccountingService +) { /** * Query the pending simulation jobs. */ @@ -50,8 +53,13 @@ class JobService @Inject constructor(private val repository: JobRepository) { /** * Atomically update the state of a [Job]. + * + * @param id The identifier of the job. + * @param newState The next state for the job. + * @param runtime The runtime of the job (in seconds). + * @param results The potential results of the job. */ - fun updateState(id: Long, newState: JobState, results: Map?): Job? { + fun updateState(id: Long, newState: JobState, runtime: Int, results: Map?): Job? { val entity = repository.findOne(id) ?: return null val state = entity.state if (!state.isTransitionLegal(newState)) { @@ -59,7 +67,15 @@ class JobService @Inject constructor(private val repository: JobRepository) { } val now = Instant.now() - if (!repository.updateOne(entity, newState, now, results)) { + var nextState = newState + val consumedBudget = (runtime - entity.runtime).coerceAtLeast(1) + + // Check whether the user still has any simulation budget left + if (accountingService.consumeSimulationBudget(entity.createdBy, consumedBudget) && nextState == JobState.RUNNING) { + nextState = JobState.FAILED // User has consumed all their budget; cancel the job + } + + if (!repository.updateOne(entity, nextState, now, runtime, results)) { throw IllegalStateException("Conflicting update") } diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/RunnerConversions.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/RunnerConversions.kt index 1dcc95ee..465ac2df 100644 --- a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/RunnerConversions.kt +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/RunnerConversions.kt @@ -49,7 +49,7 @@ internal fun Portfolio.toRunnerDto(): org.opendc.web.proto.runner.Portfolio { * Convert a [Job] into a runner-facing DTO. */ internal fun Job.toRunnerDto(): org.opendc.web.proto.runner.Job { - return org.opendc.web.proto.runner.Job(id, scenario.toRunnerDto(), state, createdAt, updatedAt, results) + return org.opendc.web.proto.runner.Job(id, scenario.toRunnerDto(), state, createdAt, updatedAt, runtime, results) } /** diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/ScenarioService.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/ScenarioService.kt index fad4e56f..083f2451 100644 --- a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/ScenarioService.kt +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/ScenarioService.kt @@ -22,6 +22,7 @@ package org.opendc.web.server.service +import org.opendc.web.proto.JobState import org.opendc.web.server.model.Job import org.opendc.web.server.model.Scenario import org.opendc.web.server.model.Workload @@ -44,7 +45,8 @@ class ScenarioService @Inject constructor( private val portfolioRepository: PortfolioRepository, private val topologyRepository: TopologyRepository, private val traceRepository: TraceRepository, - private val scenarioRepository: ScenarioRepository + private val scenarioRepository: ScenarioRepository, + private val accountingService: UserAccountingService ) { /** * List all [Scenario]s that belong a certain portfolio. @@ -123,7 +125,12 @@ class ScenarioService @Inject constructor( request.phenomena, request.schedulerName ) - val job = Job(0, scenario, now, portfolio.targets.repeats) + val job = Job(0, userId, scenario, now, portfolio.targets.repeats) + + // Fail the job if there is not enough budget for the simulation + if (!accountingService.hasSimulationBudget(userId)) { + job.state = JobState.FAILED + } scenario.job = job portfolio.scenarios.add(scenario) diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt new file mode 100644 index 00000000..13440a81 --- /dev/null +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 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, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.service + +import org.eclipse.microprofile.config.inject.ConfigProperty +import org.opendc.web.server.model.UserAccounting +import org.opendc.web.server.repository.UserAccountingRepository +import java.time.Duration +import java.time.LocalDate +import java.time.temporal.TemporalAdjusters +import javax.enterprise.context.ApplicationScoped +import javax.inject.Inject +import javax.persistence.EntityExistsException + +/** + * Service for tracking the simulation budget of users. + * + * @param repository The [UserAccountingRepository] used to communicate with the database. + * @param simulationBudget The default simulation budget for new users. + */ +@ApplicationScoped +class UserAccountingService @Inject constructor( + private val repository: UserAccountingRepository, + @ConfigProperty(name = "opendc.accounting.simulation-budget", defaultValue = "2000m") + private val simulationBudget: Duration +) { + /** + * Determine whether the user with [userId] has any remaining simulation budget. + * + * @param userId The unique identifier of the user. + * @return `true` when the user still has budget left, `false` otherwise. + */ + fun hasSimulationBudget(userId: String): Boolean { + val accounting = repository.findForUser(userId) ?: return true + val today = LocalDate.now() + + // The accounting period must be over or there must be budget remaining. + return !today.isBefore(accounting.periodEnd) || accounting.simulationTimeBudget > accounting.simulationTime + } + + /** + * Consume [seconds] from the simulation budget of the user with [userId]. + * + * @param userId The unique identifier of the user. + * @param seconds The seconds to consume from the simulation budget. + * @param `true` if the user has consumed his full budget or `false` if there is still budget remaining. + */ + fun consumeSimulationBudget(userId: String, seconds: Int): Boolean { + val today = LocalDate.now() + val nextAccountingPeriod = today.with(TemporalAdjusters.firstDayOfNextMonth()) + val repository = repository + + // We need to be careful to prevent conflicts in case of concurrency + // 1. First, we try to create the accounting object if it does not exist yet. This may fail if another instance + // creates the object concurrently. + // 2. Second, we check if the budget needs to be reset and try this atomically. + // 3. Finally, we atomically consume the budget from the object + // This is repeated three times in case there is a conflict + repeat(3) { + val accounting = repository.findForUser(userId) + + if (accounting == null) { + try { + val newAccounting = UserAccounting(userId, nextAccountingPeriod, simulationBudget.toSeconds().toInt()) + newAccounting.simulationTime = seconds + repository.save(newAccounting) + + return newAccounting.simulationTime >= newAccounting.simulationTimeBudget + } catch (e: EntityExistsException) { + // Conflict due to concurrency; retry + } + } else { + val success = if (!today.isBefore(accounting.periodEnd)) { + repository.resetBudget(accounting, nextAccountingPeriod, seconds) + } else { + repository.consumeBudget(accounting, seconds) + } + + if (success) { + return accounting.simulationTimeBudget <= accounting.simulationTime + } + } + } + + throw IllegalStateException("Failed to allocate consume budget due to conflict") + } +} diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/util/runner/QuarkusJobManager.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/util/runner/QuarkusJobManager.kt index 4704c5ae..742a510c 100644 --- a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/util/runner/QuarkusJobManager.kt +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/util/runner/QuarkusJobManager.kt @@ -43,7 +43,7 @@ class QuarkusJobManager @Inject constructor(private val service: JobService) : J @Transactional override fun claim(id: Long): Boolean { return try { - service.updateState(id, JobState.CLAIMED, null) + service.updateState(id, JobState.CLAIMED, 0, null) true } catch (e: IllegalStateException) { false @@ -51,17 +51,18 @@ class QuarkusJobManager @Inject constructor(private val service: JobService) : J } @Transactional - override fun heartbeat(id: Long) { - service.updateState(id, JobState.RUNNING, null) + override fun heartbeat(id: Long, runtime: Int): Boolean { + val res = service.updateState(id, JobState.RUNNING, runtime, null) + return res?.state != JobState.FAILED } @Transactional - override fun fail(id: Long) { - service.updateState(id, JobState.FAILED, null) + override fun fail(id: Long, runtime: Int) { + service.updateState(id, JobState.FAILED, runtime, null) } @Transactional - override fun finish(id: Long, results: Map) { - service.updateState(id, JobState.FINISHED, results) + override fun finish(id: Long, runtime: Int, results: Map) { + service.updateState(id, JobState.FINISHED, runtime, results) } } diff --git a/opendc-web/opendc-web-server/src/main/resources/db/migration/V1.0.0__core.sql b/opendc-web/opendc-web-server/src/main/resources/db/migration/V1.0.0__core.sql index 183a70ea..1a0e4046 100644 --- a/opendc-web/opendc-web-server/src/main/resources/db/migration/V1.0.0__core.sql +++ b/opendc-web/opendc-web-server/src/main/resources/db/migration/V1.0.0__core.sql @@ -65,15 +65,27 @@ create table scenarios create table jobs ( - id bigint not null, - created_at timestamp not null, - repeats integer not null, + id bigint not null, + created_by varchar(255) not null, + created_at timestamp not null, + repeats integer not null, results jsonb, - state integer not null, - updated_at timestamp not null, + state integer not null, + runtime integer not null, + updated_at timestamp not null, primary key (id) ); +-- User accounting +create table user_accounting +( + user_id varchar(255) not null, + period_end date not null, + simulation_time integer not null, + simulation_time_budget integer not null, + primary key (user_id) +); + -- Workload traces available to the user. create table traces ( diff --git a/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/runner/JobResourceTest.kt b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/runner/JobResourceTest.kt index 9aca58e9..4a86c928 100644 --- a/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/runner/JobResourceTest.kt +++ b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/runner/JobResourceTest.kt @@ -63,7 +63,7 @@ class JobResourceTest { private val dummyTopology = Topology(1, 1, "test", emptyList(), Instant.now(), Instant.now()) private val dummyTrace = Trace("bitbrains", "Bitbrains", "vm") private val dummyScenario = Scenario(1, 1, dummyPortfolio, "test", Workload(dummyTrace, 1.0), dummyTopology, OperationalPhenomena(false, false), "test") - private val dummyJob = Job(1, dummyScenario, JobState.PENDING, Instant.now(), Instant.now()) + private val dummyJob = Job(1, dummyScenario, JobState.PENDING, Instant.now(), Instant.now(), 0) @BeforeEach fun setUp() { @@ -151,10 +151,10 @@ class JobResourceTest { @Test @TestSecurity(user = "testUser", roles = ["runner"]) fun testUpdateNonExistent() { - every { jobService.updateState(1, any(), any()) } returns null + every { jobService.updateState(1, any(), any(), any()) } returns null Given { - body(Job.Update(JobState.PENDING)) + body(Job.Update(JobState.PENDING, 0)) contentType(ContentType.JSON) } When { post("/1") @@ -170,10 +170,10 @@ class JobResourceTest { @Test @TestSecurity(user = "testUser", roles = ["runner"]) fun testUpdateState() { - every { jobService.updateState(1, any(), any()) } returns dummyJob.copy(state = JobState.CLAIMED) + every { jobService.updateState(1, any(), any(), any()) } returns dummyJob.copy(state = JobState.CLAIMED) Given { - body(Job.Update(JobState.CLAIMED)) + body(Job.Update(JobState.CLAIMED, 0)) contentType(ContentType.JSON) } When { post("/1") diff --git a/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/service/UserAccountingServiceTest.kt b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/service/UserAccountingServiceTest.kt new file mode 100644 index 00000000..fdf04787 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/service/UserAccountingServiceTest.kt @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 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, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.service + +import io.mockk.every +import io.mockk.mockk +import io.quarkus.test.junit.QuarkusTest +import org.junit.jupiter.api.Assertions.assertAll +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.opendc.web.server.model.UserAccounting +import org.opendc.web.server.repository.UserAccountingRepository +import java.time.Duration +import java.time.LocalDate +import javax.persistence.EntityExistsException + +/** + * Test suite for the [UserAccountingService]. + */ +@QuarkusTest +class UserAccountingServiceTest { + /** + * The [UserAccountingRepository] that is mocked. + */ + private val repository: UserAccountingRepository = mockk() + + /** + * The [UserAccountingService] instance under test. + */ + private val service: UserAccountingService = UserAccountingService(repository, Duration.ofHours(1)) + + @Test + fun testGetUserDoesNotExist() { + val userId = "test" + + every { repository.findForUser(userId) } returns null + + val accounting = service.getAccounting(userId) + + assertTrue(accounting.periodEnd.isAfter(LocalDate.now())) + assertEquals(0, accounting.simulationTime) + } + + @Test + fun testGetUserDoesExist() { + val userId = "test" + + val now = LocalDate.now() + val periodEnd = now.plusMonths(1) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600).also { it.simulationTime = 32 } + + val accounting = service.getAccounting(userId) + + assertAll( + { assertEquals(periodEnd, accounting.periodEnd) }, + { assertEquals(32, accounting.simulationTime) }, + { assertEquals(3600, accounting.simulationTimeBudget) } + ) + } + + @Test + fun testHasBudgetUserDoesNotExist() { + val userId = "test" + + every { repository.findForUser(userId) } returns null + + assertTrue(service.hasSimulationBudget(userId)) + } + + @Test + fun testHasBudget() { + val userId = "test" + val periodEnd = LocalDate.now().plusMonths(2) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600) + + assertTrue(service.hasSimulationBudget(userId)) + } + + @Test + fun testHasBudgetExceededButPeriodExpired() { + val userId = "test" + val periodEnd = LocalDate.now().minusMonths(2) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600).also { it.simulationTime = 3900 } + + assertTrue(service.hasSimulationBudget(userId)) + } + + @Test + fun testHasBudgetPeriodExpired() { + val userId = "test" + val periodEnd = LocalDate.now().minusMonths(2) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600) + + assertTrue(service.hasSimulationBudget(userId)) + } + + @Test + fun testHasBudgetExceeded() { + val userId = "test" + val periodEnd = LocalDate.now().plusMonths(1) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600).also { it.simulationTime = 3900 } + + assertFalse(service.hasSimulationBudget(userId)) + } + + @Test + fun testConsumeBudgetNewUser() { + val userId = "test" + + every { repository.findForUser(userId) } returns null + every { repository.save(any()) } returns Unit + + assertFalse(service.consumeSimulationBudget(userId, 10)) + } + + @Test + fun testConsumeBudgetNewUserExceeded() { + val userId = "test" + + every { repository.findForUser(userId) } returns null + every { repository.save(any()) } returns Unit + + assertTrue(service.consumeSimulationBudget(userId, 4000)) + } + + @Test + fun testConsumeBudgetNewUserConflict() { + val userId = "test" + + val periodEnd = LocalDate.now().plusMonths(1) + + every { repository.findForUser(userId) } returns null andThen UserAccounting(userId, periodEnd, 3600) + every { repository.save(any()) } throws EntityExistsException() + every { repository.consumeBudget(any(), any()) } answers { + val accounting = it.invocation.args[0] as UserAccounting + accounting.simulationTime -= it.invocation.args[1] as Int + true + } + + assertFalse(service.consumeSimulationBudget(userId, 10)) + } + + @Test + fun testConsumeBudgetResetSuccess() { + val userId = "test" + + val periodEnd = LocalDate.now().minusMonths(2) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600).also { it.simulationTime = 3900 } + every { repository.resetBudget(any(), any(), any()) } answers { + val accounting = it.invocation.args[0] as UserAccounting + accounting.periodEnd = it.invocation.args[1] as LocalDate + accounting.simulationTime = it.invocation.args[2] as Int + true + } + + assertTrue(service.consumeSimulationBudget(userId, 4000)) + } + + @Test + fun testInfiniteConflict() { + val userId = "test" + + val periodEnd = LocalDate.now().plusMonths(1) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600) + every { repository.consumeBudget(any(), any()) } answers { + val accounting = it.invocation.args[0] as UserAccounting + accounting.simulationTime -= it.invocation.args[1] as Int + false + } + + assertThrows { service.consumeSimulationBudget(userId, 10) } + } +} -- cgit v1.2.3 From 2c739c4fdb5aab1d1d4480d4e233a1174bc85494 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Thu, 6 Oct 2022 23:05:27 +0200 Subject: feat(web/server): Add API for querying user accounting data This change updates the Quarkus-based web server with a new endpoint for querying data about the active user including accounting data. --- .../opendc/web/server/rest/user/UserResource.kt | 45 ++++++++++++++ .../web/server/service/UserAccountingService.kt | 23 +++++++- .../org/opendc/web/server/service/UserService.kt | 44 ++++++++++++++ .../web/server/rest/user/UserResourceTest.kt | 69 ++++++++++++++++++++++ 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/user/UserResource.kt create mode 100644 opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserService.kt create mode 100644 opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/user/UserResourceTest.kt (limited to 'opendc-web/opendc-web-server') diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/user/UserResource.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/user/UserResource.kt new file mode 100644 index 00000000..d640cc08 --- /dev/null +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/user/UserResource.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 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, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.user + +import io.quarkus.security.identity.SecurityIdentity +import org.opendc.web.proto.user.User +import org.opendc.web.server.service.UserService +import javax.annotation.security.RolesAllowed +import javax.inject.Inject +import javax.ws.rs.GET +import javax.ws.rs.Path + +/** + * A resource representing the active user. + */ +@Path("/users") +@RolesAllowed("openid") +class UserResource @Inject constructor(private val userService: UserService, private val identity: SecurityIdentity) { + /** + * Get the current active user data. + */ + @GET + @Path("me") + fun get(): User = userService.getUser(identity) +} diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt index 13440a81..11066bfb 100644 --- a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt @@ -31,6 +31,7 @@ import java.time.temporal.TemporalAdjusters import javax.enterprise.context.ApplicationScoped import javax.inject.Inject import javax.persistence.EntityExistsException +import org.opendc.web.proto.user.UserAccounting as UserAccountingDto /** * Service for tracking the simulation budget of users. @@ -44,6 +45,19 @@ class UserAccountingService @Inject constructor( @ConfigProperty(name = "opendc.accounting.simulation-budget", defaultValue = "2000m") private val simulationBudget: Duration ) { + /** + * Return the [UserAccountingDto] object for the user with the specified [userId]. If the object does not exist in the + * database, a default value is constructed. + */ + fun getAccounting(userId: String): UserAccountingDto { + val accounting = repository.findForUser(userId) + return if (accounting != null) { + UserAccountingDto(accounting.periodEnd, accounting.simulationTime, accounting.simulationTimeBudget) + } else { + UserAccountingDto(getNextAccountingPeriod(), 0, simulationBudget.toSeconds().toInt()) + } + } + /** * Determine whether the user with [userId] has any remaining simulation budget. * @@ -67,7 +81,7 @@ class UserAccountingService @Inject constructor( */ fun consumeSimulationBudget(userId: String, seconds: Int): Boolean { val today = LocalDate.now() - val nextAccountingPeriod = today.with(TemporalAdjusters.firstDayOfNextMonth()) + val nextAccountingPeriod = getNextAccountingPeriod(today) val repository = repository // We need to be careful to prevent conflicts in case of concurrency @@ -104,4 +118,11 @@ class UserAccountingService @Inject constructor( throw IllegalStateException("Failed to allocate consume budget due to conflict") } + + /** + * Helper method to find next accounting period. + */ + private fun getNextAccountingPeriod(today: LocalDate = LocalDate.now()): LocalDate { + return today.with(TemporalAdjusters.firstDayOfNextMonth()) + } } diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserService.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserService.kt new file mode 100644 index 00000000..39352267 --- /dev/null +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserService.kt @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 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, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.service + +import io.quarkus.security.identity.SecurityIdentity +import org.opendc.web.proto.user.User +import javax.enterprise.context.ApplicationScoped +import javax.inject.Inject + +/** + * Service for managing [User]s. + */ +@ApplicationScoped +class UserService @Inject constructor(private val accounting: UserAccountingService) { + /** + * Obtain the [User] object for the specified [identity]. + */ + fun getUser(identity: SecurityIdentity): User { + val userId = identity.principal.name + val accounting = accounting.getAccounting(userId) + + return User(userId, accounting) + } +} diff --git a/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/user/UserResourceTest.kt b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/user/UserResourceTest.kt new file mode 100644 index 00000000..36af20f4 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/user/UserResourceTest.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 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, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.user + +import io.quarkus.test.common.http.TestHTTPEndpoint +import io.quarkus.test.junit.QuarkusTest +import io.quarkus.test.security.TestSecurity +import io.restassured.http.ContentType +import io.restassured.module.kotlin.extensions.Then +import io.restassured.module.kotlin.extensions.When +import org.hamcrest.Matchers +import org.junit.jupiter.api.Test + +/** + * Test suite for [UserResource]. + */ +@QuarkusTest +@TestHTTPEndpoint(UserResource::class) +class UserResourceTest { + /** + * Test that tries to obtain the profile of the active user. + */ + @Test + @TestSecurity(user = "testUser", roles = ["openid"]) + fun testMe() { + When { + get("me") + } Then { + statusCode(200) + contentType(ContentType.JSON) + + body("userId", Matchers.equalTo("testUser")) + body("accounting.simulationTime", Matchers.equalTo(0)) + body("accounting.simulationTimeBudget", Matchers.greaterThan(0)) + } + } + + /** + * Test that tries to obtain the profile of the active user without authorization. + */ + @Test + fun testMeUnauthorized() { + When { + get("me") + } Then { + statusCode(401) + } + } +} -- cgit v1.2.3