Commit d7081873 authored by Smitha Manjunath's avatar Smitha Manjunath
Browse files

made changes in test class

parent 8502883e
package org.opengroup.osdu.azure.health;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.logging.CoreLogger;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import org.springframework.boot.actuate.endpoint.SecurityContext;
......@@ -20,7 +20,6 @@ import org.springframework.boot.actuate.health.HealthEndpointGroups;
import org.springframework.boot.actuate.health.Status;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
......@@ -39,8 +38,10 @@ import static org.mockito.Mockito.when;
public class AzureHealthEndpointWebExtensionTest {
private static AzureHealthEndpointWebExtension loggingHealthEndpointWebExtension;
private static AzureHealthEndpointWebExtension spy;
private static HealthContributorRegistry registry;
private static HealthEndpointGroups groups;
private static Map<String, HealthComponent> componentMap; //Contains the 'components' of the health endpoint response
@Mock
private CoreLoggerFactory coreLoggerFactory;
......@@ -48,6 +49,15 @@ public class AzureHealthEndpointWebExtensionTest {
@Mock
private CoreLogger logger;
@Mock
SecurityContext securityContext;
@Mock
Health health;
@Mock
CompositeHealth compositeHealth;
/**
* Workaround for inability to mock static methods like getInstance().
*
......@@ -65,66 +75,50 @@ public class AzureHealthEndpointWebExtensionTest {
@BeforeEach
public void setup() {
registry = mock(HealthContributorRegistry.class);
groups = mock(HealthEndpointGroups.class);
loggingHealthEndpointWebExtension = new AzureHealthEndpointWebExtension(registry,groups);
spy = Mockito.spy(loggingHealthEndpointWebExtension);
componentMap = new HashMap<>();
componentMap.put("keyvault", health);
}
@Mock
SecurityContext securityContext;
/**
Verify that nothing is logged when Status of health endpoint is UP.
*/
@Mock
Health health;
@Test
public void healthTest_whenStatusIs_UP() {
Mockito.when(health.getStatus()).thenReturn(Status.UP);
Mockito.when(compositeHealth.getComponents()).thenReturn(componentMap);
@Mock
CompositeHealth compositeHealth;
WebEndpointResponse<HealthComponent> expected = new WebEndpointResponse<>(compositeHealth, WebEndpointResponse.STATUS_OK);
@Test
public void healthTest_whenStatusIs_DOWN() throws Exception{
loggingHealthEndpointWebExtension = new AzureHealthEndpointWebExtension(registry,groups);
AzureHealthEndpointWebExtension spy = Mockito.spy(loggingHealthEndpointWebExtension);
doReturn(expected).when(spy).superClassCall(ApiVersion.LATEST, securityContext, true);
spy.health(ApiVersion.LATEST, securityContext, true, new String[0]);
verify(logger, times(0)).error(anyString(), any(), any());
}
String[] path = new String[0];
/**
Verify that a single line is logged when ONE HealthComponent has Status DOWN.
*/
@Test
public void healthTest_whenStatusIs_DOWN() throws Exception{
mockSingleton(coreLoggerFactory);
when(coreLoggerFactory.getLogger(anyString())).thenReturn(logger);
Mockito.when(health.getStatus()).thenReturn(Status.DOWN);
Map<String, HealthComponent> map = new HashMap<>();
map.put("keyvault", health);
Method privateMethod = AzureHealthEndpointWebExtension.class.getDeclaredMethod("superClassCall", ApiVersion.class, SecurityContext.class, boolean.class, String[].class);
Mockito.when(compositeHealth.getComponents()).thenReturn(map);
Mockito.when(health.getStatus()).thenReturn(Status.DOWN);
Mockito.when(compositeHealth.getComponents()).thenReturn(componentMap);
WebEndpointResponse<HealthComponent> expected = new WebEndpointResponse<>(compositeHealth, WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE);
doReturn(expected).when(spy).superClassCall(ApiVersion.LATEST, securityContext, true);
WebEndpointResponse<HealthComponent> actual = spy.health(ApiVersion.LATEST, securityContext, true, path);
// Mockito.when(superClass.health(ApiVersion.LATEST,securityContext,true, path)).thenReturn(expected);
spy.health(ApiVersion.LATEST, securityContext, true, new String[0]);
verify(logger, times(1)).error(anyString(), any(), any());
}
/* @Test
public void healthTest_whenStatusIs_UP() {
String[] path = new String[0];
Mockito.when(health.getStatus()).thenReturn(Status.UP);
Map<String, HealthComponent> map = new HashMap<>();
map.put("keyvault", health);
Mockito.when(compositeHealth.getComponents()).thenReturn(map);
WebEndpointResponse<HealthComponent> expected = new WebEndpointResponse<>(compositeHealth, WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE);
loggingHealthEndpointWebExtension = new AzureHealthEndpointWebExtension(registry, groups) {
@Override
protected WebEndpointResponse<HealthComponent> superClassCall(ApiVersion apiVersion, SecurityContext securityContext,
boolean showAll, String... path) {
return expected;
}
};
WebEndpointResponse<HealthComponent> actual = loggingHealthEndpointWebExtension.health(ApiVersion.LATEST, securityContext, true, path);
verify(logger, times(0)).error(anyString(), any(), any());
}*/
}
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