From 2322362b44880ef2474778eade471f28cc1886d2 Mon Sep 17 00:00:00 2001 From: bayusuryadana Date: Thu, 17 Aug 2023 02:08:55 +0800 Subject: [PATCH] fix and add UT --- .../com/seanmcapp/service/MamenService.scala | 2 + .../seanmcapp/external/GoogleClientSpec.scala | 85 +++++++++++++++++++ .../seanmcapp/service/MamenServiceSpec.scala | 20 +++-- 3 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 src/test/scala/com/seanmcapp/external/GoogleClientSpec.scala diff --git a/src/main/scala/com/seanmcapp/service/MamenService.scala b/src/main/scala/com/seanmcapp/service/MamenService.scala index 815b405..ef0b348 100644 --- a/src/main/scala/com/seanmcapp/service/MamenService.scala +++ b/src/main/scala/com/seanmcapp/service/MamenService.scala @@ -8,6 +8,7 @@ import scala.concurrent.Future class MamenService(stallRepo: StallRepo, googleClient: GoogleClient) { + //$COVERAGE-OFF$ def fetch(): Future[Seq[Int]] = { for { stalls <- stallRepo.getAll @@ -19,6 +20,7 @@ class MamenService(stallRepo: StallRepo, googleClient: GoogleClient) { updatedStalls <- stallRepo.update(fetchedStalls) } yield updatedStalls } + //$COVERAGE-ON$ def search(request: MamenRequest): Future[Seq[Stall]] = { for { diff --git a/src/test/scala/com/seanmcapp/external/GoogleClientSpec.scala b/src/test/scala/com/seanmcapp/external/GoogleClientSpec.scala new file mode 100644 index 0000000..2989db5 --- /dev/null +++ b/src/test/scala/com/seanmcapp/external/GoogleClientSpec.scala @@ -0,0 +1,85 @@ +package com.seanmcapp.external + +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito +import org.mockito.Mockito.when +import org.scalatest.wordspec.AnyWordSpec +import org.scalatest.matchers.should.Matchers + +class GoogleClientSpec extends AnyWordSpec with Matchers { + + "fetchLatLng" in { + val http = Mockito.mock(classOf[HttpRequestClient]) + val googleClient = new GoogleClient(http) + val response = + s""" + |{ + | "results" : [ + | { + | "address_components" : [ + | { + | "long_name" : "M9WC+GJ", + | "short_name" : "M9WC+GJ", + | "types" : [ "plus_code" ] + | }, + | { + | "long_name" : "Benteng Pasar Atas", + | "short_name" : "Benteng Pasar Atas", + | "types" : [ "administrative_area_level_4", "political" ] + | }, + | { + | "long_name" : "Guguk Panjang", + | "short_name" : "Guguk Panjang", + | "types" : [ "administrative_area_level_3", "political" ] + | }, + | { + | "long_name" : "Bukittinggi City", + | "short_name" : "Bukittinggi City", + | "types" : [ "administrative_area_level_2", "political" ] + | }, + | { + | "long_name" : "West Sumatra", + | "short_name" : "West Sumatra", + | "types" : [ "administrative_area_level_1", "political" ] + | }, + | { + | "long_name" : "Indonesia", + | "short_name" : "ID", + | "types" : [ "country", "political" ] + | } + | ], + | "formatted_address" : "M9WC+GJ, Benteng Pasar Atas, Guguk Panjang, Bukittinggi City, West Sumatra, Indonesia", + | "geometry" : { + | "location" : { + | "lat" : -0.3036875, + | "lng" : 100.3715625 + | }, + | "location_type" : "GEOMETRIC_CENTER", + | "viewport" : { + | "northeast" : { + | "lat" : -0.302338519708498, + | "lng" : 100.3729114802915 + | }, + | "southwest" : { + | "lat" : -0.305036480291502, + | "lng" : 100.3702135197085 + | } + | } + | }, + | "place_id" : "ElVNOVdDK0dKLCBCZW50ZW5nIFBhc2FyIEF0YXMsIEd1Z3VrIFBhbmphbmcsIEJ1a2l0dGluZ2dpIENpdHksIFdlc3QgU3VtYXRyYSwgSW5kb25lc2lhIiY6JAoKDTWp0f8VKXzTOxAKGhQKEgmZvt0bozjVLxECR9Uuay8ybQ", + | "plus_code" : { + | "compound_code" : "M9WC+GJ Benteng Pasar Atas, Bukittinggi City, West Sumatra, Indonesia", + | "global_code" : "6PF2M9WC+GJ" + | }, + | "types" : [ "street_address" ] + | } + | ], + | "status" : "OK" + |} + |""".stripMargin + when(http.sendGetRequest(any(), any())).thenReturn(response) + val expected = (Some(-0.3036875),Some(100.3715625)) + googleClient.fetchLatLng("") shouldBe expected + } + +} diff --git a/src/test/scala/com/seanmcapp/service/MamenServiceSpec.scala b/src/test/scala/com/seanmcapp/service/MamenServiceSpec.scala index 8b97dcc..8ff22a9 100644 --- a/src/test/scala/com/seanmcapp/service/MamenServiceSpec.scala +++ b/src/test/scala/com/seanmcapp/service/MamenServiceSpec.scala @@ -3,24 +3,30 @@ package com.seanmcapp.service import com.seanmcapp.external.{GeoFilter, LatLng, MamenFilter, MamenRequest} import com.seanmcapp.repository.seanmcmamen.{Cities, Stall, StallRepo} import org.mockito.Mockito -import org.mockito.Mockito.when +import org.mockito.Mockito.{times, verify, when} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AsyncWordSpec -import scala.concurrent.Future +import scala.concurrent.{Await, Future} +import com.seanmcapp.external.GoogleClient +import org.scalatest.time.Second + +import scala.concurrent.duration.Duration class MamenServiceSpec extends AsyncWordSpec with Matchers { val stallRepo = Mockito.mock(classOf[StallRepo]) + val googleClient = Mockito.mock(classOf[GoogleClient]) + val stallRepoData = Seq( - Stall(1, "Ayam Suharti", "7JPF+GX2, Ps. Gn. Sitoli, Kec. Gunungsitoli, Kota Gunungsitoli, Sumatera Utara", Cities.Jakarta, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-6.23), Some(106.67), None), - Stall(2, "Rm. Sederhana", "MRQJ+WR9, Aek Tolang, Kec. Pandan, Kabupaten Tapanuli Tengah, Sumatera Utara 22611", Cities.Jakarta, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-6.23), Some(106.67), None), - Stall(3, "Warteg Bahari", "39VX+XG Gunung Sarik, Padang City, West Sumatra, Indonesia", Cities.Surabaya, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-8.23), Some(110.67), None), - Stall(4, "Sate Madura Pak Kumis", "PJM8+72 Limbukan, Payakumbuh City, West Sumatra, Indonesia", Cities.Surabaya, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-8.23), Some(110.67), None) + Stall(1, "Ayam Suharti", "7JPF+GX2, Ps. Gn. Sitoli, Kec. Gunungsitoli, Kota Gunungsitoli, Sumatera Utara", Cities.Jakarta, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-6.23), Some(106.67)), + Stall(2, "Rm. Sederhana", "MRQJ+WR9, Aek Tolang, Kec. Pandan, Kabupaten Tapanuli Tengah, Sumatera Utara 22611", Cities.Jakarta, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-6.23), Some(106.67)), + Stall(3, "Warteg Bahari", "39VX+XG Gunung Sarik, Padang City, West Sumatra, Indonesia", Cities.Surabaya, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-8.23), Some(110.67)), + Stall(4, "Sate Madura Pak Kumis", "PJM8+72 Limbukan, Payakumbuh City, West Sumatra, Indonesia", Cities.Surabaya, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-8.23), Some(110.67)) ) when(stallRepo.getAll).thenReturn(Future.successful(stallRepoData)) - val mamenService = new MamenService(stallRepo) + val mamenService = new MamenService(stallRepo, googleClient) val mamenRequest = MamenRequest(MamenFilter()) "search by name should return correct result" in {