Skip to content

Upgrade play modules to scala 3 #1365

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

Merged
merged 8 commits into from
May 26, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scala.concurrent.Future

trait DbManager {

def dbConfig: DatabaseConfig[_]
def dbConfig: DatabaseConfig[?]

def execute[T](dbio: DBIO[T]): Future[T] = dbConfig.db.run(dbio)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SlickArrivalRepository @Inject() (val dbProfile: JdbcProfile)(implicit
origin,
destination,
plane
) <> ((Arrival.apply _).tupled, Arrival.unapply)
) <> ((Arrival.apply).tupled, Arrival.unapply)
}

/** The starting point for all queries on the people table.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ArrivalDecoratorService @Inject() (configuration: Configuration) {
private val maximumMediumNameLength =
configuration.get[Int]("medium-name-max")

def decorate(undecorated: Arrival): Arrival with Size = new Arrival(
def decorate(undecorated: Arrival): Arrival & Size = new Arrival(
undecorated.planeId,
undecorated.origin,
undecorated.destination,
Expand Down
2 changes: 1 addition & 1 deletion play-scala/application-tests/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version := "1.0-SNAPSHOT"

enablePlugins(PlayScala)

scalaVersion := ScalaVersions.scala2Version
scalaVersion := ScalaVersions.scala3Version

libraryDependencies += guice
libraryDependencies += "com.h2database" % "h2" % "1.4.200"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import play.api.http.Status.{BAD_REQUEST, NO_CONTENT}
import play.api.mvc.Headers
import play.api.mvc.Results.NoContent
import play.api.test.{FakeRequest, Helpers}
import play.api.mvc.{Action, AnyContent}

class SourceActionsUnitTest
extends AnyWordSpec
Expand All @@ -20,15 +21,17 @@ class SourceActionsUnitTest

"SourceAction" should {
"return BAD_REQUEST status for missing source header" in {
val testee = SourceAction(anyContentParser)(globalEc) { _ => NoContent }
val testee: Action[AnyContent] =
SourceAction(anyContentParser)(globalEc) { _ => NoContent }

whenReady(testee.apply(FakeRequest())) { result =>
assert(result.header.status === BAD_REQUEST)
}
}

"return NO_CONTENT status for when source header is present" in {
val testee = SourceAction(anyContentParser)(globalEc) { _ => NoContent }
val testee: Action[AnyContent] =
SourceAction(anyContentParser)(globalEc) { _ => NoContent }
whenReady(
testee.apply(FakeRequest().withHeaders(Headers("source" -> "foo")))
) { result =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ class ArrivalDecoratorServiceUnitTest extends MixedPlaySpec {
)
.build()
) {
private val testee = app.injector.instanceOf[ArrivalDecoratorService]
private val arrival = Arrival(1L, "Athens", "Heathrow", "12345")
assert(testee.decorate(arrival).short)
assert(!testee.decorate(arrival).medium)
assert(!testee.decorate(arrival).long)
override def running() = {
val testee = app.injector.instanceOf[ArrivalDecoratorService]
val arrival = Arrival(1L, "Athens", "Heathrow", "12345")
assert(testee.decorate(arrival).short)
assert(!testee.decorate(arrival).medium)
assert(!testee.decorate(arrival).long)
}
}

"mark as medium an arrival with plane name length = 5 with overridden configuration" in new App(
Expand All @@ -37,11 +39,13 @@ class ArrivalDecoratorServiceUnitTest extends MixedPlaySpec {
)
.build()
) {
private val testee = app.injector.instanceOf[ArrivalDecoratorService]
private val arrival = Arrival(1L, "Athens", "Heathrow", "12345")
assert(!testee.decorate(arrival).short)
assert(testee.decorate(arrival).medium)
assert(!testee.decorate(arrival).long)
override def running() = {
val testee = app.injector.instanceOf[ArrivalDecoratorService]
val arrival = Arrival(1L, "Athens", "Heathrow", "12345")
assert(!testee.decorate(arrival).short)
assert(testee.decorate(arrival).medium)
assert(!testee.decorate(arrival).long)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import slick.dbio.{DBIO, SuccessAction}
import scala.concurrent.Future

class InMemoryDbManager extends DbManager {
override def dbConfig: DatabaseConfig[_] = ???
override def dbConfig: DatabaseConfig[?] = ???

override def execute[T](dbio: DBIO[T]): Future[T] =
Future.successful(dbio.asInstanceOf[SuccessAction[T]].value)
Expand Down
2 changes: 1 addition & 1 deletion play-scala/async-tasks/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ version := "1.0-SNAPSHOT"

enablePlugins(PlayScala)

scalaVersion := ScalaVersions.scala2Version
scalaVersion := ScalaVersions.scala3Version

libraryDependencies += guice
2 changes: 1 addition & 1 deletion play-scala/caching-in-play/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version := "1.0-SNAPSHOT"

enablePlugins(PlayScala)

scalaVersion := ScalaVersions.scala2Version
scalaVersion := ScalaVersions.scala3Version

libraryDependencies += guice
libraryDependencies += caffeine
Expand Down
4 changes: 2 additions & 2 deletions play-scala/configuration-access/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ version := "1.0-SNAPSHOT"

enablePlugins(PlayScala)

scalaVersion := ScalaVersions.scala2Version
scalaVersion := ScalaVersions.scala3Version

libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.1" % Test
libraryDependencies += "org.mockito" % "mockito-scala_2.13" % "1.17.31" % Test
libraryDependencies += "org.scalatestplus" %% "mockito-3-4" % "3.2.10.0"

// Adds additional packages into Twirl
//TwirlKeys.templateImports += "com.baeldung.controllers._"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package controllers

import org.mockito.MockitoSugar
import org.scalatestplus.play._
import org.scalatestplus.play.guice._
import play.api.test.Helpers._
import play.api.test._
import services.MyService
import org.scalatestplus.mockito.MockitoSugar

/** Add your spec here. You can mock out a whole application including requests,
* plugins etc.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package services

import java.util.{Calendar, Date, TimeZone}

import org.mockito.MockitoSugar
import org.scalatestplus.play._
import org.scalatestplus.play.guice._
import play.api.test._
import org.scalatestplus.mockito.MockitoSugar

class MyServiceUnitTest
extends PlaySpec
Expand Down
2 changes: 1 addition & 1 deletion play-scala/custom-error-handling/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version := "1.0-SNAPSHOT"

enablePlugins(PlayScala)

scalaVersion := ScalaVersions.scala2Version
scalaVersion := ScalaVersions.scala3Version

libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.1" % Test
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import macwire.services.{
OrderService
}

trait OrderComponents extends {
trait OrderComponents {

lazy val p1: OrderPipelineProcessor = (order: Order) =>
println("Processor 1 processed")
Expand Down
2 changes: 1 addition & 1 deletion play-scala/dependency-injection/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version := "1.0-SNAPSHOT"

enablePlugins(PlayScala)

scalaVersion := ScalaVersions.scala2Version
scalaVersion := ScalaVersions.scala3Version

libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.1" % Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ class ServiceWithRemoteCallUnitTest
.overrides(new MockApiModule, new ServiceModule)
.build()
new App(application) {
val srv = app.injector.instanceOf[ServiceWithRemoteCall]
assert(srv.call() == "Mock remote api call")
override def running() = {
val srv = app.injector.instanceOf[ServiceWithRemoteCall]
assert(srv.call() == "Mock remote api call")
}
}
}

Expand All @@ -30,8 +32,10 @@ class ServiceWithRemoteCallUnitTest
.overrides(new ApiModule, new ServiceModule)
.build()
new App(application) {
val srv = app.injector.instanceOf[ServiceWithRemoteCall]
assert(srv.call() == "Real remote api call")
override def running() = {
val srv = app.injector.instanceOf[ServiceWithRemoteCall]
assert(srv.call() == "Real remote api call")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion play-scala/introduction-to-play/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version := "1.0-SNAPSHOT"

enablePlugins(PlayScala)

scalaVersion := ScalaVersions.scala2Version
scalaVersion := ScalaVersions.scala3Version

libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.1" % Test
2 changes: 1 addition & 1 deletion play-scala/introduction-to-play/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# ~~~~

# An example controller showing a sample home page
GET / controllers.HomeController.index
GET / controllers.HomeController.index()
GET /sum/:first/:second controllers.HomeController.printSum(first: Long, second: Long)

# Map static resources from the /public folder to the /assets URL path
Expand Down
2 changes: 1 addition & 1 deletion play-scala/play-static-assets/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version := "1.0-SNAPSHOT"
enablePlugins(PlayScala)
enablePlugins(SbtWeb)

scalaVersion := ScalaVersions.scala2Version
scalaVersion := ScalaVersions.scala3Version

libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.1" % Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MenuController @Inject() (
cc: ControllerComponents
) extends AbstractController(cc) {

def availableProducts = Action { implicit request =>
def availableProducts = Action {
val products = List(
Product("coffee", 8.99, true),
Product("cake", 12.00, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ case class Article(title: String, url: String)
class ViewTemplateController @Inject() (cc: ControllerComponents)
extends AbstractController(cc) {

def index = Action { implicit request =>
def index = Action {
val articles = List(
(
"Introduction to Play Framework",
Expand All @@ -22,7 +22,7 @@ class ViewTemplateController @Inject() (cc: ControllerComponents)
Ok(views.html.Baeldung.index(articles))
}

def withClass = Action { implicit request =>
def withClass = Action {
val articles = List(
Article(
"Introduction to Play Framework",
Expand Down
2 changes: 1 addition & 1 deletion play-scala/play-templates/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version := "1.0-SNAPSHOT"

enablePlugins(PlayScala)

scalaVersion := ScalaVersions.scala2Version
scalaVersion := ScalaVersions.scala3Version

libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.1" % Test
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ class MenuControllerUnitTest

val controller = new MenuController(template, stubControllerComponents())

val result = controller
.availableProducts()
val result = controller.availableProducts
.apply(
FakeRequest(GET, "/menu")
)

contentAsString(result) must include("coffee 8,99 €")
contentAsString(result) `must` include("coffee 8,99 €")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class ViewTemplateControllerUnitTest

"should return two articles" in {
val controller = new ViewTemplateController(stubControllerComponents())
val result = controller
.index()
val result = controller.index
.apply(
FakeRequest(GET, "/template")
)
Expand All @@ -27,8 +26,7 @@ class ViewTemplateControllerUnitTest

"with_class function returns the same two articles" in {
val controller = new ViewTemplateController(stubControllerComponents())
val result = controller
.withClass()
val result = controller.withClass
.apply(
FakeRequest(GET, "/withclass")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class TodoListController @Inject() (
}

// curl -v -d '{"description": "some new item"}' -H 'Content-Type: application/json' -X POST localhost:9000/todo
def addNewItem() = Action { implicit request =>
def addNewItem(): Action[AnyContent] = Action { request =>
val content = request.body
val jsonObject = content.asJson

Expand Down
2 changes: 1 addition & 1 deletion play-scala/rest-api/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version := "1.0-SNAPSHOT"

enablePlugins(PlayScala)

scalaVersion := ScalaVersions.scala2Version
scalaVersion := ScalaVersions.scala3Version

libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.1" % Test
Expand Down
6 changes: 3 additions & 3 deletions play-scala/rest-api/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

# Map static resources from the /public folder to the /assets URL path

GET /todo controllers.TodoListController.getAll
GET /todo controllers.TodoListController.getAll()
GET /todo/:itemId controllers.TodoListController.getById(itemId: Long)
PUT /todo/done/:itemId controllers.TodoListController.markAsDone(itemId: Long)
DELETE /todo/done controllers.TodoListController.deleteAllDone
POST /todo controllers.TodoListController.addNewItem
DELETE /todo/done controllers.TodoListController.deleteAllDone()
POST /todo controllers.TodoListController.addNewItem()
2 changes: 1 addition & 1 deletion sbt-standalone/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name := "SCALA-156"

version := "0.1"

scalaVersion := "2.13.6"
scalaVersion := "3.4.1"

// Setting target for Scala
scalacOptions += "-target:17"
Expand Down
2 changes: 1 addition & 1 deletion sbt-standalone/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.9.7
sbt.version=1.10.0
2 changes: 1 addition & 1 deletion scala-graalvm/scala-graalvm-http-app/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ organization := "com.baeldung"

version := "1.0-SNAPSHOT"

scalaVersion := "2.13.14"
scalaVersion := "3.4.1"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.8.0",
"com.typesafe.akka" %% "akka-stream" % "2.8.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.7.2
sbt.version=1.10.0
2 changes: 1 addition & 1 deletion scala-sbt/intro-to-sbt/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Dependencies._

ThisBuild / scalaVersion := "2.13.14"
ThisBuild / scalaVersion := "3.4.1"
ThisBuild / version := "0.1.0-SNAPSHOT"

lazy val printHello = taskKey[Unit]("prints hello")
Expand Down
2 changes: 1 addition & 1 deletion scala-sbt/intro-to-sbt/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.6.2
sbt.version=1.10.0