1
+ /* ******************************************************************************
2
+ * Copyright (c) 2025 Red Hat, Inc.
3
+ * Distributed under license by Red Hat, Inc. All rights reserved.
4
+ * This program is made available under the terms of the
5
+ * Eclipse Public License v2.0 which accompanies this distribution,
6
+ * and is available at http://www.eclipse.org/legal/epl-v20.html
7
+ *
8
+ * Contributors:
9
+ * Red Hat, Inc. - initial API and implementation
10
+ ******************************************************************************/
11
+ package com.redhat.devtools.intellij.kubernetes.editor
12
+
13
+ import com.intellij.json.JsonFileType
14
+ import com.intellij.openapi.fileTypes.PlainTextFileType
15
+ import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.resource
16
+ import com.redhat.devtools.intellij.kubernetes.model.util.ResourceException
17
+ import io.fabric8.kubernetes.api.model.Pod
18
+ import io.fabric8.kubernetes.client.utils.Serialization
19
+ import org.assertj.core.api.Assertions.assertThat
20
+ import org.assertj.core.api.Assertions.assertThatThrownBy
21
+ import org.jetbrains.yaml.YAMLFileType
22
+ import org.junit.Test
23
+
24
+ class EditorResourceSerializationTest {
25
+
26
+ @Test
27
+ fun `#deserialize return empty list if it is given null yaml` () {
28
+ // given
29
+ // when
30
+ val result = EditorResourceSerialization .deserialize(null , YAMLFileType .YML , " dagobah" )
31
+ // then
32
+ assertThat(result).isEmpty()
33
+ }
34
+
35
+ @Test
36
+ fun `#deserialize returns a list of resources if given multi-resource YAML` () {
37
+ // given
38
+ val yaml = """
39
+ apiVersion: v1
40
+ kind: Pod
41
+ metadata:
42
+ name: yoda
43
+ ---
44
+ apiVersion: v1
45
+ kind: Service
46
+ metadata:
47
+ name: luke
48
+ """ .trimIndent()
49
+ // when
50
+ val result = EditorResourceSerialization .deserialize(yaml, YAMLFileType .YML , " dagobah" )
51
+ // then
52
+ assertThat(result)
53
+ .hasSize(2 )
54
+ .extracting(" kind" )
55
+ .containsExactly(" Pod" , " Service" )
56
+ }
57
+
58
+ @Test
59
+ fun `#deserialize throws if given multiple json resources` () {
60
+ // given
61
+ val json = """
62
+ {"apiVersion": "v1", "kind": "Pod"}
63
+ ---
64
+ {"apiVersion": "v1", "kind": "Service"}
65
+ """ .trimIndent()
66
+
67
+ assertThatThrownBy {
68
+ // when
69
+ EditorResourceSerialization .deserialize(json, JsonFileType .INSTANCE , " test" )
70
+ // then
71
+ }.isInstanceOf(ResourceException ::class .java)
72
+ }
73
+
74
+ @Test
75
+ fun `#deserialize returns a list with a single resource if given valid JSON with a single resource` () {
76
+ // given
77
+ val json = """
78
+ {
79
+ "apiVersion": "v1",
80
+ "kind": "Pod",
81
+ "metadata": {
82
+ "name": "obiwan"
83
+ }
84
+ }
85
+ """ .trimIndent()
86
+ // when
87
+ val result = EditorResourceSerialization .deserialize(json, JsonFileType .INSTANCE , " test" )
88
+ // then
89
+ assertThat(result)
90
+ .singleElement()
91
+ .extracting(" kind" )
92
+ .isEqualTo(" Pod" )
93
+ }
94
+
95
+ @Test
96
+ fun `#deserialize sets the current namespace to the resulting resource if it has no namespace` () {
97
+ // given
98
+ val yaml = """
99
+ apiVersion: v1
100
+ kind: Pod
101
+ metadata:
102
+ name: has-no-namespace
103
+ """ .trimIndent()
104
+ // when
105
+ val result = EditorResourceSerialization .deserialize(yaml, YAMLFileType .YML , " namespace-that-should-be-set" )
106
+ // then
107
+ assertThat(result)
108
+ .first()
109
+ .extracting { it.metadata.namespace }
110
+ .isEqualTo(" namespace-that-should-be-set" )
111
+ }
112
+
113
+ @Test
114
+ fun `#deserialize does not change namespace in the resulting resource if it has a namespace` () {
115
+ // given
116
+ val yaml = """
117
+ apiVersion: v1
118
+ kind: Pod
119
+ metadata:
120
+ name: yoda
121
+ namespace: has-a-namespace
122
+ """ .trimIndent()
123
+ // when
124
+ val result = EditorResourceSerialization .deserialize(yaml, YAMLFileType .YML , null ) // no namespace provided
125
+ // then
126
+ assertThat(result)
127
+ .first()
128
+ .extracting { it.metadata.namespace }
129
+ .isEqualTo(" has-a-namespace" )
130
+ }
131
+
132
+ @Test
133
+ fun `#deserialize throws if given invalid yaml` () {
134
+ // given
135
+ val invalidYaml = """
136
+ apiVersion: v1
137
+ kind: Pod
138
+ metadata: invalid
139
+ """ .trimIndent()
140
+ assertThatThrownBy {
141
+ // when
142
+ EditorResourceSerialization .deserialize(invalidYaml, YAMLFileType .YML , " dagobah" )
143
+ // then
144
+ }.isInstanceOf(ResourceException ::class .java)
145
+ }
146
+
147
+ @Test
148
+ fun `#serialize returns null if given null file type` () {
149
+ // given
150
+ val resource = resource<Pod >(" darth vader" )
151
+ // when
152
+ val result = EditorResourceSerialization .serialize(listOf (resource), null )
153
+ // then
154
+ assertThat(result)
155
+ .isNull()
156
+ }
157
+
158
+ @Test
159
+ fun `#serialize throws if given multiple resources and non-YAML file type` () {
160
+ // given
161
+ val resources = listOf (
162
+ resource<Pod >(" darth vader" ),
163
+ resource<Pod >(" emperor" )
164
+ )
165
+ assertThatThrownBy {
166
+ // when
167
+ EditorResourceSerialization .serialize(resources, JsonFileType .INSTANCE )
168
+ // then
169
+ }.isInstanceOf(UnsupportedOperationException ::class .java)
170
+ }
171
+
172
+ @Test
173
+ fun `#serialize returns correct YAML if given single resource and YAML file type` () {
174
+ // given
175
+ val resource = resource<Pod >(" obiwan" )
176
+ val expected = Serialization .asYaml(resource).trim()
177
+ // when
178
+ val result = EditorResourceSerialization .serialize(listOf (resource), YAMLFileType .YML )
179
+ // then
180
+ assertThat(result)
181
+ .isEqualTo(expected)
182
+ }
183
+
184
+ @Test
185
+ fun `#serialize returns multiple YAML resources joined with newline if given 2 resources and YAML file type` () {
186
+ // given
187
+ val resources = listOf (
188
+ resource<Pod >(" leia" ),
189
+ resource<Pod >(" luke" )
190
+ )
191
+ val expected = resources
192
+ .joinToString(" \n " ) {
193
+ Serialization .asYaml(it).trim()
194
+ }
195
+ // when
196
+ val result = EditorResourceSerialization .serialize(resources, YAMLFileType .YML )
197
+ // then
198
+ assertThat(result)
199
+ .isEqualTo(expected)
200
+ }
201
+
202
+ @Test
203
+ fun `#serialize returns JSON if given JSON file type` () {
204
+ // given
205
+ val resource = resource<Pod >(" obiwan" )
206
+ val expected = Serialization .asJson(resource).trim()
207
+ // when
208
+ val result = EditorResourceSerialization .serialize(listOf (resource), JsonFileType .INSTANCE )
209
+ // then
210
+ assertThat(result)
211
+ .isEqualTo(expected)
212
+ }
213
+
214
+ @Test
215
+ fun `#serialize returns null if given unsupported file type` () {
216
+ // given
217
+ val resource = resource<Pod >(" leia" )
218
+ // when
219
+ val result = EditorResourceSerialization .serialize(listOf (resource), PlainTextFileType .INSTANCE )
220
+ // then
221
+ assertThat(result)
222
+ .isEqualTo(" " )
223
+ }
224
+
225
+ }
0 commit comments