Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.LinkedMultiValueMap;
Expand All @@ -38,6 +39,7 @@
import java.util.concurrent.TimeoutException;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -434,4 +436,137 @@ public void testCheckErrorResponseRetry() throws JsonParseException, JsonMapping
assertEquals(cause.getErrorText(), HotlistErrorConstants.CLIENT_ERROR.getErrorMessage());
}
}

@Test
public void testRequestSyncSuccess() throws Exception {

RestRequestDTO dto = new RestRequestDTO();
dto.setUri("http://test.com");
dto.setHttpMethod(HttpMethod.GET);
dto.setResponseType(ObjectNode.class);

ReflectionTestUtils.setField(restHelper, "mapper", new ObjectMapper());
ReflectionTestUtils.setField(restHelper, "webClient", webClient);

String jsonResponse = "{\"response\":{\"status\":\"success\"}}";

when(webClient.method(HttpMethod.GET)).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri("http://test.com")).thenReturn(requestBodySpec);
when(requestBodySpec.retrieve()).thenReturn(responseSpec);
when(responseSpec.bodyToMono(ObjectNode.class))
.thenReturn(Mono.just(new ObjectMapper().readValue(jsonResponse, ObjectNode.class)));

ObjectNode response = restHelper.requestSync(dto);

assertNotNull(response);
assertTrue(response.has("response"));
}

@Test
public void testRequestAsyncSuccess() {

RestRequestDTO dto = new RestRequestDTO();
dto.setUri("http://test.com");
dto.setHttpMethod(HttpMethod.GET);
dto.setResponseType(String.class);

when(webClient.method(HttpMethod.GET)).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri("http://test.com")).thenReturn(requestBodySpec);
when(requestBodySpec.retrieve()).thenReturn(responseSpec);
when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("ASYNC_OK"));

ReflectionTestUtils.setField(restHelper, "webClient", webClient);

Object result = restHelper.requestAsync(dto).get();

assertEquals("ASYNC_OK", result);
}

@Test(expected = RestServiceException.class)
public void testRequestSync_WebClientResponseException() throws Exception {

RestRequestDTO dto = new RestRequestDTO();
dto.setUri("http://test.com");
dto.setHttpMethod(HttpMethod.GET);
dto.setResponseType(ObjectNode.class);

ReflectionTestUtils.setField(restHelper, "mapper", new ObjectMapper());
ReflectionTestUtils.setField(restHelper, "webClient", webClient);

when(webClient.method(HttpMethod.GET)).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri("http://test.com")).thenReturn(requestBodySpec);
when(requestBodySpec.retrieve()).thenReturn(responseSpec);

String errorBody = "{\"errors\":[{\"errorCode\":\"ERR-001\",\"message\":\"Bad Request\"}]}";

when(responseSpec.bodyToMono(ObjectNode.class))
.thenReturn(Mono.error(
new WebClientResponseException(
"Bad Request",
400,
"Bad Request",
null,
errorBody.getBytes(),
null)));

restHelper.requestSync(dto);
}

@Test
public void testRequestSync_WithHeadersAndBody() throws Exception {

RestRequestDTO dto = new RestRequestDTO();
dto.setUri("http://test.com");
dto.setHttpMethod(HttpMethod.POST);
dto.setResponseType(ObjectNode.class);

// ✅ Use HttpHeaders (IMPORTANT)
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
dto.setHeaders(headers);

dto.setRequestBody("{\"name\":\"test\"}");

ReflectionTestUtils.setField(restHelper, "mapper", new ObjectMapper());
ReflectionTestUtils.setField(restHelper, "webClient", webClient);

when(webClient.method(HttpMethod.POST)).thenReturn(requestBodyUriSpec);
when(requestBodyUriSpec.uri("http://test.com")).thenReturn(requestBodySpec);

when(requestBodySpec.headers(any())).thenReturn(requestBodySpec);
when(requestBodySpec.syncBody(any())).thenReturn(requestHeadersSpec);
when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);

ObjectNode node = new ObjectMapper()
.readValue("{\"response\":\"ok\"}", ObjectNode.class);

when(responseSpec.bodyToMono(ObjectNode.class))
.thenReturn(Mono.just(node));

ObjectNode response = restHelper.requestSync(dto);

assertNotNull(response);
verify(requestBodySpec).headers(any());
verify(requestBodySpec).syncBody(any());
}

@Test
public void testCheckErrorResponse_ResponseHasErrors_ThrowsClientError() throws Exception {
// Arrange
RestRequestDTO dto = new RestRequestDTO();
dto.setUri("http://dummy.com");
dto.setHttpMethod(HttpMethod.GET);
dto.setResponseType(ObjectNode.class);

String json = "{\"errors\":[{\"errorCode\":\"ERR-001\",\"message\":\"some error\"}]}";
ObjectMapper realMapper = new ObjectMapper();
Object response = realMapper.readValue(json, Object.class);

ReflectionTestUtils.setField(restHelper, "mapper", realMapper);

// Act & Assert
assertThrows(UndeclaredThrowableException.class, () -> {
ReflectionTestUtils.invokeMethod(restHelper, "checkErrorResponse", response, ObjectNode.class);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.mosip.kernel.masterdata.test.service.impl;

import io.mosip.kernel.masterdata.service.impl.CacheManagementServiceImpl;
import io.mosip.kernel.masterdata.utils.CacheName;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.util.Arrays;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.*;

class CacheManagementServiceImplTest {

@InjectMocks
private CacheManagementServiceImpl cacheService;

@Mock
private CacheManager cacheManager;

@Mock
private Cache mockCache1;

@Mock
private Cache mockCache2;

@BeforeEach
void setup() {
MockitoAnnotations.openMocks(this);
}

@Test
void clearCacheByCacheName_existingCache_clearsOnlyMatched() {
when(cacheManager.getCacheNames()).thenReturn(Arrays.asList("blocklisted-words", "other-cache"));
when(cacheManager.getCache("blocklisted-words")).thenReturn(mockCache1);
when(cacheManager.getCache("other-cache")).thenReturn(mockCache2);

cacheService.clearCacheByCacheName(CacheName.BLOCK_LISTED_WORDS);

verify(mockCache1).clear();
verify(mockCache2, never()).clear();
}

@Test
void clearCacheByCacheName_cacheNotFound_doesNotThrow() {
when(cacheManager.getCacheNames()).thenReturn(Collections.singletonList("CACHE2"));
when(cacheManager.getCache("CACHE2")).thenReturn(mockCache2);

assertDoesNotThrow(() -> cacheService.clearCacheByCacheName(CacheName.BLOCK_LISTED_WORDS));

verify(mockCache2, never()).clear();
}

@Test
void clearCacheByCacheName_nullCache_ignored() {
when(cacheManager.getCacheNames()).thenReturn(Collections.singletonList("CACHE1"));
when(cacheManager.getCache("CACHE1")).thenReturn(null);

assertDoesNotThrow(() -> cacheService.clearCacheByCacheName(CacheName.BLOCK_LISTED_WORDS));
}

@Test
void clearCache_clearsAllCaches() {
when(cacheManager.getCacheNames()).thenReturn(Arrays.asList("CACHE1", "CACHE2"));
when(cacheManager.getCache("CACHE1")).thenReturn(mockCache1);
when(cacheManager.getCache("CACHE2")).thenReturn(mockCache2);

cacheService.clearCache();

verify(mockCache1).clear();
verify(mockCache2).clear();
}

@Test
void clearCache_nullCaches_ignored() {
when(cacheManager.getCacheNames()).thenReturn(Arrays.asList("CACHE1", "CACHE2"));
when(cacheManager.getCache("CACHE1")).thenReturn(null);
when(cacheManager.getCache("CACHE2")).thenReturn(mockCache2);

cacheService.clearCache();

verify(mockCache2).clear();
}
}
Loading
Loading