Skip to content

Commit 0bce60e

Browse files
Merge pull request #28 from ruksana2808/cbrelease-4.8.27.2
KB-11217 | BE | QA issue with read API while reading data
2 parents af76551 + c4a77f1 commit 0bce60e

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

src/main/java/com/igot/cb/service/ContentStateServiceImpl.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,12 @@ public ApiResponse readContentState(Map<String, Object> requestBody, String auth
153153
return mapped;
154154
}).toList();
155155

156-
response.getResult().put(Constants.CONTENT_LIST,
157-
objectMapper.convertValue(mappedUserContentDetails, new TypeReference<Object>() {
158-
}));
156+
Object convertInstantsToString = convertInstantsToString(mappedUserContentDetails);
157+
158+
response.getResult().put(
159+
Constants.CONTENT_LIST,
160+
objectMapper.convertValue(convertInstantsToString, new TypeReference<Object>() {})
161+
);
159162
response.setResponseCode(HttpStatus.OK);
160163
return response;
161164

@@ -167,6 +170,28 @@ public ApiResponse readContentState(Map<String, Object> requestBody, String auth
167170
}
168171
}
169172

173+
174+
@SuppressWarnings("unchecked")
175+
private static Object convertInstantsToString(Object value) {
176+
if (value instanceof Instant) {
177+
return value.toString();
178+
} else if (value instanceof Map) {
179+
Map<String, Object> convertedMap = new HashMap<>();
180+
((Map<?, ?>) value).forEach((k, v) ->
181+
convertedMap.put(String.valueOf(k), convertInstantsToString(v))
182+
);
183+
return convertedMap;
184+
} else if (value instanceof List) {
185+
return ((List<?>) value).stream()
186+
.map(ContentStateServiceImpl::convertInstantsToString)
187+
.toList();
188+
} else {
189+
return value;
190+
}
191+
}
192+
193+
194+
170195
public ApiResponse updateContentState(Map<String, Object> requestBody, String authToken) {
171196
log.info("CourseService::readContentState:inside");
172197
ApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_CONTENT_V2_STATE_READ);

src/test/java/com/igot/cb/service/ContentStateServiceImplTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import org.mockito.Mock;
1313
import org.mockito.junit.jupiter.MockitoExtension;
1414
import org.springframework.http.HttpStatus;
15+
import org.springframework.test.util.ReflectionTestUtils;
1516

17+
import java.time.Instant;
1618
import java.util.*;
1719

1820
import static org.junit.jupiter.api.Assertions.*;
@@ -255,4 +257,68 @@ void testMapPayloadToCassandraColumns() {
255257
Map<String, Object> result = ContentStateServiceImpl.mapPayloadToCassandraColumns(payload, mapping);
256258
assertEquals("bar", result.get("baz"));
257259
}
260+
261+
@Test
262+
void testConvertInstantsToString_singleInstant() {
263+
Instant now = Instant.now();
264+
Object result = invokeConvert(now);
265+
266+
assertTrue(result instanceof String);
267+
assertEquals(now.toString(), result);
268+
}
269+
270+
@Test
271+
void testConvertInstantsToString_mapWithInstant() {
272+
Instant now = Instant.now();
273+
Map<String, Object> input = Map.of("time", now);
274+
275+
Object result = invokeConvert(input);
276+
277+
assertTrue(result instanceof Map);
278+
assertEquals(now.toString(), ((Map<?, ?>) result).get("time"));
279+
}
280+
281+
@Test
282+
void testConvertInstantsToString_listWithInstant() {
283+
Instant now = Instant.now();
284+
List<Object> input = List.of(now, "test");
285+
286+
Object result = invokeConvert(input);
287+
288+
assertTrue(result instanceof List);
289+
assertEquals(now.toString(), ((List<?>) result).get(0));
290+
assertEquals("test", ((List<?>) result).get(1));
291+
}
292+
293+
@Test
294+
void testConvertInstantsToString_nestedMapAndList() {
295+
Instant now = Instant.now();
296+
Map<String, Object> input = Map.of(
297+
"list", List.of(Map.of("time", now))
298+
);
299+
300+
Object result = invokeConvert(input);
301+
302+
assertEquals(
303+
now.toString(),
304+
((Map<?, ?>) ((List<?>) ((Map<?, ?>) result).get("list")).get(0)).get("time")
305+
);
306+
}
307+
308+
@Test
309+
void testConvertInstantsToString_nonInstantValue() {
310+
String value = "not a time";
311+
Object result = invokeConvert(value);
312+
313+
assertSame(value, result);
314+
}
315+
316+
private Object invokeConvert(Object value) {
317+
// Call the private static method using ReflectionTestUtils
318+
return ReflectionTestUtils.invokeMethod(
319+
ContentStateServiceImpl.class,
320+
"convertInstantsToString",
321+
value
322+
);
323+
}
258324
}

0 commit comments

Comments
 (0)