29
29
import io .kubernetes .client .util .ModelMapper ;
30
30
import java .io .File ;
31
31
import java .io .IOException ;
32
+ import java .io .InputStream ;
33
+ import java .nio .charset .StandardCharsets ;
32
34
import java .nio .file .Files ;
33
35
import java .nio .file .Paths ;
36
+ import java .util .Properties ;
34
37
35
38
import org .junit .Before ;
36
39
import org .junit .Rule ;
@@ -40,51 +43,92 @@ public class KubectlDeleteTest {
40
43
41
44
private ApiClient apiClient ;
42
45
43
- private static final String DISCOVERY_API =
44
- new File (KubectlDeleteTest .class .getClassLoader ().getResource ("discovery-api.json" ).getPath ())
45
- .toString ();
46
-
47
- private static final String DISCOVERY_APIV1 =
48
- new File (
49
- KubectlDeleteTest .class
50
- .getClassLoader ()
51
- .getResource ("discovery-api-v1.json" )
52
- .getPath ())
53
- .toString ();
54
-
55
-
56
- private static final String ADD_JOB =
57
- new File (
58
- KubectlDeleteTest .class
59
- .getClassLoader ()
60
- .getResource ("deleted-add-job.json" )
61
- .getPath ())
62
- .toString ();
63
- private static final String GET_BATCH =
64
- new File (
65
- KubectlDeleteTest .class
66
- .getClassLoader ()
67
- .getResource ("deleted-get-batch.json" )
68
- .getPath ())
69
- .toString ();
70
- private static final String DELETED_FIRST =
71
- new File (
72
- KubectlDeleteTest .class
73
- .getClassLoader ()
74
- .getResource ("deleted-success.json" )
75
- .getPath ())
76
- .toString ();
77
- private static final String DELETED_SECOND =
78
- new File (
79
- KubectlDeleteTest .class
80
- .getClassLoader ()
81
- .getResource ("deleted-not-found.json" )
82
- .getPath ())
83
- .toString ();
84
-
85
- private static final String DISCOVERY_APIS =
86
- new File (KubectlDeleteTest .class .getClassLoader ().getResource ("discovery-apis.json" ).getPath ())
87
- .toString ();
46
+ private static final byte [] DISCOVERY_API ;
47
+
48
+ static {
49
+ try {
50
+ DISCOVERY_API = KubectlDeleteTest .class
51
+ .getClassLoader ()
52
+ .getResourceAsStream ("discovery-api.json" )
53
+ .readAllBytes ();
54
+ } catch (IOException e ) {
55
+ throw new RuntimeException ("Failed to load resource" , e );
56
+ }
57
+ }
58
+
59
+ private static final byte [] DISCOVERY_APIV1 ;
60
+
61
+ static {
62
+ try {
63
+ DISCOVERY_APIV1 = KubectlDeleteTest .class
64
+ .getClassLoader ()
65
+ .getResourceAsStream ("discovery-api-v1.json" )
66
+ .readAllBytes ();
67
+ } catch (IOException e ) {
68
+ throw new RuntimeException (e );
69
+ }
70
+ }
71
+
72
+
73
+ private static final byte [] ADD_JOB ;
74
+
75
+ static {
76
+ try {
77
+ ADD_JOB = KubectlDeleteTest .class
78
+ .getClassLoader ()
79
+ .getResourceAsStream ("deleted-add-job.json" ).readAllBytes ();
80
+ } catch (IOException e ) {
81
+ throw new RuntimeException (e );
82
+ }
83
+ }
84
+
85
+ private static final byte [] GET_BATCH ;
86
+
87
+ static {
88
+ try {
89
+ GET_BATCH = KubectlDeleteTest .class
90
+ .getClassLoader ()
91
+ .getResourceAsStream ("deleted-get-batch.json" ).readAllBytes ();
92
+ } catch (IOException e ) {
93
+ throw new RuntimeException (e );
94
+ }
95
+ }
96
+
97
+ private static final byte [] DELETED_FIRST ;
98
+
99
+ static {
100
+ try {
101
+ DELETED_FIRST = KubectlDeleteTest .class
102
+ .getClassLoader ()
103
+ .getResourceAsStream ("deleted-success.json" ).readAllBytes ();
104
+ } catch (IOException e ) {
105
+ throw new RuntimeException (e );
106
+ }
107
+ }
108
+
109
+ private static final byte [] DELETED_SECOND ;
110
+
111
+ static {
112
+ try {
113
+ DELETED_SECOND = KubectlDeleteTest .class
114
+ .getClassLoader ()
115
+ .getResourceAsStream ("deleted-not-found.json" ).readAllBytes ();
116
+ } catch (IOException e ) {
117
+ throw new RuntimeException (e );
118
+ }
119
+ }
120
+
121
+ private static final byte [] DISCOVERY_APIS ;
122
+
123
+ static {
124
+ try {
125
+ DISCOVERY_APIS = KubectlDeleteTest .class
126
+ .getClassLoader ()
127
+ .getResourceAsStream ("discovery-apis.json" ).readAllBytes ();
128
+ } catch (IOException e ) {
129
+ throw new RuntimeException (e );
130
+ }
131
+ }
88
132
89
133
@ Rule public WireMockRule wireMockRule = new WireMockRule (wireMockConfig ().dynamicPort ());
90
134
@@ -100,14 +144,14 @@ public void testKubectlDelete() throws KubectlException, IOException, ApiExcepti
100
144
.willReturn (
101
145
aResponse ()
102
146
.withStatus (201 )
103
- .withBody (new String ( Files . readAllBytes ( Paths . get ( ADD_JOB ))) )));
147
+ .withBody (ADD_JOB )));
104
148
wireMockRule .stubFor (
105
149
delete (urlPathEqualTo ("/apis/batch%2Fv1/batch%2Fv1/namespaces/foo/jobs/bar" ))
106
150
.inScenario ("JobDeletionScenario" )
107
151
.whenScenarioStateIs (Scenario .STARTED )
108
152
.willReturn (aResponse ()
109
153
.withStatus (200 )
110
- .withBody (new String ( Files . readAllBytes ( Paths . get ( DELETED_FIRST ))) )
154
+ .withBody (DELETED_FIRST )
111
155
)
112
156
.willSetStateTo ("SecondCall" )
113
157
);
@@ -118,34 +162,35 @@ public void testKubectlDelete() throws KubectlException, IOException, ApiExcepti
118
162
.whenScenarioStateIs ("SecondCall" )
119
163
.willReturn (aResponse ()
120
164
.withStatus (404 )
121
- .withBody (new String ( Files . readAllBytes ( Paths . get ( DELETED_SECOND ))) )
165
+ .withBody (DELETED_SECOND )
122
166
)
123
167
);
124
168
169
+
125
170
wireMockRule .stubFor (
126
171
get (urlPathEqualTo ("/api" ))
127
172
.willReturn (
128
173
aResponse ()
129
174
.withStatus (200 )
130
- .withBody (new String ( Files . readAllBytes ( Paths . get ( DISCOVERY_API ))) )));
175
+ .withBody (DISCOVERY_API )));
131
176
wireMockRule .stubFor (
132
177
get (urlPathEqualTo ("/apis" ))
133
178
.willReturn (
134
179
aResponse ()
135
180
.withStatus (200 )
136
- .withBody (new String ( Files . readAllBytes ( Paths . get ( DISCOVERY_APIS ))) )));
181
+ .withBody (DISCOVERY_APIS )));
137
182
wireMockRule .stubFor (
138
183
get (urlPathEqualTo ("/api/v1" ))
139
184
.willReturn (
140
185
aResponse ()
141
186
.withStatus (200 )
142
- .withBody (new String ( Files . readAllBytes ( Paths . get ( DISCOVERY_APIV1 ))) )));
187
+ .withBody (DISCOVERY_APIV1 )));
143
188
wireMockRule .stubFor (
144
189
get (urlPathEqualTo ("/apis/batch/v1/" ))
145
190
.willReturn (
146
191
aResponse ()
147
192
.withStatus (200 )
148
- .withBody (new String ( Files . readAllBytes ( Paths . get ( GET_BATCH ))) )));
193
+ .withBody (GET_BATCH )));
149
194
150
195
V1JobSpec v1JobSpec = new V1JobSpec ()
151
196
.template (new V1PodTemplateSpec ()
0 commit comments