-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathTestCaseMockTest.java
More file actions
236 lines (193 loc) · 7.96 KB
/
TestCaseMockTest.java
File metadata and controls
236 lines (193 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package org.openmetadata.sdk.entities;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Field;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openmetadata.schema.api.tests.CreateTestCase;
import org.openmetadata.schema.tests.TestCase;
import org.openmetadata.schema.tests.type.TestCaseResult;
import org.openmetadata.sdk.client.OpenMetadata;
import org.openmetadata.sdk.client.OpenMetadataClient;
import org.openmetadata.sdk.services.tests.TestCaseService;
/**
* Mock tests for TestCase entity operations.
*/
public class TestCaseMockTest {
@Mock private OpenMetadataClient mockClient;
@Mock private TestCaseService mockTestCaseService;
@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
when(mockClient.testCases()).thenReturn(mockTestCaseService);
// Ensure TestCase entity operations use this test's mock client even if another
// test class initialized the static default client earlier in the JVM.
org.openmetadata.sdk.entities.TestCase.setDefaultClient(mockClient);
// Use reflection to set the private defaultClient field
Field field = OpenMetadata.class.getDeclaredField("defaultClient");
field.setAccessible(true);
field.set(null, mockClient);
}
@Test
void testCreateTestCase() {
// Arrange
CreateTestCase createRequest = new CreateTestCase();
createRequest.setName("null-check-test");
createRequest.setDisplayName("Null Check Test");
createRequest.setDescription("Test to check for null values in column");
// createRequest.setTestSuites(...); // Optional on CreateTestCase
TestCase expectedTestCase = new TestCase();
expectedTestCase.setId(UUID.randomUUID());
expectedTestCase.setName("null-check-test");
expectedTestCase.setFullyQualifiedName("quality-test-suite.null-check-test");
when(mockTestCaseService.create(any(CreateTestCase.class))).thenReturn(expectedTestCase);
// Act
TestCase result = org.openmetadata.sdk.entities.TestCase.create(createRequest);
// Assert
assertNotNull(result);
assertEquals("null-check-test", result.getName());
verify(mockTestCaseService).create(any(CreateTestCase.class));
}
@Test
void testRetrieveTestCase() {
// Arrange
String testCaseId = UUID.randomUUID().toString();
TestCase expectedTestCase = new TestCase();
expectedTestCase.setId(UUID.fromString(testCaseId));
expectedTestCase.setName("duplicate-check");
// expectedTestCase.setEnabled(true); // Check if this field exists
when(mockTestCaseService.get(testCaseId)).thenReturn(expectedTestCase);
// Act
TestCase result = org.openmetadata.sdk.entities.TestCase.retrieve(testCaseId);
// Assert
assertNotNull(result);
assertEquals(testCaseId, result.getId().toString());
assertEquals("duplicate-check", result.getName());
// assertTrue(result.getEnabled());
verify(mockTestCaseService).get(testCaseId);
}
@Test
void testRetrieveTestCaseWithResults() {
// Arrange
String testCaseId = UUID.randomUUID().toString();
String fields = "testCaseResults,testSuite";
TestCase expectedTestCase = new TestCase();
expectedTestCase.setId(UUID.fromString(testCaseId));
expectedTestCase.setName("range-check");
// Mock test results
TestCaseResult result1 = new TestCaseResult();
// result1.setTestCaseStatus(TestCaseStatus.SUCCESS);
result1.setResult("All values within expected range");
// expectedTestCase.setTestCaseResults(List.of(result1));
when(mockTestCaseService.get(testCaseId, fields)).thenReturn(expectedTestCase);
// Act
TestCase result = org.openmetadata.sdk.entities.TestCase.retrieve(testCaseId, fields);
// Assert
assertNotNull(result);
// assertNotNull(result.getTestCaseResults());
// assertEquals(1, result.getTestCaseResults().size());
// assertEquals(TestCaseStatus.SUCCESS, result.getTestCaseResults().get(0).getTestCaseStatus());
verify(mockTestCaseService).get(testCaseId, fields);
}
@Test
void testRetrieveTestCaseByName() {
// Arrange
String fqn = "quality-suite.uniqueness-test";
TestCase expectedTestCase = new TestCase();
expectedTestCase.setName("uniqueness-test");
expectedTestCase.setFullyQualifiedName(fqn);
when(mockTestCaseService.getByName(fqn)).thenReturn(expectedTestCase);
// Act
TestCase result = org.openmetadata.sdk.entities.TestCase.retrieveByName(fqn);
// Assert
assertNotNull(result);
assertEquals(fqn, result.getFullyQualifiedName());
verify(mockTestCaseService).getByName(fqn);
}
@Test
void testUpdateTestCase() {
// Arrange
TestCase testCaseToUpdate = new TestCase();
testCaseToUpdate.setId(UUID.randomUUID());
testCaseToUpdate.setName("updated-test");
testCaseToUpdate.setDescription("Updated test case description");
// testCaseToUpdate.setEnabled(false);
TestCase expectedTestCase = new TestCase();
expectedTestCase.setId(testCaseToUpdate.getId());
expectedTestCase.setName(testCaseToUpdate.getName());
expectedTestCase.setDescription(testCaseToUpdate.getDescription());
// expectedTestCase.setEnabled(false);
when(mockTestCaseService.update(testCaseToUpdate.getId().toString(), testCaseToUpdate))
.thenReturn(expectedTestCase);
// Act
TestCase result =
org.openmetadata.sdk.entities.TestCase.update(
testCaseToUpdate.getId().toString(), testCaseToUpdate);
// Assert
assertNotNull(result);
assertEquals("Updated test case description", result.getDescription());
// assertFalse(result.getEnabled());
verify(mockTestCaseService).update(testCaseToUpdate.getId().toString(), testCaseToUpdate);
}
@Test
void testDeleteTestCase() {
// Arrange
String testCaseId = UUID.randomUUID().toString();
doNothing().when(mockTestCaseService).delete(eq(testCaseId), any());
// Act
org.openmetadata.sdk.entities.TestCase.delete(testCaseId, false, false);
// Assert
verify(mockTestCaseService).delete(eq(testCaseId), any());
}
@Test
void testTestCaseWithParameters() {
// Arrange
String testCaseId = UUID.randomUUID().toString();
TestCase expectedTestCase = new TestCase();
expectedTestCase.setId(UUID.fromString(testCaseId));
expectedTestCase.setName("threshold-test");
// Mock test parameters as JSON
// expectedTestCase.setParameterValues(List.of(
// Map.of("column", "price", "min", 0, "max", 1000000)
// ));
when(mockTestCaseService.get(testCaseId, "parameterValues")).thenReturn(expectedTestCase);
// Act
TestCase result =
org.openmetadata.sdk.entities.TestCase.retrieve(testCaseId, "parameterValues");
// Assert
assertNotNull(result);
// assertNotNull(result.getParameterValues());
// assertEquals(1, result.getParameterValues().size());
verify(mockTestCaseService).get(testCaseId, "parameterValues");
}
@Test
void testAsyncOperations() throws Exception {
// Arrange
String testCaseId = UUID.randomUUID().toString();
TestCase expectedTestCase = new TestCase();
expectedTestCase.setId(UUID.fromString(testCaseId));
expectedTestCase.setName("async-test");
when(mockTestCaseService.get(testCaseId)).thenReturn(expectedTestCase);
// Act
// Async not available in TestCase SDK, simulate with CompletableFuture
var future =
CompletableFuture.supplyAsync(
() -> {
try {
return org.openmetadata.sdk.entities.TestCase.retrieve(testCaseId);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
TestCase result = future.get();
// Assert
assertNotNull(result);
assertEquals("async-test", result.getName());
verify(mockTestCaseService).get(testCaseId);
}
}