aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/services/CategoryParameterServiceWithRolesTest.kt
blob: 2582012d958c0c78863f5efcf9826703c3bcdaea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package org.onap.vid.services

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import net.javacrumbs.jsonunit.JsonMatchers.jsonEquals
import net.javacrumbs.jsonunit.JsonMatchers.jsonPartEquals
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.MatcherAssert.assertThat
import org.intellij.lang.annotations.Language
import org.mockito.ArgumentMatchers.any
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.mock
import org.mockito.invocation.InvocationOnMock
import org.onap.vid.category.CategoryParameterOptionRep
import org.onap.vid.category.CategoryParametersResponse
import org.onap.vid.roles.RoleProvider
import org.onap.vid.roles.RoleValidator
import org.onap.vid.services.CategoryParameterServiceWithRoles.OwningEntityOptionRep
import org.onap.vid.testUtils.TestUtils
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test
import org.togglz.core.manager.FeatureManager
import javax.servlet.http.HttpServletRequest
import org.mockito.Mockito.`when` as _when

class CategoryParameterServiceWithRolesTest {

    @Mock lateinit var categoryParameterService: CategoryParameterService
    @Mock lateinit var featureManager: FeatureManager
    @Mock lateinit var roleProvider: RoleProvider
    @Mock lateinit var request: HttpServletRequest

    private lateinit var alwaysTrueRoles: RoleValidator
    private lateinit var alwaysFalseRoles: RoleValidator

    @InjectMocks
    lateinit var categoryParameterServiceWithRoles: CategoryParameterServiceWithRoles;

    @BeforeMethod
    fun initMocks() {
        TestUtils.initMockitoMocks(this)

        alwaysTrueRoles = mock(RoleValidator::class.java, Mockito.withSettings().defaultAnswer { o: InvocationOnMock? -> true })
        alwaysFalseRoles = mock(RoleValidator::class.java)
    }

    @Test
    fun `treatPermissions -- given no permissions -- owningEntity lists is empty, the rest left intact`() {
        _when(roleProvider.getUserRolesValidator(any())).thenReturn(alwaysFalseRoles)

        assertThat(
                categoryParameterServiceWithRoles.treatPermissions(categoryParametersResponse),
                allOf(
                    jsonPartEquals("categoryParameters.owningEntity", emptyList<Any>()),
                    jsonEquals<CategoryParametersResponse>(categoryParametersResponse)
                            .whenIgnoringPaths("categoryParameters.owningEntity")
                )
        )
    }

    @Test
    fun `treatPermissions -- given all permissions -- response left intact`() {
        _when(roleProvider.getUserRolesValidator(any())).thenReturn(alwaysTrueRoles)

        assertThat(
                categoryParameterServiceWithRoles.treatPermissions(categoryParametersResponse),
                jsonEquals(categoryParametersResponse)
        )
    }

    @Test
    fun `treatPermissions -- given permission to WayneHolland -- only one owningEntity WayneHolland is left`() {
        val wayneHolland = CategoryParameterOptionRep("d61e6f2d-12fa-4cc2-91df-7c244011d6fc", "WayneHolland")

        val roleValidatorForWayneHolland = mock(RoleValidator::class.java)
        _when(roleValidatorForWayneHolland.isServicePermitted(OwningEntityOptionRep(wayneHolland))).thenReturn(true)

        _when(roleProvider.getUserRolesValidator(any())).thenReturn(roleValidatorForWayneHolland)

        assertThat(
                categoryParameterServiceWithRoles.treatPermissions(categoryParametersResponse),
                jsonPartEquals("categoryParameters.owningEntity", listOf(wayneHolland))
        )
    }



    @Language("JSON") val categoryParametersResponse: CategoryParametersResponse =
        jacksonObjectMapper().readValue("""
            {
              "categoryParameters": {
                "lineOfBusiness": [
                  { "id": "ONAP", "name": "ONAP" },
                  { "id": "zzz1", "name": "zzz1" }
                ],
                "owningEntity": [
                  { "id": "b1a9a80e-71b8-4176-9ac6-d265bf30e9d9", "name": "Melissa" },
                  { "id": "d61e6f2d-12fa-4cc2-91df-7c244011d6fc", "name": "WayneHolland" }
                ],
                "project": [
                  { "id": "WATKINS", "name": "WATKINS" },
                  { "id": "yyy1", "name": "yyy1" }
                ],
                "platform": [
                  { "id": "platform", "name": "platform" },
                  { "id": "xxx1", "name": "xxx1" }
                ]
              }
            }""")


}