2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
package software.aws.toolkits.jetbrains.services.codemodernizer
5
+ import io.mockk.every
6
+ import io.mockk.just
7
+ import io.mockk.mockkStatic
8
+ import io.mockk.runs
5
9
import kotlinx.coroutines.runBlocking
6
10
import org.assertj.core.api.Assertions.assertThat
7
11
import org.junit.Before
@@ -11,10 +15,13 @@ import org.mockito.kotlin.any
11
15
import org.mockito.kotlin.times
12
16
import org.mockito.kotlin.verify
13
17
import org.mockito.kotlin.whenever
18
+ import software.amazon.awssdk.services.codewhispererruntime.model.AccessDeniedException
14
19
import software.amazon.awssdk.services.codewhispererruntime.model.TransformationProgressUpdate
15
20
import software.amazon.awssdk.services.codewhispererruntime.model.TransformationStatus
21
+ import software.amazon.awssdk.services.ssooidc.model.InvalidGrantException
16
22
import software.aws.toolkits.jetbrains.services.codemodernizer.utils.getTableMapping
17
23
import software.aws.toolkits.jetbrains.services.codemodernizer.utils.pollTransformationStatusAndPlan
24
+ import software.aws.toolkits.jetbrains.services.codemodernizer.utils.refreshToken
18
25
import java.util.concurrent.atomic.AtomicBoolean
19
26
20
27
class CodeWhispererCodeModernizerUtilsTest : CodeWhispererCodeModernizerTestBase () {
@@ -57,6 +64,88 @@ class CodeWhispererCodeModernizerUtilsTest : CodeWhispererCodeModernizerTestBase
57
64
assertThat(expected).isEqualTo(mutableList)
58
65
}
59
66
67
+ @Test
68
+ fun `refresh on access denied` () {
69
+ val mockAccessDeniedException = Mockito .mock(AccessDeniedException ::class .java)
70
+
71
+ mockkStatic(::refreshToken)
72
+ every { refreshToken(any()) } just runs
73
+
74
+ Mockito .doThrow(
75
+ mockAccessDeniedException
76
+ ).doReturn(
77
+ exampleGetCodeMigrationResponse,
78
+ exampleGetCodeMigrationResponse.replace(TransformationStatus .STARTED ),
79
+ exampleGetCodeMigrationResponse.replace(TransformationStatus .COMPLETED ), // Should stop before this point
80
+ ).whenever(clientAdaptorSpy).getCodeModernizationJob(any())
81
+
82
+ Mockito .doReturn(exampleGetCodeMigrationPlanResponse)
83
+ .whenever(clientAdaptorSpy).getCodeModernizationPlan(any())
84
+
85
+ val mutableList = mutableListOf<TransformationStatus >()
86
+ runBlocking {
87
+ jobId.pollTransformationStatusAndPlan(
88
+ setOf (TransformationStatus .STARTED ),
89
+ setOf (TransformationStatus .FAILED ),
90
+ clientAdaptorSpy,
91
+ 0 ,
92
+ 0 ,
93
+ AtomicBoolean (false ),
94
+ project
95
+ ) { _, status, _ ->
96
+ mutableList.add(status)
97
+ }
98
+ }
99
+ val expected =
100
+ listOf<TransformationStatus >(
101
+ exampleGetCodeMigrationResponse.transformationJob().status(),
102
+ TransformationStatus .STARTED ,
103
+ )
104
+ assertThat(expected).isEqualTo(mutableList)
105
+ io.mockk.verify { refreshToken(any()) }
106
+ }
107
+
108
+ @Test
109
+ fun `refresh on invalid grant` () {
110
+ val mockInvalidGrantException = Mockito .mock(InvalidGrantException ::class .java)
111
+
112
+ mockkStatic(::refreshToken)
113
+ every { refreshToken(any()) } just runs
114
+
115
+ Mockito .doThrow(
116
+ mockInvalidGrantException
117
+ ).doReturn(
118
+ exampleGetCodeMigrationResponse,
119
+ exampleGetCodeMigrationResponse.replace(TransformationStatus .STARTED ),
120
+ exampleGetCodeMigrationResponse.replace(TransformationStatus .COMPLETED ), // Should stop before this point
121
+ ).whenever(clientAdaptorSpy).getCodeModernizationJob(any())
122
+
123
+ Mockito .doReturn(exampleGetCodeMigrationPlanResponse)
124
+ .whenever(clientAdaptorSpy).getCodeModernizationPlan(any())
125
+
126
+ val mutableList = mutableListOf<TransformationStatus >()
127
+ runBlocking {
128
+ jobId.pollTransformationStatusAndPlan(
129
+ setOf (TransformationStatus .STARTED ),
130
+ setOf (TransformationStatus .FAILED ),
131
+ clientAdaptorSpy,
132
+ 0 ,
133
+ 0 ,
134
+ AtomicBoolean (false ),
135
+ project
136
+ ) { _, status, _ ->
137
+ mutableList.add(status)
138
+ }
139
+ }
140
+ val expected =
141
+ listOf<TransformationStatus >(
142
+ exampleGetCodeMigrationResponse.transformationJob().status(),
143
+ TransformationStatus .STARTED ,
144
+ )
145
+ assertThat(expected).isEqualTo(mutableList)
146
+ io.mockk.verify { refreshToken(any()) }
147
+ }
148
+
60
149
@Test
61
150
fun `stops polling when status transitions to failOn` () {
62
151
Mockito .doReturn(
0 commit comments