Skip to content

Commit fc32b50

Browse files
committed
switched file usage to getResourceAsStream
1 parent 12d1b55 commit fc32b50

File tree

1 file changed

+97
-52
lines changed

1 file changed

+97
-52
lines changed

extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlDeleteTest.java

Lines changed: 97 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929
import io.kubernetes.client.util.ModelMapper;
3030
import java.io.File;
3131
import java.io.IOException;
32+
import java.io.InputStream;
33+
import java.nio.charset.StandardCharsets;
3234
import java.nio.file.Files;
3335
import java.nio.file.Paths;
36+
import java.util.Properties;
3437

3538
import org.junit.Before;
3639
import org.junit.Rule;
@@ -40,51 +43,92 @@ public class KubectlDeleteTest {
4043

4144
private ApiClient apiClient;
4245

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+
}
88132

89133
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
90134

@@ -100,14 +144,14 @@ public void testKubectlDelete() throws KubectlException, IOException, ApiExcepti
100144
.willReturn(
101145
aResponse()
102146
.withStatus(201)
103-
.withBody(new String(Files.readAllBytes(Paths.get(ADD_JOB))))));
147+
.withBody(ADD_JOB)));
104148
wireMockRule.stubFor(
105149
delete(urlPathEqualTo("/apis/batch%2Fv1/batch%2Fv1/namespaces/foo/jobs/bar"))
106150
.inScenario("JobDeletionScenario")
107151
.whenScenarioStateIs(Scenario.STARTED)
108152
.willReturn(aResponse()
109153
.withStatus(200)
110-
.withBody(new String(Files.readAllBytes(Paths.get(DELETED_FIRST))))
154+
.withBody(DELETED_FIRST)
111155
)
112156
.willSetStateTo("SecondCall")
113157
);
@@ -118,34 +162,35 @@ public void testKubectlDelete() throws KubectlException, IOException, ApiExcepti
118162
.whenScenarioStateIs("SecondCall")
119163
.willReturn(aResponse()
120164
.withStatus(404)
121-
.withBody(new String(Files.readAllBytes(Paths.get(DELETED_SECOND))))
165+
.withBody(DELETED_SECOND)
122166
)
123167
);
124168

169+
125170
wireMockRule.stubFor(
126171
get(urlPathEqualTo("/api"))
127172
.willReturn(
128173
aResponse()
129174
.withStatus(200)
130-
.withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_API))))));
175+
.withBody(DISCOVERY_API)));
131176
wireMockRule.stubFor(
132177
get(urlPathEqualTo("/apis"))
133178
.willReturn(
134179
aResponse()
135180
.withStatus(200)
136-
.withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIS))))));
181+
.withBody(DISCOVERY_APIS)));
137182
wireMockRule.stubFor(
138183
get(urlPathEqualTo("/api/v1"))
139184
.willReturn(
140185
aResponse()
141186
.withStatus(200)
142-
.withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIV1))))));
187+
.withBody(DISCOVERY_APIV1)));
143188
wireMockRule.stubFor(
144189
get(urlPathEqualTo("/apis/batch/v1/"))
145190
.willReturn(
146191
aResponse()
147192
.withStatus(200)
148-
.withBody(new String(Files.readAllBytes(Paths.get(GET_BATCH))))));
193+
.withBody(GET_BATCH)));
149194

150195
V1JobSpec v1JobSpec = new V1JobSpec()
151196
.template(new V1PodTemplateSpec()

0 commit comments

Comments
 (0)