diff options
Diffstat (limited to 'so-cnf-adapter-application')
3 files changed, 306 insertions, 43 deletions
diff --git a/so-cnf-adapter-application/pom.xml b/so-cnf-adapter-application/pom.xml index cd2d28e..e51144d 100755 --- a/so-cnf-adapter-application/pom.xml +++ b/so-cnf-adapter-application/pom.xml @@ -163,6 +163,16 @@ <version>2.3.0</version> <type>pom</type> </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/CnfAdapterServiceTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/CnfAdapterServiceTest.java index 8e8cd5c..f049310 100644 --- a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/CnfAdapterServiceTest.java +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/CnfAdapterServiceTest.java @@ -20,108 +20,280 @@ package org.onap.so.adapters.cnf.service; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; + +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.InjectMocks; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.onap.so.adapters.cnf.MulticloudConfiguration; import org.onap.so.adapters.cnf.model.BpmnInstanceRequest; +import org.onap.so.adapters.cnf.service.healthcheck.HealthCheckService; +import org.onap.so.adapters.cnf.service.statuscheck.SimpleStatusCheckService; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.HttpServerErrorException; +import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.RestTemplate; +import java.io.IOException; import java.util.HashMap; import java.util.Map; +import javax.persistence.EntityNotFoundException; + +import lombok.Data; + @RunWith(SpringRunner.class) public class CnfAdapterServiceTest { - private static final String INSTANCE_CREATE_PATH = "/v1/instance"; + + private static final String INSTANCE_ID = "ins"; + + private CnfAdapterService cnfAdapterService; + @Mock private RestTemplate restTemplate; - @InjectMocks - CnfAdapterService cnfAdapterService; - @Mock - private MulticloudConfiguration multicloudConfiguration; + HealthCheckService healthCheckService; @Mock - ResponseEntity<String> instanceResponse; + SimpleStatusCheckService simpleStatusCheckService; + + + @Before + public void setUp() { + MulticloudConfiguration multicloudConfiguration = mock(MulticloudConfiguration.class); + doReturn("http://test.url").when(multicloudConfiguration).getMulticloudUrl(); + cnfAdapterService = spy(new CnfAdapterService(restTemplate, healthCheckService, simpleStatusCheckService, multicloudConfiguration)); + } + - @Test public void createInstanceTest() throws Exception { - Map<String, String> labels = new HashMap<String, String>(); - labels.put("custom-label-1", "label1"); - Map<String, String> overrideValues = new HashMap<String, String>(); - overrideValues.put("a", "b"); - labels.put("image.tag", "latest"); - labels.put("dcae_collector_ip", "1.2.3.4"); - BpmnInstanceRequest bpmnInstanceRequest = new BpmnInstanceRequest(); - bpmnInstanceRequest.setCloudRegionId("v1"); - bpmnInstanceRequest.setLabels(labels); - bpmnInstanceRequest.setModelInvariantId("krd"); - bpmnInstanceRequest.setModelVersionId("p1"); - bpmnInstanceRequest.setOverrideValues(overrideValues); - bpmnInstanceRequest.setVfModuleUUID("20200824"); - bpmnInstanceRequest.setK8sRBProfileName("K8sRBProfileName is required"); try { - cnfAdapterService.createInstance(bpmnInstanceRequest); + cnfAdapterService.createInstance(getBpmnInstanceRequest()); + } catch (Exception exp) { + assert (true); } - catch (Exception exp) { - assert(true); + } + + @Test(expected = EntityNotFoundException.class) + public void testcreateInstanceHttpException() { + doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + try { + cnfAdapterService.createInstance(getBpmnInstanceRequest()); + } catch (IOException e) { + e.printStackTrace(); } + } + @Test(expected = HttpStatusCodeException.class) + public void testcreateInstanceHttpStatusCodeException() { + doThrow(new HttpServerErrorException(HttpStatus.CONFLICT)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + try { + cnfAdapterService.createInstance(getBpmnInstanceRequest()); + } catch (IOException e) { + e.printStackTrace(); + } } @Test public void getInstanceByInstanceIdTest() throws Exception { - String instanceId = "ins"; try { - cnfAdapterService.getInstanceByInstanceId(instanceId); + cnfAdapterService.getInstanceByInstanceId(INSTANCE_ID); + } catch (Exception exp) { + assert (true); } - catch (Exception exp) { - assert(true); + } + + @Test(expected = EntityNotFoundException.class) + public void testInstanceByInstanceIdHttpException() { + doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + try { + cnfAdapterService.getInstanceByInstanceId(INSTANCE_ID); + } catch (IOException e) { + e.printStackTrace(); } + } + @Test(expected = HttpStatusCodeException.class) + public void testInstanceByInstanceIdHttpStatusCodeException() { + doThrow(new HttpServerErrorException(HttpStatus.CONFLICT)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + try { + cnfAdapterService.getInstanceByInstanceId(INSTANCE_ID); + } catch (IOException e) { + e.printStackTrace(); + } } @Test public void getInstanceStatusByInstanceIdTest() throws Exception { - String instanceId = "ins"; try { - cnfAdapterService.getInstanceStatusByInstanceId(instanceId); + cnfAdapterService.getInstanceStatusByInstanceId(INSTANCE_ID); + } catch (Exception exp) { + assert (true); } - catch (Exception exp) { - assert(true); + } + + @Test(expected = EntityNotFoundException.class) + public void testInstanceStatusByInstanceIdHttpException() { + doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + try { + cnfAdapterService.getInstanceStatusByInstanceId(INSTANCE_ID); + } catch (IOException e) { + e.printStackTrace(); } + } + @Test(expected = HttpStatusCodeException.class) + public void testInstanceStatusByInstanceIdHttpStatusCodeException() { + doThrow(new HttpServerErrorException(HttpStatus.CONFLICT)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + try { + cnfAdapterService.getInstanceStatusByInstanceId(INSTANCE_ID); + } catch (IOException e) { + e.printStackTrace(); + } } @Test public void getInstanceByRBNameOrRBVersionOrProfileNameTest() throws Exception { - String rbName = "rb"; - String rbVersion = "rv1"; - String profileName = "p1"; + RbNameVersionData rbNameVersionData = new RbNameVersionData(); try { - cnfAdapterService.getInstanceByRBNameOrRBVersionOrProfileName(rbName, rbVersion, profileName); + cnfAdapterService.getInstanceByRBNameOrRBVersionOrProfileName(rbNameVersionData.getRbName(), rbNameVersionData.getRbVersion(), + rbNameVersionData.getProfileName()); + } catch (Exception exp) { + assert (true); } - catch (Exception exp) { - assert(true); + } + + @Test(expected = EntityNotFoundException.class) + public void testInstanceByRBNameOrRBVersionOrProfileNameHttpException() { + RbNameVersionData rbNameVersionData = new RbNameVersionData(); + doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + try { + cnfAdapterService.getInstanceByRBNameOrRBVersionOrProfileName(rbNameVersionData.getRbName(), rbNameVersionData.getRbVersion(), + rbNameVersionData.getProfileName()); + } catch (IOException e) { + e.printStackTrace(); } + } + @Test(expected = HttpStatusCodeException.class) + public void testInstanceByRBNameOrRBVersionOrProfileNameHttpStatusCodeException() { + RbNameVersionData rbNameVersionData = new RbNameVersionData(); + doThrow(new HttpServerErrorException(HttpStatus.CONFLICT)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + try { + cnfAdapterService.getInstanceByRBNameOrRBVersionOrProfileName(rbNameVersionData.getRbName(), rbNameVersionData.getRbVersion(), + rbNameVersionData.getProfileName()); + } catch (IOException e) { + e.printStackTrace(); + } } @Test public void deleteInstanceByInstanceIdTest() throws Exception { - String instanceId = "ins"; try { - cnfAdapterService.deleteInstanceByInstanceId(instanceId); + cnfAdapterService.deleteInstanceByInstanceId(INSTANCE_ID); + } catch (Exception exp) { + assert (true); + } + } + + @Test(expected = EntityNotFoundException.class) + public void testdeleteInstanceByInstanceIdHttpException() { + doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + try { + cnfAdapterService.deleteInstanceByInstanceId(INSTANCE_ID); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test(expected = HttpStatusCodeException.class) + public void testdeleteInstanceByInstanceIdException() { + doThrow(new HttpServerErrorException(HttpStatus.CONFLICT)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + try { + cnfAdapterService.deleteInstanceByInstanceId(INSTANCE_ID); + } catch (IOException e) { + e.printStackTrace(); } - catch (Exception exp) { - assert(true); + } + + @Test + public void queryInstanceResourcesTest() { + try { + cnfAdapterService.queryInstanceResources(INSTANCE_ID, "", "", "", "", ""); + } catch (Exception exp) { + assert (true); } + } + @Test(expected = EntityNotFoundException.class) + public void queryInstanceResourcesException() { + doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + cnfAdapterService.queryInstanceResources(INSTANCE_ID, "", "", "", "", ""); + } + + @Test + public void queryResourcesTest() { + try { + cnfAdapterService.queryResources(INSTANCE_ID, "", "", "", "", ""); + } catch (Exception exp) { + assert (true); + } + } + + @Test(expected = EntityNotFoundException.class) + public void queryResourcesException() { + doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when(restTemplate).exchange(ArgumentMatchers.anyString(), + ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.<Class<String>>any()); + cnfAdapterService.queryResources(INSTANCE_ID, "", "", "", "", ""); + } + + private BpmnInstanceRequest getBpmnInstanceRequest() { + Map<String, String> labels = new HashMap<String, String>(); + labels.put("custom-label-1", "label1"); + Map<String, String> overrideValues = new HashMap<String, String>(); + overrideValues.put("a", "b"); + labels.put("image.tag", "latest"); + labels.put("dcae_collector_ip", "1.2.3.4"); + BpmnInstanceRequest bpmnInstanceRequest = new BpmnInstanceRequest(); + bpmnInstanceRequest.setCloudRegionId("v1"); + bpmnInstanceRequest.setLabels(labels); + bpmnInstanceRequest.setModelInvariantId("krd"); + bpmnInstanceRequest.setModelVersionId("p1"); + bpmnInstanceRequest.setOverrideValues(overrideValues); + bpmnInstanceRequest.setVfModuleUUID("20200824"); + bpmnInstanceRequest.setK8sRBProfileName("K8sRBProfileName is required"); + return bpmnInstanceRequest; + } + + @Data + private class RbNameVersionData { + + String rbName = "rb"; + String rbVersion = "rv1"; + String profileName = "p1"; } } diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/healthcheck/HealthCheckServiceTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/healthcheck/HealthCheckServiceTest.java new file mode 100644 index 0000000..9a043ba --- /dev/null +++ b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/healthcheck/HealthCheckServiceTest.java @@ -0,0 +1,81 @@ +package org.onap.so.adapters.cnf.service.healthcheck; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.UUID; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.so.adapters.cnf.client.MulticloudClient; +import org.onap.so.adapters.cnf.model.CheckInstanceRequest; +import org.onap.so.adapters.cnf.model.InstanceRequest; +import org.onap.so.adapters.cnf.model.healthcheck.HealthCheckResponse; +import org.onap.so.adapters.cnf.model.healthcheck.K8sRbInstanceHealthCheck; +import org.onap.so.adapters.cnf.model.healthcheck.K8sRbInstanceHealthCheckSimple; +import org.onap.so.client.exception.BadResponseException; + +class HealthCheckServiceTest { + + @InjectMocks + HealthCheckService healthCheckService; + + @Mock + MulticloudClient multicloudClient; + + @BeforeEach + void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void healthCheckTest() { + try { + doReturn(getK8sRbInstanceHealthCheckSimple()).when(multicloudClient).startInstanceHealthCheck(any()); + doReturn(getK8sRbInstanceHealthCheck()).when(multicloudClient).getInstanceHealthCheck(any(),any()); + HealthCheckResponse response = healthCheckService.healthCheck(getTestCheckInstanceRequest()); + + verify(multicloudClient, times(3)).startInstanceHealthCheck(any()); + verify(multicloudClient, times(3)).getInstanceHealthCheck(any(),any()); + assertThat(response.getInstanceResponse()).hasSize(getTestCheckInstanceRequest().getInstances().size()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private CheckInstanceRequest getTestCheckInstanceRequest(){ + CheckInstanceRequest checkInstanceRequest=new CheckInstanceRequest() ; + checkInstanceRequest.setInstances(asList(getRandomInstance(),getRandomInstance(),getRandomInstance())); + return checkInstanceRequest; + } + + private InstanceRequest getRandomInstance(){ + InstanceRequest instanceRequest=new InstanceRequest(); + instanceRequest.setInstanceId(UUID.randomUUID().toString()); + return instanceRequest; + } + + private K8sRbInstanceHealthCheckSimple getK8sRbInstanceHealthCheckSimple(){ + K8sRbInstanceHealthCheckSimple k8sRbInstanceHealthCheckSimple=new K8sRbInstanceHealthCheckSimple(); + k8sRbInstanceHealthCheckSimple.setId(UUID.randomUUID().toString()); + k8sRbInstanceHealthCheckSimple.setStatus("RUNNING"); + return k8sRbInstanceHealthCheckSimple; + } + + private K8sRbInstanceHealthCheck getK8sRbInstanceHealthCheck(){ + K8sRbInstanceHealthCheck k8sRbInstanceHealthCheck=new K8sRbInstanceHealthCheck(); + k8sRbInstanceHealthCheck.setStatus("STOPPED"); + return k8sRbInstanceHealthCheck; + } +}
\ No newline at end of file |