diff options
author | Ofir Sonsino <os0695@intl.att.com> | 2018-10-21 07:52:19 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-10-21 07:52:19 +0000 |
commit | 7b3b0ccf0ec6f62d5ab43992f6485335f4fcb272 (patch) | |
tree | a28887759847725a3e7b5e919d7d595133719628 /vid-app-common/src/test/java | |
parent | af0e76fd35fa13538c7d12a7d3187eac632d3d40 (diff) | |
parent | f3d478377229b4b7cb22070cb1317fcc1d196161 (diff) |
Merge "Increase tests coverage in backend"
Diffstat (limited to 'vid-app-common/src/test/java')
5 files changed, 600 insertions, 67 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AuditServiceImplTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AuditServiceImplTest.java new file mode 100644 index 000000000..3d2a20b63 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/services/AuditServiceImplTest.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.vid.services; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.UUID; +import org.glassfish.grizzly.http.util.HttpStatus; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class AuditServiceImplTest { + @Mock + private AsyncInstantiationBusinessLogic asyncInstantiationBL; + + @InjectMocks + private AuditServiceImpl auditService; + + @BeforeClass + public void init() { + initMocks(this); + } + + @Test + public void setFailedAuditStatusFromMsoTest() { + + UUID jobUuid = UUID.randomUUID(); + String requestId = "1"; + int statusCode = HttpStatus.OK_200.getStatusCode(); + String msoResponse = "{}"; + + auditService.setFailedAuditStatusFromMso(jobUuid, requestId, statusCode, msoResponse); + + verify(asyncInstantiationBL, times(1)) + .auditMsoStatus( + Mockito.any(UUID.class), + Mockito.anyString(), + Mockito.anyString(), + Mockito.anyString()); + } +} diff --git a/vid-app-common/src/test/java/org/onap/vid/services/BulkInstantiationServiceImplTest.java b/vid-app-common/src/test/java/org/onap/vid/services/BulkInstantiationServiceImplTest.java new file mode 100644 index 000000000..6677da357 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/services/BulkInstantiationServiceImplTest.java @@ -0,0 +1,135 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.vid.services; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import javax.ws.rs.NotFoundException; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.onap.vid.job.Job; +import org.onap.vid.job.JobAdapter; +import org.onap.vid.job.JobsBrokerService; +import org.onap.vid.job.impl.JobDaoImpl; +import org.onap.vid.model.JobBulk; +import org.onap.vid.model.JobModel; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.Test; + +public class BulkInstantiationServiceImplTest { + + @Mock + private JobsBrokerService jobsBrokerService; + + @Mock + private JobAdapter jobAdapter; + + @InjectMocks + private BulkInstantiationServiceImpl testSubject; + + @BeforeSuite + public void before() { + initMocks(this); + } + + @BeforeMethod + public void resetMocks() { + reset(jobsBrokerService); + reset(jobAdapter); + } + + @Test + public void saveBulkTest() { + UUID uuid = UUID.randomUUID(); + Map<String, Object> bulkRequest = new HashMap<>(); + List<Job> jobList = new ArrayList<>(); + jobList.add(createJob(uuid)); + when(jobAdapter.createBulkOfJobs(bulkRequest)).thenReturn(jobList); + + JobBulk jobBulk = createJobBulk(jobList); + when(jobAdapter.toModelBulk(jobList)).thenReturn(jobBulk); + + JobBulk result = testSubject.saveBulk(bulkRequest); + + Assert.assertEquals(result.getJobs().size(), jobList.size()); + Assert.assertEquals(result.getJobs().get(0).getUuid(), uuid); + } + + @Test + public void getJobTest() { + UUID uuid = UUID.randomUUID(); + Job job = createJob(uuid); + doReturn(job).when(jobsBrokerService).peek(uuid); + JobModel jobModel = createJobModel(uuid); + when(jobAdapter.toModel(job)).thenReturn(jobModel); + + JobModel response = testSubject.getJob(uuid); + Assert.assertEquals(response.getUuid(), uuid); + } + + @Test(expectedExceptions = {NotFoundException.class}) + public void getJobTest_throwsExceptionOnEmptyUUID() { + UUID uuid = null; + Job job = createJob(uuid); + doReturn(job).when(jobsBrokerService).peek(uuid); + JobModel response = testSubject.getJob(uuid); + Assert.fail(); + } + + @Test(expectedExceptions = {NotFoundException.class}) + public void getJobTest_throwsExceptionCauseJobDoesNotExists() { + UUID uuid = UUID.randomUUID(); + doReturn(null).when(jobsBrokerService).peek(uuid); + JobModel response = testSubject.getJob(uuid); + Assert.fail(); + } + + private Job createJob(UUID uuid) { + Job job = new JobDaoImpl(); + job.setUuid(uuid); + return job; + } + + private JobModel createJobModel(UUID uuid) { + JobModel jobModel = new JobModel(); + jobModel.setUuid(uuid); + return jobModel; + } + + private JobBulk createJobBulk(List<Job> jobList) { + List<JobModel> jobBulkList = new ArrayList<>(); + jobList.stream().forEach(job -> { + JobModel jm = new JobModel(); + jm.setUuid(job.getUuid()); + jobBulkList.add(jm); + }); + return new JobBulk(jobBulkList); + } +} diff --git a/vid-app-common/src/test/java/org/onap/vid/services/CategoryParameterServiceImplTest.java b/vid-app-common/src/test/java/org/onap/vid/services/CategoryParameterServiceImplTest.java index 440a2a7bf..04ee46436 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/CategoryParameterServiceImplTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/CategoryParameterServiceImplTest.java @@ -1,80 +1,261 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ package org.onap.vid.services; -import org.junit.Test; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.ws.rs.ForbiddenException; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.onap.portalsdk.core.service.DataAccessService; import org.onap.vid.category.AddCategoryOptionResponse; import org.onap.vid.category.AddCategoryOptionsRequest; import org.onap.vid.category.CategoryParameterOptionRep; import org.onap.vid.category.CategoryParametersResponse; +import org.onap.vid.model.CategoryParameter; +import org.onap.vid.model.CategoryParameter.Family; import org.onap.vid.model.CategoryParameterOption; +import org.onap.vid.services.CategoryParameterServiceImpl.AlreadyExistOptionNameException; +import org.onap.vid.services.CategoryParameterServiceImpl.UnfoundedCategoryException; +import org.onap.vid.services.CategoryParameterServiceImpl.UnfoundedCategoryOptionException; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.Test; public class CategoryParameterServiceImplTest { - private CategoryParameterServiceImpl createTestSubject() { - return new CategoryParameterServiceImpl(); + private static final String CATEGORY_NAME = "SAMPLE_CATEGORY_NAME"; + private static final String OPTION_NAME = "SAMPLE_OPTION_NAME"; + private static final String UNIQUE_OPTION_NAME = "UNIQUE_OPTION_NAME"; + private static final String APP_ID_VID = "VID"; + private static final String APP_ID_SDC = "SDC"; + private static final String QUERY_STRING_FOR_CATEGORY_NAME = String.format(" where name = '%s' ", CATEGORY_NAME); + + + @Mock + private DataAccessService dataAccessService; + + @InjectMocks + private CategoryParameterServiceImpl testSubject; + + @BeforeSuite + public void before() { + initMocks(this); + } + + @BeforeMethod + public void resetMocks() { + reset(dataAccessService); } @Test - public void testCreateCategoryParameterOptions() throws Exception { - CategoryParameterServiceImpl testSubject; - String categoryName = ""; - AddCategoryOptionsRequest optionsRequest = null; - AddCategoryOptionResponse result; - - // default test - try { - testSubject = createTestSubject(); - result = testSubject.createCategoryParameterOptions(categoryName, optionsRequest); - } catch ( - - Exception e) { - } + public void createCategoryParameterOptions_happyPath() { + AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest(); + optionsRequest.options.add(UNIQUE_OPTION_NAME); + CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); + List<CategoryParameter> aList = createCategoryParametersList(categoryParameter); + + doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); + + AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest); + + Assert.assertTrue(result.getErrors().isEmpty());; + verify(dataAccessService, times(1)) + .saveDomainObject(anyObject(), anyObject()); } @Test - public void testDeleteCategoryOption() throws Exception { - CategoryParameterServiceImpl testSubject; - String categoryName = ""; + public void createCategoryParameterOptions_existingOptionsForCategory() { + AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest(); + optionsRequest.options.add(OPTION_NAME); + CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); + categoryParameter.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter)); + List<CategoryParameter> aList = createCategoryParametersList(categoryParameter); + + String expectedError = String.format(CategoryParameterServiceImpl.OPTION_ALREADY_EXIST_FOR_CATEGORY + , OPTION_NAME, CATEGORY_NAME); + + doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); + + AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest); + + Assert.assertFalse(result.getErrors().isEmpty()); + Assert.assertEquals(result.getErrors().size(), 1); + Assert.assertTrue(result.getErrors().stream().allMatch(expectedError::equals)); + } + + private List<CategoryParameter> createCategoryParametersList(CategoryParameter categoryParameter) { + List<CategoryParameter> aList = new ArrayList<>(); + aList.add(categoryParameter); + return aList; + } + + @Test + public void createCategoryParameterOptions_nonExistingOptionsForCategory() { + AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest(); + + List<CategoryParameter> aList = createCategoryParametersList(new CategoryParameter()); + doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); + + AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest); + + Assert.assertTrue(result.getErrors().isEmpty()); + } + + @Test(expectedExceptions = { UnfoundedCategoryException.class }) + public void createCategoryParameterOptions_wrongNumberOfCategoryParameters() { + AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest(); + List<CategoryParameter> aList = Collections.emptyList(); + + doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); + + AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest); + + Assert.fail(); + } + + @Test(expectedExceptions = { UnfoundedCategoryException.class }) + public void deleteCategoryOption_wrongNumberOfParameters() { CategoryParameterOption option = null; + List<CategoryParameter> aList = Collections.emptyList(); + + doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); + + testSubject.deleteCategoryOption(CATEGORY_NAME, option); - // default test - try { - testSubject = createTestSubject(); - testSubject.deleteCategoryOption(categoryName, option); - } catch ( + Assert.fail(); + } + + @Test + public void deleteCategoryOption_happyPath() { + CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); + CategoryParameterOption categoryParameterOption = + new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter); + categoryParameter.getOptions().add(categoryParameterOption); + List<CategoryParameter> aList = createCategoryParametersList(categoryParameter); + + doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject()); - Exception e) { - } + testSubject.deleteCategoryOption(CATEGORY_NAME, categoryParameterOption); + + verify(dataAccessService, times(1)) + .deleteDomainObject(anyObject(), anyObject()); } @Test - public void testGetCategoryParameters() throws Exception { - CategoryParameterServiceImpl testSubject; - CategoryParametersResponse result; - - // default test - try { - testSubject = createTestSubject(); - testSubject.getCategoryParameters(null); - } catch ( - - Exception e) { - } + public void getCategoryParametersTest() { + CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); + CategoryParameterOption categoryParameterOption = + new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter); + categoryParameter.getOptions().add(categoryParameterOption); + List<CategoryParameter> aList = createCategoryParametersList(categoryParameter); + + doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject()); + + CategoryParametersResponse response = testSubject.getCategoryParameters(Family.PARAMETER_STANDARDIZATION); + + Assert.assertFalse(response.getCategoryParameters().isEmpty()); + Assert.assertTrue(response.getCategoryParameters().containsKey(CATEGORY_NAME)); + + verify(dataAccessService, times(1)) + .getList(anyObject(), anyString(), anyString(), anyObject()); } @Test - public void testUpdateCategoryParameterOption() throws Exception { - CategoryParameterServiceImpl testSubject; - String categoryName = ""; - CategoryParameterOptionRep option = null; - AddCategoryOptionResponse result; - - // default test - try { - testSubject = createTestSubject(); - result = testSubject.updateCategoryParameterOption(categoryName, option); - } catch ( - - Exception e) { - } + public void updateCategoryParameterOption_domainObjectGetsSavedSuccessfully() { + CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME); + CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); + categoryParameter.getOptions().add( + new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, categoryParameter)); + List<CategoryParameter> aList = createCategoryParametersList(categoryParameter); + + doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); + + AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting); + + verify(dataAccessService, times(1)) + .saveDomainObject(anyObject(), anyObject()); + } + + @Test(expectedExceptions = { ForbiddenException.class }) + public void updateCategoryParameterOption_shouldFailUpdateForbidden() { + CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("1", CATEGORY_NAME); + CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, false); + categoryParameter.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter)); + List<CategoryParameter> aList = createCategoryParametersList(categoryParameter); + + doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); + + AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep); + + Assert.fail(); + } + + @Test(expectedExceptions = { UnfoundedCategoryOptionException.class }) + public void updateCategoryParameterOption_CategoryNotFound() { + CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("SOME_UNRELATED_ID", CATEGORY_NAME); + + CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); + categoryParameter.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter)); + List<CategoryParameter> aList = createCategoryParametersList(categoryParameter); + + doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); + + AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep); + + Assert.fail(); + } + + @Test(expectedExceptions = { AlreadyExistOptionNameException.class }) + public void updateCategoryParameterOption_OptionNameExists() { + CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME); + + CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); + categoryParameter.getOptions().add( + new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, categoryParameter)); + categoryParameter.getOptions().add( + new CategoryParameterOption(APP_ID_SDC, OPTION_NAME, categoryParameter)); + List<CategoryParameter> aList = createCategoryParametersList(categoryParameter); + + doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); + + AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting); + + Assert.fail(); + } + + private CategoryParameter createCategoryParameter(String categoryName, boolean idSupported) { + CategoryParameter categoryParameter = new CategoryParameter(); + categoryParameter.setName(categoryName); + categoryParameter.setIdSupported(idSupported); + return categoryParameter; } }
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/services/PombaServiceImplTest.java b/vid-app-common/src/test/java/org/onap/vid/services/PombaServiceImplTest.java new file mode 100644 index 000000000..d0eec2619 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/services/PombaServiceImplTest.java @@ -0,0 +1,40 @@ +package org.onap.vid.services; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.MockitoAnnotations.initMocks; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.vid.aai.PombaClientInterface; +import org.onap.vid.model.PombaInstance.PombaRequest; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class PombaServiceImplTest { + + @Mock + private PombaClientInterface pombaClientInterface; + + @InjectMocks + private PombaServiceImpl testSubject; + + @BeforeClass + public void beforeClass() { + initMocks(this); + } + + @BeforeMethod + public void resetMocks() { + Mockito.reset(pombaClientInterface); + } + + @Test + public void testVerify() { + PombaRequest pombaRequest = new PombaRequest(); + testSubject.verify(pombaRequest); + verify(pombaClientInterface, times(1)) + .verify(pombaRequest); + } +}
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/services/RoleGenaratorServiceImplTest.java b/vid-app-common/src/test/java/org/onap/vid/services/RoleGenaratorServiceImplTest.java index 544a799a0..d7db4955c 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/RoleGenaratorServiceImplTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/RoleGenaratorServiceImplTest.java @@ -1,25 +1,139 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ package org.onap.vid.services; -import static org.junit.Assert.*; -import java.util.*; -import org.junit.Assert; -import org.junit.Test; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.ArrayList; +import org.apache.commons.lang.StringUtils; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.vid.aai.AaiClientInterface; +import org.onap.vid.aai.AaiResponse; +import org.onap.vid.aai.ServiceSubscription; +import org.onap.vid.aai.ServiceSubscriptions; +import org.onap.vid.aai.Services; +import org.onap.vid.model.Subscriber; +import org.onap.vid.model.SubscriberList; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; public class RoleGenaratorServiceImplTest { - private RoleGenaratorServiceImpl createTestSubject() { - return new RoleGenaratorServiceImpl(); + private static final String GLOBAL_CUSTOMER_ID = "997"; + private static final String SUBSCRIBER_NAME = "name"; + private static final String SUBSCRIBER_TYPE = "subscriber_type"; + private static final String RESOURCE_VERSION = "1"; + private static final String SERVICE_TYPE = "service_type"; + + @Mock + private AaiClientInterface aaiClientInterface; + + @InjectMocks + private RoleGenaratorServiceImpl testSubject; + + @BeforeClass + public void beforeClass() { + initMocks(this); + } + + @BeforeMethod + public void resetMocks() { + Mockito.reset(aaiClientInterface); + } + + @Test + public void tenerateRoleScript_firstRun() { + boolean firstRun = true; + + Subscriber subscriber = createSubscriber(); + AaiResponse<SubscriberList> subscribers = createSubscriberListAaiResponse(subscriber); + doReturn(subscribers).when(aaiClientInterface).getAllSubscribers(); + + ServiceSubscription serviceSubscription = createServiceSubscription(); + AaiResponse<Services> subscriberResponse = createServicesAaiResponse(serviceSubscription); + doReturn(subscriberResponse).when(aaiClientInterface).getSubscriberData(subscriber.globalCustomerId); + + String result = testSubject.generateRoleScript(firstRun); + Assert.assertTrue(StringUtils.isNotBlank(result)); } @Test - public void testGenerateRoleScript() throws Exception { - RoleGenaratorServiceImpl testSubject; - Boolean firstRun = null; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.generateRoleScript(firstRun); + public void tenerateRoleScript_notAFirstRun() { + boolean firstRun = false; + + Subscriber subscriber = createSubscriber(); + AaiResponse<SubscriberList> subscribers = createSubscriberListAaiResponse(subscriber); + doReturn(subscribers).when(aaiClientInterface).getAllSubscribers(); + + ServiceSubscription serviceSubscription = createServiceSubscription(); + AaiResponse<Services> subscriberResponse = createServicesAaiResponse(serviceSubscription); + doReturn(subscriberResponse).when(aaiClientInterface).getSubscriberData(subscriber.globalCustomerId); + + String result = testSubject.generateRoleScript(firstRun); + Assert.assertTrue(StringUtils.isNotBlank(result)); + } + + @Test(expectedExceptions = { Exception.class }) + public void tenerateRoleScript_errorGettingDataFromAAIClient() { + boolean firstRun = false; + + doThrow(new Exception("This is expected.")).when(aaiClientInterface).getAllSubscribers(); + + String result = testSubject.generateRoleScript(firstRun); + Assert.fail(); + } + + private ServiceSubscription createServiceSubscription() { + ServiceSubscription serviceSubscription = new ServiceSubscription(); + serviceSubscription.serviceType = SERVICE_TYPE; + return serviceSubscription; + } + + private AaiResponse<SubscriberList> createSubscriberListAaiResponse(Subscriber subscriber) { + AaiResponse<SubscriberList> subscribers = new AaiResponse<>(new SubscriberList(new ArrayList<>()), "", 200); + subscribers.getT().customer.add(subscriber); + return subscribers; + } + + private AaiResponse<Services> createServicesAaiResponse(ServiceSubscription serviceSubscription) { + AaiResponse<Services> subscriberResponse = new AaiResponse<>(new Services(), "", 200); + subscriberResponse.getT().serviceSubscriptions = new ServiceSubscriptions(); + subscriberResponse.getT().serviceSubscriptions.serviceSubscription = new ArrayList<>(); + subscriberResponse.getT().serviceSubscriptions.serviceSubscription.add(serviceSubscription); + return subscriberResponse; + } + + private Subscriber createSubscriber() { + Subscriber subscriber = new Subscriber(); + subscriber.globalCustomerId = GLOBAL_CUSTOMER_ID; + subscriber.subscriberName = SUBSCRIBER_NAME; + subscriber.subscriberType = SUBSCRIBER_TYPE; + subscriber.resourceVersion = RESOURCE_VERSION; + return subscriber; } |