Commit 77898bfb authored by harshit aggarwal's avatar harshit aggarwal
Browse files

removing search uts

parent b97897d6
Pipeline #26605 passed with stages
in 19 minutes and 8 seconds
package org.opengroup.osdu.wks.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.wks.config.RequestIdentity;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.model.RequestParameters;
import org.opengroup.osdu.wks.model.RestResponse;
import org.opengroup.osdu.wks.provider.interfaces.UserCredential;
import org.opengroup.osdu.wks.util.RestClient;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
public class SearchServiceTest {
private static final String CORRELATION_ID = "someCorrelationId";
private static final String DATA_PARTITION_ID = "someDataPartitionId";
private static final String QUERY = "Some query";
private static final String TEST_URL = "https://domain-name.org";
@Mock
private UserCredential userCredential;
@Mock
private RestClient restClient;
@Mock
private ITenantFactory tenantStorageFactory;
@Mock
private RequestIdentity requestIdentity;
@Mock
private RestClientConfig restClientConfig;
@Mock
private ServiceHostConfiguration serviceHostConfiguration;
@InjectMocks
private SearchService searchService;
@BeforeEach
void setUp() {
when(requestIdentity.getDataPartitionId()).thenReturn("someDataPartitionId");
when(requestIdentity.getCorrelationId()).thenReturn("someCorrelationId");
when(serviceHostConfiguration.getSearchServiceUrl()).thenReturn(TEST_URL);
}
@Test
public void testQuerySuccess() throws ApplicationException {
ArgumentCaptor<RequestParameters> httpRequestArgument = ArgumentCaptor.forClass(RequestParameters.class);
TenantInfo tenantInfo = new TenantInfo();
when(tenantStorageFactory.getTenantInfo(Mockito.anyString())).thenReturn(tenantInfo);
when(userCredential.getIdToken(Mockito.any(TenantInfo.class))).thenReturn("authToken");
RestResponse response = new RestResponse();
response.setStatus(HttpStatus.OK.value());
response.setMessage("message");
doReturn(response).when(restClient).processRequest(Mockito.any(RequestParameters.class));
String successResponse = searchService.query(QUERY);
assertNotNull(successResponse);
verify(restClient).processRequest(httpRequestArgument.capture());
// Verify its a POST request
assertEquals(HttpMethod.POST, httpRequestArgument.getValue().getMethod());
// Verify data partition is set
assertEquals(DATA_PARTITION_ID, httpRequestArgument.getValue().getDataPartitionId());
// Verify data correlation id is set
assertEquals(CORRELATION_ID, httpRequestArgument.getValue().getCorrelationId());
// Verify auth token is set
assertEquals("authToken", httpRequestArgument.getValue().getAuthKey());
// Verify auth token is set
assertEquals("authToken", httpRequestArgument.getValue().getAuthKey());
// Verify payload is set
assertEquals(QUERY, httpRequestArgument.getValue().getPayload());
}
@Test()
public void testQueryException() throws ApplicationException{
TenantInfo tenantInfo = new TenantInfo();
when(tenantStorageFactory.getTenantInfo(Mockito.anyString())).thenReturn(tenantInfo);
when(userCredential.getIdToken(Mockito.any(TenantInfo.class))).thenReturn("authToken");
RestResponse response = new RestResponse();
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
response.setMessage("message");
doReturn(response).when(restClient).processRequest(Mockito.any(RequestParameters.class));
assertThrows(ApplicationException.class,()->{
searchService.query(QUERY);
});
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment