12
12
*/
13
13
package io .kubernetes .client .util ;
14
14
15
- import static org .junit .Assert .*;
16
-
15
+ import io .kubernetes .client .custom .IntOrString ;
17
16
import io .kubernetes .client .models .V1ObjectMeta ;
17
+ import io .kubernetes .client .models .V1Service ;
18
+ import io .kubernetes .client .models .V1ServicePort ;
19
+ import org .junit .Test ;
20
+
18
21
import java .io .StringReader ;
19
22
import java .lang .reflect .Method ;
20
- import org .junit .Test ;
23
+
24
+ import static org .junit .Assert .*;
21
25
22
26
public class YamlTest {
23
27
@ Test
24
28
public void testLoad () {
25
- String [] kinds = new String [] {"Pod" , "Deployment" , "ClusterRole" , "APIService" , "Scale" };
29
+ String [] kinds = new String []{"Pod" , "Deployment" , "ClusterRole" , "APIService" , "Scale" };
26
30
String [] apiVersions =
27
- new String [] {"v1" , "v1beta2" , "v1alpha1" , "v1beta1" , "extensions/v1beta1" };
31
+ new String []{"v1" , "v1beta2" , "v1alpha1" , "v1beta1" , "extensions/v1beta1" };
28
32
String [] classNames =
29
- new String [] {
30
- "V1Pod" ,
31
- "V1beta2Deployment" ,
32
- "V1alpha1ClusterRole" ,
33
- "V1beta1APIService" ,
34
- "ExtensionsV1beta1Scale"
35
- };
33
+ new String []{
34
+ "V1Pod" ,
35
+ "V1beta2Deployment" ,
36
+ "V1alpha1ClusterRole" ,
37
+ "V1beta1APIService" ,
38
+ "ExtensionsV1beta1Scale"
39
+ };
36
40
for (int i = 0 ; i < kinds .length ; i ++) {
37
41
String kind = kinds [i ];
38
42
String className = classNames [i ];
39
43
try {
40
44
String input =
41
- "kind: "
42
- + kind
43
- + "\n "
44
- + "apiVersion: "
45
- + apiVersions [i ]
46
- + "\n "
47
- + "metadata:\n "
48
- + " name: foo" ;
45
+ "kind: "
46
+ + kind
47
+ + "\n "
48
+ + "apiVersion: "
49
+ + apiVersions [i ]
50
+ + "\n "
51
+ + "metadata:\n "
52
+ + " name: foo" ;
49
53
Object obj = Yaml .load (new StringReader (input ));
50
54
Method m = obj .getClass ().getMethod ("getMetadata" );
51
55
V1ObjectMeta metadata = (V1ObjectMeta ) m .invoke (obj );
@@ -57,4 +61,21 @@ public void testLoad() {
57
61
}
58
62
}
59
63
}
64
+
65
+ @ Test
66
+ public void testLoadIntOrString () {
67
+ try {
68
+ String strInput = "targetPort: test" ;
69
+ String intInput = "targetPort: 1" ;
70
+
71
+ V1ServicePort stringPort = Yaml .loadAs (strInput , V1ServicePort .class );
72
+ V1ServicePort intPort = Yaml .loadAs (intInput , V1ServicePort .class );
73
+
74
+ assertFalse ("Target port for 'stringPort' was parsed to an integer, string expected." , stringPort .getTargetPort ().isInteger ());
75
+ assertTrue ("Target port for 'intPort' was parsed to a string, integer expected." , intPort .getTargetPort ().isInteger ());
76
+
77
+ } catch (Exception ex ) {
78
+ assertNull ("Unexpected exception: " + ex .toString (), ex );
79
+ }
80
+ }
60
81
}
0 commit comments