-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
210 additions
and
8 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/no/digdir/servicecatalog/controller/ServiceCountController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package no.digdir.servicecatalog.controller | ||
|
||
import no.digdir.servicecatalog.model.* | ||
import no.digdir.servicecatalog.security.EndpointPermissions | ||
import no.digdir.servicecatalog.service.CountService | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.MediaType | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal | ||
import org.springframework.security.oauth2.jwt.Jwt | ||
import org.springframework.stereotype.Controller | ||
import org.springframework.web.bind.annotation.CrossOrigin | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
|
||
@Controller | ||
@CrossOrigin | ||
@RequestMapping(value = ["/internal/catalogs/count"]) | ||
class CatalogCountController( | ||
private val endpointPermissions: EndpointPermissions, | ||
private val countService: CountService | ||
) { | ||
|
||
@GetMapping(produces = [MediaType.APPLICATION_JSON_VALUE]) | ||
fun getServiceCountsForPermittedCatalogs( | ||
@AuthenticationPrincipal jwt: Jwt | ||
): ResponseEntity<List<ServiceCount>> { | ||
return when { | ||
endpointPermissions.hasSysAdminPermission(jwt) -> { | ||
ResponseEntity( | ||
countService.getServiceCountForAllCatalogs(), | ||
HttpStatus.OK | ||
) | ||
} | ||
else -> ResponseEntity( | ||
countService.getServiceCountForListOfCatalogs( | ||
endpointPermissions.getOrgsWithMinimumReadPermission(jwt) | ||
), HttpStatus.OK | ||
) | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/no/digdir/servicecatalog/model/ServiceCount.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package no.digdir.servicecatalog.model | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
import com.fasterxml.jackson.annotation.JsonInclude | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
data class ServiceCount ( | ||
val catalogId: String, | ||
val serviceCount: Int, | ||
val publicServiceCount: Int | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/no/digdir/servicecatalog/service/CountService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package no.digdir.servicecatalog.service | ||
|
||
import no.digdir.servicecatalog.model.ServiceCount | ||
import no.digdir.servicecatalog.mongodb.PublicServiceRepository | ||
import no.digdir.servicecatalog.mongodb.ServiceRepository | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class CountService( | ||
private val serviceRepository: ServiceRepository, | ||
private val publicServiceRepository: PublicServiceRepository, | ||
) { | ||
private fun getAllDistinctCatalogIds(): List<String> { | ||
val serviceCatalogIds = serviceRepository.findAll().map { it.catalogId } | ||
val publicServiceCatalogIds = publicServiceRepository.findAll().map { it.catalogId } | ||
|
||
return (serviceCatalogIds + publicServiceCatalogIds).distinct() | ||
} | ||
|
||
fun getServiceCountForListOfCatalogs(catalogIds: Set<String>): List<ServiceCount> = | ||
catalogIds.map { getServiceCountForCatalog(it) } | ||
|
||
fun getServiceCountForAllCatalogs(): List<ServiceCount> = | ||
getAllDistinctCatalogIds().map { getServiceCountForCatalog(it) } | ||
|
||
private fun getServiceCountForCatalog(catalogId: String): ServiceCount = | ||
ServiceCount( | ||
catalogId = catalogId, | ||
serviceCount = serviceRepository.getByCatalogId(catalogId) | ||
.distinctBy { it.id } | ||
.size, | ||
publicServiceCount = publicServiceRepository.getByCatalogId(catalogId) | ||
.distinctBy { it.id } | ||
.size, | ||
) | ||
} |
89 changes: 89 additions & 0 deletions
89
src/test/kotlin/no/digdir/servicecatalog/integration/ServiceCountTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package no.digdir.servicecatalog.integration | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import no.digdir.servicecatalog.model.ServiceCount | ||
import no.digdir.servicecatalog.utils.ApiTestContext | ||
import no.digdir.servicecatalog.utils.LIST_OF_SERVICE_COUNTS_ROOT | ||
import no.digdir.servicecatalog.utils.SERVICE_COUNT_1 | ||
import no.digdir.servicecatalog.utils.apiAuthorizedRequest | ||
import no.digdir.servicecatalog.utils.jwt.Access | ||
import no.digdir.servicecatalog.utils.jwt.JwtToken | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.test.context.ContextConfiguration | ||
|
||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
@SpringBootTest( | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, | ||
properties = ["spring.profiles.active=test"] | ||
) | ||
@ContextConfiguration(initializers = [ApiTestContext.Initializer::class]) | ||
@Tag("integration") | ||
class ServiceCountTests : ApiTestContext() { | ||
private val mapper = jacksonObjectMapper() | ||
val path = "/internal/catalogs/count" | ||
|
||
@Test | ||
fun `get service counts when sysAdmin`() { | ||
|
||
val response = apiAuthorizedRequest( | ||
path, | ||
port, | ||
null, | ||
JwtToken(Access.ROOT).toString(), | ||
HttpMethod.GET | ||
) | ||
Assertions.assertEquals(HttpStatus.OK.value(), response["status"]) | ||
|
||
val result: List<ServiceCount> = mapper.readValue(response["body"] as String) | ||
Assertions.assertEquals(LIST_OF_SERVICE_COUNTS_ROOT, result) | ||
} | ||
|
||
@Test | ||
fun `get service counts with read permission`() { | ||
val response = apiAuthorizedRequest( | ||
path, | ||
port, | ||
null, | ||
JwtToken(Access.ORG_READ).toString(), | ||
HttpMethod.GET | ||
) | ||
Assertions.assertEquals(HttpStatus.OK.value(), response["status"]) | ||
|
||
val result: List<ServiceCount> = mapper.readValue(response["body"] as String) | ||
Assertions.assertEquals(listOf(SERVICE_COUNT_1), result) | ||
} | ||
|
||
@Test | ||
fun `get service counts with write permission`() { | ||
val response = apiAuthorizedRequest( | ||
path, | ||
port, | ||
null, | ||
JwtToken(Access.ORG_WRITE).toString(), | ||
HttpMethod.GET | ||
) | ||
Assertions.assertEquals(HttpStatus.OK.value(), response["status"]) | ||
|
||
val result: List<ServiceCount> = mapper.readValue(response["body"] as String) | ||
Assertions.assertEquals(listOf(SERVICE_COUNT_1), result) | ||
} | ||
|
||
@Test | ||
fun `unauthorized when missing token`() { | ||
val response = apiAuthorizedRequest( | ||
path, | ||
port, | ||
null, | ||
null, | ||
HttpMethod.GET) | ||
Assertions.assertEquals(HttpStatus.UNAUTHORIZED.value(), response["status"]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters