Skip to content

Commit 22db52e

Browse files
authored
Add YamlModelRepository test for removal of version or element type (#4758)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
1 parent 7820dc4 commit 22db52e

File tree

7 files changed

+101
-4
lines changed

7 files changed

+101
-4
lines changed

bundles/org.openhab.core.model.yaml/src/test/java/org/openhab/core/model/yaml/internal/YamlModelRepositoryImplTest.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.junit.jupiter.api.Test;
3333
import org.junit.jupiter.api.extension.ExtendWith;
3434
import org.junit.jupiter.api.io.TempDir;
35+
import org.junit.jupiter.params.ParameterizedTest;
36+
import org.junit.jupiter.params.provider.CsvSource;
3537
import org.mockito.ArgumentCaptor;
3638
import org.mockito.Captor;
3739
import org.mockito.Mock;
@@ -120,9 +122,9 @@ public void testFileAddedAfterListeners() throws IOException {
120122
List<Collection<FirstTypeDTO>> firstTypeCaptorValues = firstTypeCaptor.getAllValues();
121123
assertThat(firstTypeCaptorValues, hasSize(2));
122124
List<Collection<SecondTypeDTO>> secondTypeCaptor1Values = secondTypeCaptor1.getAllValues();
123-
assertThat(firstTypeCaptorValues, hasSize(2));
125+
assertThat(secondTypeCaptor1Values, hasSize(2));
124126
List<Collection<SecondTypeDTO>> secondTypeCaptor2Values = secondTypeCaptor2.getAllValues();
125-
assertThat(firstTypeCaptorValues, hasSize(2));
127+
assertThat(secondTypeCaptor2Values, hasSize(2));
126128

127129
Collection<FirstTypeDTO> firstTypeElements = firstTypeCaptorValues.getFirst();
128130
Collection<SecondTypeDTO> secondTypeElements1 = secondTypeCaptor1Values.getFirst();
@@ -179,9 +181,9 @@ public void testFileAddedBeforeListeners() throws IOException {
179181
List<Collection<FirstTypeDTO>> firstTypeCaptorValues = firstTypeCaptor.getAllValues();
180182
assertThat(firstTypeCaptorValues, hasSize(2));
181183
List<Collection<SecondTypeDTO>> secondTypeCaptor1Values = secondTypeCaptor1.getAllValues();
182-
assertThat(firstTypeCaptorValues, hasSize(2));
184+
assertThat(secondTypeCaptor1Values, hasSize(2));
183185
List<Collection<SecondTypeDTO>> secondTypeCaptor2Values = secondTypeCaptor2.getAllValues();
184-
assertThat(firstTypeCaptorValues, hasSize(2));
186+
assertThat(secondTypeCaptor2Values, hasSize(2));
185187

186188
Collection<FirstTypeDTO> firstTypeElements = firstTypeCaptorValues.getFirst();
187189
Collection<SecondTypeDTO> secondTypeElements1 = secondTypeCaptor1Values.getFirst();
@@ -268,6 +270,50 @@ public void testFileUpdated() throws IOException {
268270
assertThat(firstTypeCaptorValues.get(7), contains(new FirstTypeDTO("Third", "Third original")));
269271
}
270272

273+
@ParameterizedTest
274+
@CsvSource({ //
275+
"modelFileUpdateRemovedElements.yaml,modelV2FileUpdateRemovedElements.yaml",
276+
"modelFileUpdateRenamedElements.yaml,modelV2FileUpdateRenamedElements.yaml",
277+
"modelFileUpdateRemovedVersion.yaml,modelV2FileUpdateRemovedVersion.yaml" //
278+
})
279+
public void testFileRemovedElements(String v1File, String v2File) throws IOException {
280+
YamlModelRepositoryImpl modelRepository = new YamlModelRepositoryImpl(watchServiceMock);
281+
modelRepository.addYamlModelListener(firstTypeListener);
282+
283+
// File in v1 format
284+
Files.copy(SOURCE_PATH.resolve("modelFileUpdatePost.yaml"), fullModelPath);
285+
modelRepository.processWatchEvent(WatchService.Kind.CREATE, MODEL_PATH);
286+
verify(firstTypeListener).addedModel(eq(MODEL_NAME), any());
287+
288+
Files.copy(SOURCE_PATH.resolve(v1File), fullModelPath, StandardCopyOption.REPLACE_EXISTING);
289+
modelRepository.processWatchEvent(WatchService.Kind.MODIFY, MODEL_PATH);
290+
verify(firstTypeListener).removedModel(eq(MODEL_NAME), firstTypeCaptor.capture());
291+
292+
Collection<FirstTypeDTO> firstTypeElements = firstTypeCaptor.getAllValues().getFirst();
293+
294+
// Check that the elements were removed
295+
assertThat(firstTypeElements, hasSize(3));
296+
assertThat(firstTypeElements, containsInAnyOrder(new FirstTypeDTO("First", "First original"),
297+
new FirstTypeDTO("Second", "Second original"), new FirstTypeDTO("Third", "Third original")));
298+
299+
// File in v2 format
300+
Files.copy(SOURCE_PATH.resolve("modelV2FileUpdatePost.yaml"), fullModel2Path);
301+
modelRepository.processWatchEvent(WatchService.Kind.CREATE, MODEL2_PATH);
302+
verify(firstTypeListener).addedModel(eq(MODEL2_NAME), any());
303+
304+
Files.copy(SOURCE_PATH.resolve(v2File), fullModel2Path, StandardCopyOption.REPLACE_EXISTING);
305+
modelRepository.processWatchEvent(WatchService.Kind.MODIFY, MODEL2_PATH);
306+
verify(firstTypeListener).removedModel(eq(MODEL2_NAME), firstTypeCaptor.capture());
307+
308+
assertThat(firstTypeCaptor.getAllValues(), hasSize(2));
309+
firstTypeElements = firstTypeCaptor.getAllValues().getLast();
310+
311+
// Check that the elements were removed
312+
assertThat(firstTypeElements, hasSize(3));
313+
assertThat(firstTypeElements, containsInAnyOrder(new FirstTypeDTO("First", "First original"),
314+
new FirstTypeDTO("Second", "Second original"), new FirstTypeDTO("Third", "Third original")));
315+
}
316+
271317
@Test
272318
public void testFileRemoved() throws IOException {
273319
YamlModelRepositoryImpl modelRepository = new YamlModelRepositoryImpl(watchServiceMock);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# A YAML test file for updating the model
2+
3+
version: 1
4+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# A YAML test file for updating the model
2+
3+
# version: 1 => removed in this update
4+
5+
firstType:
6+
- uid: First
7+
description: First original
8+
- uid: Second
9+
description: Second original
10+
- uid: Third
11+
description: Third original
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# A YAML test file for updating the model
2+
3+
version: 1
4+
5+
firstTypeRenamed:
6+
- uid: First
7+
description: First original
8+
- uid: Second
9+
description: Second original
10+
- uid: Third
11+
description: Third original
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# A YAML test file for updating the model
2+
3+
version: 2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# A YAML test file in version 2 for updating the model
2+
3+
# version: 2 => removed
4+
5+
firstType:
6+
First:
7+
description: First original
8+
Second:
9+
description: Second original
10+
Third:
11+
description: Third original
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# A YAML test file for updating the model
2+
3+
version: 2
4+
5+
firstTypeRenamed:
6+
First:
7+
description: First original
8+
Second:
9+
description: Second original
10+
Third:
11+
description: Third original

0 commit comments

Comments
 (0)