From: Fiete Ostkamp Date: Tue, 26 Mar 2024 10:28:49 +0000 (+0100) Subject: Use more dependency injection in model-loader X-Git-Tag: 1.13.6~4 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Fmodel-loader.git;a=commitdiff_plain;h=19f034b2554895285f12979b0f36c1629f9af984 Use more dependency injection in model-loader - use more dependency injection - make class variables final if possible - simplify mock creation in some tests using @Mock Issue-ID: AAI-3811 Change-Id: I7a7ccba02df78e6fd3bf082c23aac0968137661b Signed-off-by: Fiete Ostkamp --- diff --git a/src/main/java/org/onap/aai/modelloader/config/BeanConfig.java b/src/main/java/org/onap/aai/modelloader/config/BeanConfig.java index 2d7775d..8f7b2bb 100644 --- a/src/main/java/org/onap/aai/modelloader/config/BeanConfig.java +++ b/src/main/java/org/onap/aai/modelloader/config/BeanConfig.java @@ -27,8 +27,6 @@ import java.util.Properties; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; -import org.onap.aai.modelloader.notification.EventCallback; -import org.onap.aai.modelloader.service.BabelServiceClientFactory; import org.onap.aai.modelloader.service.ModelLoaderMsgs; import org.onap.sdc.api.IDistributionClient; import org.onap.sdc.impl.DistributionClientFactory; @@ -60,9 +58,4 @@ public class BeanConfig { public IDistributionClient iDistributionClient() { return DistributionClientFactory.createDistributionClient(); } - - @Bean - public EventCallback eventCallback(IDistributionClient client, ModelLoaderConfig config, BabelServiceClientFactory babelClientFactory) { - return new EventCallback(client, config, babelClientFactory); - } } diff --git a/src/main/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandler.java b/src/main/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandler.java index c54d7b2..c1f6e9b 100644 --- a/src/main/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandler.java +++ b/src/main/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandler.java @@ -44,6 +44,7 @@ import org.onap.aai.modelloader.entity.ArtifactHandler; import org.onap.aai.modelloader.restclient.AaiRestClient; import org.onap.aai.modelloader.service.ModelLoaderMsgs; import org.onap.aai.restclient.client.OperationResult; +import org.springframework.stereotype.Component; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -53,6 +54,7 @@ import org.xml.sax.InputSource; /** * VNF Catalog specific handling */ +@Component public class VnfCatalogArtifactHandler extends ArtifactHandler { private static Logger logger = LoggerFactory.getInstance().getLogger(VnfCatalogArtifactHandler.class.getName()); diff --git a/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactHandler.java b/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactHandler.java index 626ca49..7aa2191 100644 --- a/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactHandler.java +++ b/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactHandler.java @@ -28,7 +28,9 @@ import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.entity.ArtifactHandler; import org.onap.aai.modelloader.restclient.AaiRestClient; import org.onap.aai.modelloader.service.ModelLoaderMsgs; +import org.springframework.stereotype.Service; +@Service public class ModelArtifactHandler extends ArtifactHandler { private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactHandler.class.getName()); diff --git a/src/main/java/org/onap/aai/modelloader/extraction/VnfCatalogExtractor.java b/src/main/java/org/onap/aai/modelloader/extraction/VnfCatalogExtractor.java index 3dbdfcc..64ecf8a 100644 --- a/src/main/java/org/onap/aai/modelloader/extraction/VnfCatalogExtractor.java +++ b/src/main/java/org/onap/aai/modelloader/extraction/VnfCatalogExtractor.java @@ -38,6 +38,7 @@ import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.entity.ArtifactType; import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact; import org.onap.aai.modelloader.service.ModelLoaderMsgs; +import org.springframework.stereotype.Component; /** @@ -47,6 +48,7 @@ import org.onap.aai.modelloader.service.ModelLoaderMsgs; * A .csar file is a compressed archive like a zip file and this class will treat the byte array as it if were a zip * file. */ +@Component public class VnfCatalogExtractor { private static final Logger logger = LoggerFactory.getInstance().getLogger(VnfCatalogExtractor.class.getName()); diff --git a/src/main/java/org/onap/aai/modelloader/notification/ArtifactDownloadManager.java b/src/main/java/org/onap/aai/modelloader/notification/ArtifactDownloadManager.java index 58bb074..dcec799 100644 --- a/src/main/java/org/onap/aai/modelloader/notification/ArtifactDownloadManager.java +++ b/src/main/java/org/onap/aai/modelloader/notification/ArtifactDownloadManager.java @@ -49,6 +49,7 @@ import org.onap.sdc.api.notification.INotificationData; import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.utils.ArtifactTypeEnum; import org.onap.sdc.utils.DistributionActionResultEnum; +import org.springframework.stereotype.Component; /** * This class is responsible for downloading the artifacts from the ASDC. @@ -60,22 +61,26 @@ import org.onap.sdc.utils.DistributionActionResultEnum; * * TOSCA_CSAR file artifacts will be converted into XML and returned as model artifacts. */ +@Component public class ArtifactDownloadManager { private static Logger logger = LoggerFactory.getInstance().getLogger(ArtifactDownloadManager.class); - private IDistributionClient client; - private NotificationPublisher notificationPublisher; - private BabelArtifactConverter babelArtifactConverter; - private ModelLoaderConfig config; - private BabelServiceClientFactory clientFactory; - private VnfCatalogExtractor vnfCatalogExtractor; + private final IDistributionClient client; + private final NotificationPublisher notificationPublisher; + private final BabelArtifactConverter babelArtifactConverter; + private final ModelLoaderConfig config; + private final BabelServiceClientFactory clientFactory; + private final VnfCatalogExtractor vnfCatalogExtractor; public ArtifactDownloadManager(IDistributionClient client, ModelLoaderConfig config, - BabelServiceClientFactory clientFactory) { + BabelServiceClientFactory clientFactory, BabelArtifactConverter babelArtifactConverter, NotificationPublisher notificationPublisher, VnfCatalogExtractor vnfCatalogExtractor) { this.client = client; + this.notificationPublisher = notificationPublisher; + this.babelArtifactConverter = babelArtifactConverter; this.config = config; this.clientFactory = clientFactory; + this.vnfCatalogExtractor = vnfCatalogExtractor; } /** @@ -96,10 +101,10 @@ public class ArtifactDownloadManager { IDistributionClientDownloadResult downloadResult = downloadIndividualArtifacts(data, artifact); processDownloadedArtifacts(modelArtifacts, catalogArtifacts, artifact, downloadResult, data); } catch (DownloadFailureException e) { - getNotificationPublisher().publishDownloadFailure(client, data, artifact, e.getMessage()); + notificationPublisher.publishDownloadFailure(client, data, artifact, e.getMessage()); success = false; } catch (Exception e) { - getNotificationPublisher().publishDeployFailure(client, data, artifact); + notificationPublisher.publishDeployFailure(client, data, artifact); success = false; } @@ -126,7 +131,7 @@ public class ArtifactDownloadManager { if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult())) { logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Downloaded artifact: " + artifact.getArtifactName()); - getNotificationPublisher().publishDownloadSuccess(client, data, artifact); + notificationPublisher.publishDownloadSuccess(client, data, artifact); } else { throw new DownloadFailureException(downloadResult.getDistributionMessageResult()); } @@ -156,7 +161,7 @@ public class ArtifactDownloadManager { invokeBabelService(modelArtifacts, catalogArtifacts, payload, artifactInfo, distributionId, serviceVersion); // Get VNF Catalog artifacts directly from CSAR - List csarCatalogArtifacts = getVnfCatalogExtractor().extract(payload, artifactInfo.getArtifactName()); + List csarCatalogArtifacts = vnfCatalogExtractor.extract(payload, artifactInfo.getArtifactName()); // Throw an error if VNF Catalog data is present in the Babel payload and directly in the CSAR if (!catalogArtifacts.isEmpty() && !csarCatalogArtifacts.isEmpty()) { @@ -187,12 +192,12 @@ public class ArtifactDownloadManager { if (artifactMap.containsKey(BabelArtifact.ArtifactType.MODEL)) { modelArtifacts.addAll( - getBabelArtifactConverter().convertToModel(artifactMap.get(BabelArtifact.ArtifactType.MODEL))); + babelArtifactConverter.convertToModel(artifactMap.get(BabelArtifact.ArtifactType.MODEL))); artifactMap.remove(BabelArtifact.ArtifactType.MODEL); } if (artifactMap.containsKey(BabelArtifact.ArtifactType.VNFCATALOG)) { - catalogArtifacts.addAll(getBabelArtifactConverter() + catalogArtifacts.addAll(babelArtifactConverter .convertToCatalog(artifactMap.get(BabelArtifact.ArtifactType.VNFCATALOG))); artifactMap.remove(BabelArtifact.ArtifactType.VNFCATALOG); } @@ -257,28 +262,4 @@ public class ArtifactDownloadManager { private boolean parsedArtifactsExist(List parsedArtifacts) { return parsedArtifacts != null && !parsedArtifacts.isEmpty(); } - - private NotificationPublisher getNotificationPublisher() { - if (notificationPublisher == null) { - notificationPublisher = new NotificationPublisher(); - } - - return notificationPublisher; - } - - private BabelArtifactConverter getBabelArtifactConverter() { - if (babelArtifactConverter == null) { - babelArtifactConverter = new BabelArtifactConverter(); - } - - return babelArtifactConverter; - } - - private VnfCatalogExtractor getVnfCatalogExtractor() { - if (vnfCatalogExtractor == null) { - vnfCatalogExtractor = new VnfCatalogExtractor(); - } - - return vnfCatalogExtractor; - } } diff --git a/src/main/java/org/onap/aai/modelloader/notification/BabelArtifactConverter.java b/src/main/java/org/onap/aai/modelloader/notification/BabelArtifactConverter.java index ad4eb7d..480a461 100644 --- a/src/main/java/org/onap/aai/modelloader/notification/BabelArtifactConverter.java +++ b/src/main/java/org/onap/aai/modelloader/notification/BabelArtifactConverter.java @@ -29,12 +29,14 @@ import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact; import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; import org.onap.aai.modelloader.entity.model.ModelArtifact; import org.onap.aai.modelloader.entity.model.ModelArtifactParser; +import org.springframework.stereotype.Component; /** * This class is responsible for converting TOSCA artifacts into instances of {@link ModelArtifact} ready for pushing * the converted artifacts . */ -class BabelArtifactConverter { +@Component +public class BabelArtifactConverter { /** * This method converts BabelArtifacts into instances of {@link ModelArtifact}. diff --git a/src/main/java/org/onap/aai/modelloader/notification/EventCallback.java b/src/main/java/org/onap/aai/modelloader/notification/EventCallback.java index d7bccba..754eaff 100644 --- a/src/main/java/org/onap/aai/modelloader/notification/EventCallback.java +++ b/src/main/java/org/onap/aai/modelloader/notification/EventCallback.java @@ -26,33 +26,32 @@ import java.util.List; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.cl.mdc.MdcContext; -import org.onap.aai.modelloader.config.ModelLoaderConfig; import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.extraction.ArtifactInfoExtractor; import org.onap.aai.modelloader.service.ArtifactDeploymentManager; -import org.onap.aai.modelloader.service.BabelServiceClientFactory; import org.onap.aai.modelloader.service.ModelLoaderMsgs; import org.onap.sdc.api.IDistributionClient; import org.onap.sdc.api.consumer.INotificationCallback; import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.INotificationData; import org.slf4j.MDC; +import org.springframework.stereotype.Component; +@Component public class EventCallback implements INotificationCallback { private static Logger logger = LoggerFactory.getInstance().getLogger(EventCallback.class.getName()); private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(EventCallback.class.getName()); - private ArtifactDeploymentManager artifactDeploymentManager; - private ArtifactDownloadManager artifactDownloadManager; - private NotificationPublisher notificationPublisher; - private IDistributionClient client; - private ModelLoaderConfig config; - private BabelServiceClientFactory babelServiceClientFactory; + private final ArtifactDeploymentManager artifactDeploymentManager; + private final ArtifactDownloadManager artifactDownloadManager; + private final NotificationPublisher notificationPublisher; + private final IDistributionClient client; - public EventCallback(IDistributionClient client, ModelLoaderConfig config, BabelServiceClientFactory babelServiceClientFactory) { + public EventCallback(IDistributionClient client, ArtifactDeploymentManager artifactDeploymentManager, ArtifactDownloadManager artifactDownloadManager, NotificationPublisher notificationPublisher) { + this.artifactDeploymentManager = artifactDeploymentManager; + this.artifactDownloadManager = artifactDownloadManager; + this.notificationPublisher = notificationPublisher; this.client = client; - this.config = config; - this.babelServiceClientFactory = babelServiceClientFactory; } @Override @@ -65,10 +64,10 @@ public class EventCallback implements INotificationCallback { List modelArtifacts = new ArrayList<>(); boolean success = - getArtifactDownloadManager().downloadArtifacts(data, artifacts, modelArtifacts, catalogArtifacts); + artifactDownloadManager.downloadArtifacts(data, artifacts, modelArtifacts, catalogArtifacts); if (success) { - success = getArtifactDeploymentManager().deploy(data, modelArtifacts, catalogArtifacts); + success = artifactDeploymentManager.deploy(data, modelArtifacts, catalogArtifacts); } String statusString = success ? "SUCCESS" : "FAILURE"; @@ -85,37 +84,12 @@ public class EventCallback implements INotificationCallback { boolean deploymentSuccess) { if (deploymentSuccess) { artifacts.stream().filter(a -> filterType.equalsIgnoreCase(a.getArtifactType())) - .forEach(a -> getNotificationPublisher().publishDeploySuccess(client, data, a)); - getNotificationPublisher().publishComponentSuccess(client, data); + .forEach(a -> notificationPublisher.publishDeploySuccess(client, data, a)); + notificationPublisher.publishComponentSuccess(client, data); } else { artifacts.stream().filter(a -> filterType.equalsIgnoreCase(a.getArtifactType())) - .forEach(a -> getNotificationPublisher().publishDeployFailure(client, data, a)); - getNotificationPublisher().publishComponentFailure(client, data, "deploy failure"); + .forEach(a -> notificationPublisher.publishDeployFailure(client, data, a)); + notificationPublisher.publishComponentFailure(client, data, "deploy failure"); } } - - private ArtifactDeploymentManager getArtifactDeploymentManager() { - if (artifactDeploymentManager == null) { - artifactDeploymentManager = new ArtifactDeploymentManager(config); - } - - return artifactDeploymentManager; - } - - private ArtifactDownloadManager getArtifactDownloadManager() { - if (artifactDownloadManager == null) { - artifactDownloadManager = new ArtifactDownloadManager(client, config, babelServiceClientFactory); - } - - return artifactDownloadManager; - } - - - private NotificationPublisher getNotificationPublisher() { - if (notificationPublisher == null) { - notificationPublisher = new NotificationPublisher(); - } - - return notificationPublisher; - } } diff --git a/src/main/java/org/onap/aai/modelloader/notification/NotificationPublisher.java b/src/main/java/org/onap/aai/modelloader/notification/NotificationPublisher.java index 6e6ff13..7d5b1cc 100644 --- a/src/main/java/org/onap/aai/modelloader/notification/NotificationPublisher.java +++ b/src/main/java/org/onap/aai/modelloader/notification/NotificationPublisher.java @@ -38,10 +38,12 @@ import org.onap.sdc.api.notification.INotificationData; import org.onap.sdc.api.results.IDistributionClientResult; import org.onap.sdc.utils.DistributionActionResultEnum; import org.onap.sdc.utils.DistributionStatusEnum; +import org.springframework.stereotype.Component; /** * This class is responsible for publishing the status of actions performed working with artifacts. */ +@Component public class NotificationPublisher { private static Logger logger = LoggerFactory.getInstance().getLogger(NotificationPublisher.class); diff --git a/src/main/java/org/onap/aai/modelloader/restclient/HttpsBabelServiceClient.java b/src/main/java/org/onap/aai/modelloader/restclient/HttpsBabelServiceClient.java index 289015c..88967b2 100644 --- a/src/main/java/org/onap/aai/modelloader/restclient/HttpsBabelServiceClient.java +++ b/src/main/java/org/onap/aai/modelloader/restclient/HttpsBabelServiceClient.java @@ -224,7 +224,8 @@ public class HttpsBabelServiceClient implements BabelServiceClient { MdcOverride override = new MdcOverride(); override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter)); - WebResource webResource = client.resource(config.getBabelBaseUrl() + config.getBabelGenerateArtifactsUrl()); + String resourceUrl = config.getBabelBaseUrl() + config.getBabelGenerateArtifactsUrl(); + WebResource webResource = client.resource(resourceUrl); ClientResponse response = webResource.type("application/json") .header(AaiRestClient.HEADER_TRANS_ID, Collections.singletonList(transactionId)) .header(AaiRestClient.HEADER_FROM_APP_ID, Collections.singletonList(AaiRestClient.ML_APP_NAME)) diff --git a/src/main/java/org/onap/aai/modelloader/service/ArtifactDeploymentManager.java b/src/main/java/org/onap/aai/modelloader/service/ArtifactDeploymentManager.java index af006f5..9f09703 100644 --- a/src/main/java/org/onap/aai/modelloader/service/ArtifactDeploymentManager.java +++ b/src/main/java/org/onap/aai/modelloader/service/ArtifactDeploymentManager.java @@ -28,18 +28,22 @@ import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifactHandler; import org.onap.aai.modelloader.entity.model.ModelArtifactHandler; import org.onap.aai.modelloader.restclient.AaiRestClient; import org.onap.sdc.api.notification.INotificationData; +import org.springframework.stereotype.Component; /** * This class is responsible for deploying model and catalog artifacts. */ +@Component public class ArtifactDeploymentManager { - private ModelLoaderConfig config; - private ModelArtifactHandler modelArtifactHandler; - private VnfCatalogArtifactHandler vnfCatalogArtifactHandler; + private final ModelLoaderConfig config; + private final ModelArtifactHandler modelArtifactHandler; + private final VnfCatalogArtifactHandler vnfCatalogArtifactHandler; - public ArtifactDeploymentManager(ModelLoaderConfig config) { + public ArtifactDeploymentManager(ModelLoaderConfig config, ModelArtifactHandler modelArtifactHandler, VnfCatalogArtifactHandler vnfCatalogArtifactHandler) { this.config = config; + this.modelArtifactHandler = modelArtifactHandler; + this.vnfCatalogArtifactHandler = vnfCatalogArtifactHandler; } /** @@ -59,36 +63,20 @@ public class ArtifactDeploymentManager { List completedArtifacts = new ArrayList<>(); boolean deploySuccess = - getModelArtifactHandler().pushArtifacts(modelArtifacts, distributionId, completedArtifacts, aaiClient); + modelArtifactHandler.pushArtifacts(modelArtifacts, distributionId, completedArtifacts, aaiClient); if (!deploySuccess) { - getModelArtifactHandler().rollback(completedArtifacts, distributionId, aaiClient); + modelArtifactHandler.rollback(completedArtifacts, distributionId, aaiClient); } else { List completedImageData = new ArrayList<>(); - deploySuccess = getVnfCatalogArtifactHandler().pushArtifacts(catalogArtifacts, distributionId, + deploySuccess = vnfCatalogArtifactHandler.pushArtifacts(catalogArtifacts, distributionId, completedImageData, aaiClient); if (!deploySuccess) { - getModelArtifactHandler().rollback(completedArtifacts, distributionId, aaiClient); - getVnfCatalogArtifactHandler().rollback(completedImageData, distributionId, aaiClient); + modelArtifactHandler.rollback(completedArtifacts, distributionId, aaiClient); + vnfCatalogArtifactHandler.rollback(completedImageData, distributionId, aaiClient); } } return deploySuccess; } - - private ModelArtifactHandler getModelArtifactHandler() { - if (modelArtifactHandler == null) { - modelArtifactHandler = new ModelArtifactHandler(config); - } - - return modelArtifactHandler; - } - - private VnfCatalogArtifactHandler getVnfCatalogArtifactHandler() { - if (vnfCatalogArtifactHandler == null) { - this.vnfCatalogArtifactHandler = new VnfCatalogArtifactHandler(config); - } - - return vnfCatalogArtifactHandler; - } } diff --git a/src/main/java/org/onap/aai/modelloader/service/ModelController.java b/src/main/java/org/onap/aai/modelloader/service/ModelController.java index 0ca2c02..0921982 100644 --- a/src/main/java/org/onap/aai/modelloader/service/ModelController.java +++ b/src/main/java/org/onap/aai/modelloader/service/ModelController.java @@ -55,19 +55,20 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("/services/model-loader/v1/model-service") public class ModelController implements ModelLoaderInterface { - private static final Logger logger = LoggerFactory.getInstance().getLogger(ModelController.class.getName()); + private static final Logger logger = LoggerFactory.getInstance().getLogger(ModelController.class); private final IDistributionClient client; private final ModelLoaderConfig config; private final EventCallback eventCallback; - private final BabelServiceClientFactory babelClientFactory; + private final ArtifactDeploymentManager artifactDeploymentManager; + private final ArtifactDownloadManager artifactDownloadManager; - public ModelController(IDistributionClient client, ModelLoaderConfig config, EventCallback eventCallback, - BabelServiceClientFactory babelClientFactory) { + public ModelController(IDistributionClient client, ModelLoaderConfig config, EventCallback eventCallback, ArtifactDeploymentManager artifactDeploymentManager, ArtifactDownloadManager artifactDownloadManager) { this.client = client; this.config = config; this.eventCallback = eventCallback; - this.babelClientFactory = babelClientFactory; + this.artifactDeploymentManager = artifactDeploymentManager; + this.artifactDownloadManager = artifactDownloadManager; } /** @@ -167,7 +168,7 @@ public class ModelController implements ModelLoaderInterface { List modelArtifacts = new ArrayList<>(); List catalogArtifacts = new ArrayList<>(); - new ArtifactDownloadManager(client, config, babelClientFactory).processToscaArtifacts(modelArtifacts, + artifactDownloadManager.processToscaArtifacts(modelArtifacts, catalogArtifacts, csarFile, artifactInfo, "test-transaction-id", modelVersion); logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Loading xml models from test artifacts: " @@ -176,7 +177,7 @@ public class ModelController implements ModelLoaderInterface { NotificationDataImpl notificationData = new NotificationDataImpl(); notificationData.setDistributionID("TestDistributionID"); boolean success = - new ArtifactDeploymentManager(config).deploy(notificationData, modelArtifacts, catalogArtifacts); + artifactDeploymentManager.deploy(notificationData, modelArtifacts, catalogArtifacts); logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Deployment success was " + success); response = success ? Response.ok().build() : Response.serverError().build(); } catch (Exception e) { diff --git a/src/main/java/org/onap/aai/modelloader/service/SdcConnectionJob.java b/src/main/java/org/onap/aai/modelloader/service/SdcConnectionJob.java index 9a6c6e9..79cf538 100644 --- a/src/main/java/org/onap/aai/modelloader/service/SdcConnectionJob.java +++ b/src/main/java/org/onap/aai/modelloader/service/SdcConnectionJob.java @@ -34,12 +34,11 @@ public class SdcConnectionJob extends TimerTask { private static final Logger logger = LoggerFactory.getInstance().getLogger(SdcConnectionJob.class.getName()); private final IDistributionClient client; - private ModelLoaderConfig config; - private EventCallback callback; - private Timer timer; + private final ModelLoaderConfig config; + private final EventCallback callback; + private final Timer timer; public SdcConnectionJob(IDistributionClient client, ModelLoaderConfig config, EventCallback callback, Timer timer) { - super(); this.client = client; this.timer = timer; this.callback = callback; diff --git a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java index 1b8f33b..aa2d299 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java +++ b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java @@ -25,7 +25,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile; @@ -36,7 +35,9 @@ import java.util.Properties; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.aai.modelloader.config.ModelLoaderConfig; import org.onap.aai.modelloader.entity.Artifact; @@ -56,40 +57,29 @@ import org.onap.sdc.api.notification.INotificationData; import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.impl.DistributionClientDownloadResultImpl; import org.onap.sdc.utils.DistributionActionResultEnum; -import org.springframework.test.util.ReflectionTestUtils; /** * Tests {@link ArtifactDownloadManager} with VNF Catalog Artifacts. */ public class ArtifactDownloadManagerVnfcTest { - private ArtifactDownloadManager downloadManager; - private BabelServiceClient mockBabelClient; - private IDistributionClient mockDistributionClient; - private NotificationPublisher mockNotificationPublisher; - private BabelArtifactConverter mockBabelArtifactConverter; - private BabelServiceClientFactory mockClientFactory; - private VnfCatalogExtractor mockVnfCatalogExtractor; + @Mock private ArtifactDownloadManager downloadManager; + @Mock private BabelServiceClient mockBabelClient; + @Mock private IDistributionClient mockDistributionClient; + @Mock private NotificationPublisher mockNotificationPublisher; + @Mock private BabelArtifactConverter mockBabelArtifactConverter; + @Mock private BabelServiceClientFactory mockClientFactory; + @Mock private VnfCatalogExtractor mockVnfCatalogExtractor; @BeforeEach public void setup() throws Exception { - mockBabelClient = mock(BabelServiceClient.class); - mockDistributionClient = mock(IDistributionClient.class); - mockNotificationPublisher = mock(NotificationPublisher.class); - mockBabelArtifactConverter = mock(BabelArtifactConverter.class); - mockClientFactory = mock(BabelServiceClientFactory.class); + MockitoAnnotations.openMocks(this); when(mockClientFactory.create(Mockito.any())).thenReturn(mockBabelClient); - mockVnfCatalogExtractor = mock(VnfCatalogExtractor.class); Properties configProperties = new Properties(); configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties")); downloadManager = new ArtifactDownloadManager(mockDistributionClient, - new ModelLoaderConfig(configProperties, "."), mockClientFactory); - - - ReflectionTestUtils.setField(downloadManager, "notificationPublisher", mockNotificationPublisher); - ReflectionTestUtils.setField(downloadManager, "babelArtifactConverter", mockBabelArtifactConverter); - ReflectionTestUtils.setField(downloadManager, "vnfCatalogExtractor", mockVnfCatalogExtractor); + new ModelLoaderConfig(configProperties, "."), mockClientFactory, mockBabelArtifactConverter, mockNotificationPublisher, mockVnfCatalogExtractor); } @Test diff --git a/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDeploymentManager.java b/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDeploymentManager.java index 2cac3c9..dfced1a 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDeploymentManager.java +++ b/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDeploymentManager.java @@ -24,7 +24,6 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithCatalogFile; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneOfEach; @@ -37,7 +36,9 @@ import java.util.Properties; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.aai.modelloader.config.ModelLoaderConfig; import org.onap.aai.modelloader.entity.Artifact; @@ -50,7 +51,6 @@ import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder; import org.onap.aai.modelloader.service.ArtifactDeploymentManager; import org.onap.aai.modelloader.util.ArtifactTestUtils; import org.onap.sdc.api.notification.INotificationData; -import org.springframework.test.util.ReflectionTestUtils; /** * Tests {@link ArtifactDeploymentManager}. @@ -63,28 +63,24 @@ public class TestArtifactDeploymentManager { private Properties configProperties; private ArtifactDeploymentManager manager; - private ModelArtifactHandler mockModelArtifactHandler; - private VnfCatalogArtifactHandler mockVnfCatalogArtifactHandler; + @Mock private ModelArtifactHandler modelArtifactHandlerMock; + @Mock private VnfCatalogArtifactHandler vnfCatalogArtifactHandlerMock; @BeforeEach public void setup() throws IOException { + MockitoAnnotations.openMocks(this); configProperties = new Properties(); configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); - mockModelArtifactHandler = mock(ModelArtifactHandler.class); - mockVnfCatalogArtifactHandler = mock(VnfCatalogArtifactHandler.class); - - manager = new ArtifactDeploymentManager(new ModelLoaderConfig(configProperties, null)); - - ReflectionTestUtils.setField(manager, "modelArtifactHandler", mockModelArtifactHandler); - ReflectionTestUtils.setField(manager, "vnfCatalogArtifactHandler", mockVnfCatalogArtifactHandler); + ModelLoaderConfig modelLoaderConfig = new ModelLoaderConfig(configProperties, null); + manager = new ArtifactDeploymentManager(modelLoaderConfig, modelArtifactHandlerMock, vnfCatalogArtifactHandlerMock); } @AfterEach public void tearDown() { configProperties = null; - mockModelArtifactHandler = null; - mockVnfCatalogArtifactHandler = null; + modelArtifactHandlerMock = null; + vnfCatalogArtifactHandlerMock = null; manager = null; } @@ -95,18 +91,18 @@ public class TestArtifactDeploymentManager { List toscaArtifacts = setupTest(xml, data); List modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); - when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) + when(modelArtifactHandlerMock.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) .thenReturn(false); assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, new ArrayList<>()), is(false)); - Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), + Mockito.verify(modelArtifactHandlerMock).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()); - Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).pushArtifacts(eq(modelArtifacts), + Mockito.verify(vnfCatalogArtifactHandlerMock, Mockito.never()).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()); - Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList()), eq(data.getDistributionID()), + Mockito.verify(modelArtifactHandlerMock).rollback(eq(new ArrayList()), eq(data.getDistributionID()), any()); - Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(eq(new ArrayList()), + Mockito.verify(vnfCatalogArtifactHandlerMock, Mockito.never()).rollback(eq(new ArrayList()), eq(data.getDistributionID()), any()); } @@ -129,19 +125,19 @@ public class TestArtifactDeploymentManager { List catalogFiles = new ArrayList<>(); catalogFiles.add(new VnfCatalogArtifact("Some catalog content")); - when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true); - when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any())) + when(modelArtifactHandlerMock.pushArtifacts(any(), any(), any(), any())).thenReturn(true); + when(vnfCatalogArtifactHandlerMock.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any())) .thenReturn(false); assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, new ArrayList<>(), catalogFiles), is(false)); - Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(new ArrayList()), + Mockito.verify(modelArtifactHandlerMock).pushArtifacts(eq(new ArrayList()), eq(data.getDistributionID()), any(), any()); - Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), + Mockito.verify(vnfCatalogArtifactHandlerMock).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()); - Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList()), eq(data.getDistributionID()), + Mockito.verify(modelArtifactHandlerMock).rollback(eq(new ArrayList()), eq(data.getDistributionID()), any()); - Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList()), + Mockito.verify(vnfCatalogArtifactHandlerMock).rollback(eq(new ArrayList()), eq(data.getDistributionID()), any()); } @@ -170,34 +166,34 @@ public class TestArtifactDeploymentManager { List catalogFiles = new ArrayList<>(); catalogFiles.add(new VnfCatalogArtifact("Some catalog content")); - when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any())) + when(vnfCatalogArtifactHandlerMock.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any())) .thenReturn(catalogsDeployed); - when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) + when(modelArtifactHandlerMock.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) .thenReturn(modelsDeployed); assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, catalogFiles), is(false)); // Catalog artifacts are only pushed if models are successful. - Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), + Mockito.verify(modelArtifactHandlerMock).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()); if (modelsDeployed) { - Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), + Mockito.verify(vnfCatalogArtifactHandlerMock).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()); } if (modelsDeployed && catalogsDeployed) { - Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any()); - Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any()); + Mockito.verify(modelArtifactHandlerMock, Mockito.never()).rollback(any(), any(), any()); + Mockito.verify(vnfCatalogArtifactHandlerMock, Mockito.never()).rollback(any(), any(), any()); } else { if (modelsDeployed) { - Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList()), + Mockito.verify(modelArtifactHandlerMock).rollback(eq(new ArrayList()), eq(data.getDistributionID()), any()); - Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList()), + Mockito.verify(vnfCatalogArtifactHandlerMock).rollback(eq(new ArrayList()), eq(data.getDistributionID()), any()); } else { - Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList()), + Mockito.verify(modelArtifactHandlerMock).rollback(eq(new ArrayList()), eq(data.getDistributionID()), any()); - Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any()); + Mockito.verify(vnfCatalogArtifactHandlerMock, Mockito.never()).rollback(any(), any(), any()); } } } @@ -219,18 +215,18 @@ public class TestArtifactDeploymentManager { List catalogFiles = new ArrayList<>(); catalogFiles.add(new VnfCatalogArtifact("Some catalog content")); - when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any())) + when(vnfCatalogArtifactHandlerMock.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any())) .thenReturn(true); - when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) + when(modelArtifactHandlerMock.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) .thenReturn(true); assertThat(manager.deploy(data, modelArtifacts, catalogFiles), is(true)); - Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), + Mockito.verify(vnfCatalogArtifactHandlerMock).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()); - Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), + Mockito.verify(modelArtifactHandlerMock).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()); - Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any()); - Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any()); + Mockito.verify(modelArtifactHandlerMock, Mockito.never()).rollback(any(), any(), any()); + Mockito.verify(vnfCatalogArtifactHandlerMock, Mockito.never()).rollback(any(), any(), any()); } } diff --git a/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java b/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java index 561791b..c09eff5 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java +++ b/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java @@ -44,15 +44,17 @@ import org.hamcrest.collection.IsEmptyCollection; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.aai.modelloader.config.ModelLoaderConfig; import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; +import org.onap.aai.modelloader.extraction.VnfCatalogExtractor; import org.onap.aai.modelloader.restclient.BabelServiceClient; import org.onap.aai.modelloader.restclient.BabelServiceClientException; import org.onap.aai.modelloader.service.BabelServiceClientFactory; -import org.onap.aai.modelloader.service.HttpsBabelServiceClientFactory; import org.onap.aai.modelloader.util.ArtifactTestUtils; import org.onap.sdc.api.IDistributionClient; import org.onap.sdc.api.notification.IArtifactInfo; @@ -60,7 +62,6 @@ import org.onap.sdc.api.notification.INotificationData; import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.impl.DistributionClientDownloadResultImpl; import org.onap.sdc.utils.DistributionActionResultEnum; -import org.springframework.test.util.ReflectionTestUtils; /** * Tests {@link ArtifactDownloadManager}. @@ -68,28 +69,23 @@ import org.springframework.test.util.ReflectionTestUtils; public class TestArtifactDownloadManager { private ArtifactDownloadManager downloadManager; - private BabelServiceClient mockBabelClient; - private IDistributionClient mockDistributionClient; - private NotificationPublisher mockNotificationPublisher; - private BabelArtifactConverter mockBabelArtifactConverter; - private BabelServiceClientFactory mockClientFactory; + @Mock private BabelServiceClient mockBabelClient; + @Mock private IDistributionClient mockDistributionClient; + @Mock private NotificationPublisher mockNotificationPublisher; + @Mock private BabelArtifactConverter mockBabelArtifactConverter; + @Mock private BabelServiceClientFactory mockClientFactory; + private VnfCatalogExtractor vnfCatalogExtractor; @BeforeEach public void setup() throws Exception { - mockBabelClient = mock(BabelServiceClient.class); - mockDistributionClient = mock(IDistributionClient.class); - mockNotificationPublisher = mock(NotificationPublisher.class); - mockBabelArtifactConverter = mock(BabelArtifactConverter.class); - mockClientFactory = mock(HttpsBabelServiceClientFactory.class); + MockitoAnnotations.openMocks(this); + vnfCatalogExtractor = new VnfCatalogExtractor(); when(mockClientFactory.create(any())).thenReturn(mockBabelClient); Properties configProperties = new Properties(); configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties")); downloadManager = new ArtifactDownloadManager(mockDistributionClient, - new ModelLoaderConfig(configProperties, "."), mockClientFactory); - - ReflectionTestUtils.setField(downloadManager, "notificationPublisher", mockNotificationPublisher); - ReflectionTestUtils.setField(downloadManager, "babelArtifactConverter", mockBabelArtifactConverter); + new ModelLoaderConfig(configProperties, "."), mockClientFactory, mockBabelArtifactConverter, mockNotificationPublisher, vnfCatalogExtractor); } @AfterEach diff --git a/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java b/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java index d9245d9..1073a61 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java +++ b/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java @@ -21,7 +21,6 @@ package org.onap.aai.modelloader.notification; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -32,14 +31,14 @@ import java.util.Properties; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.mockito.Mockito; -import org.onap.aai.modelloader.config.ModelLoaderConfig; +import org.mockito.MockitoAnnotations; import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder; import org.onap.aai.modelloader.service.ArtifactDeploymentManager; import org.onap.sdc.api.IDistributionClient; import org.onap.sdc.api.notification.INotificationData; -import org.springframework.test.util.ReflectionTestUtils; /** * Tests {@link EventCallback}. @@ -48,36 +47,25 @@ public class TestEventCallback { private static final String CONFIG_FILE = "model-loader.properties"; - private ModelLoaderConfig config; private Properties configProperties; private EventCallback eventCallback; - private ArtifactDeploymentManager mockArtifactDeploymentManager; - private ArtifactDownloadManager mockArtifactDownloadManager; - private IDistributionClient mockDistributionClient; - private NotificationPublisher mockNotificationPublisher; + @Mock private ArtifactDeploymentManager mockArtifactDeploymentManager; + @Mock private ArtifactDownloadManager mockArtifactDownloadManager; + @Mock private IDistributionClient mockDistributionClient; + @Mock private NotificationPublisher mockNotificationPublisher; @BeforeEach public void setup() throws IOException { + MockitoAnnotations.openMocks(this); configProperties = new Properties(); configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); - config = new ModelLoaderConfig(configProperties, null); - mockArtifactDeploymentManager = mock(ArtifactDeploymentManager.class); - mockArtifactDownloadManager = mock(ArtifactDownloadManager.class); - mockDistributionClient = mock(IDistributionClient.class); - mockNotificationPublisher = mock(NotificationPublisher.class); - - eventCallback = new EventCallback(mockDistributionClient, config, null); - - ReflectionTestUtils.setField(eventCallback, "artifactDeploymentManager", mockArtifactDeploymentManager); - ReflectionTestUtils.setField(eventCallback, "artifactDownloadManager", mockArtifactDownloadManager); - ReflectionTestUtils.setField(eventCallback, "notificationPublisher", mockNotificationPublisher); + eventCallback = new EventCallback(mockDistributionClient, mockArtifactDeploymentManager, mockArtifactDownloadManager, mockNotificationPublisher); } @AfterEach public void tearDown() { - config = null; configProperties = null; eventCallback = null; mockArtifactDeploymentManager = null; diff --git a/src/test/java/org/onap/aai/modelloader/notification/TestNotificationPublisher.java b/src/test/java/org/onap/aai/modelloader/notification/TestNotificationPublisher.java index edf50a8..c4aa932 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/TestNotificationPublisher.java +++ b/src/test/java/org/onap/aai/modelloader/notification/TestNotificationPublisher.java @@ -63,7 +63,7 @@ public class TestNotificationPublisher { @BeforeEach public void setupMocks() { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); when(client.getConfiguration()).thenReturn(config); when(client.sendDownloadStatus(any())).thenReturn(clientResult); when(client.sendComponentDoneStatus(any())).thenReturn(clientResult); diff --git a/src/test/java/org/onap/aai/modelloader/service/TestModelLoaderService.java b/src/test/java/org/onap/aai/modelloader/service/TestModelController.java similarity index 51% rename from src/test/java/org/onap/aai/modelloader/service/TestModelLoaderService.java rename to src/test/java/org/onap/aai/modelloader/service/TestModelController.java index e58716c..9106342 100644 --- a/src/test/java/org/onap/aai/modelloader/service/TestModelLoaderService.java +++ b/src/test/java/org/onap/aai/modelloader/service/TestModelController.java @@ -22,16 +22,30 @@ package org.onap.aai.modelloader.service; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.io.IOException; import java.util.Base64; +import java.util.Collections; import javax.ws.rs.core.Response; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.onap.aai.modelloader.config.BeanConfig; +import org.mockito.Mock; +import org.onap.aai.modelloader.config.ModelLoaderConfig; +import org.onap.aai.modelloader.extraction.VnfCatalogExtractor; +import org.onap.aai.modelloader.notification.ArtifactDownloadManager; +import org.onap.aai.modelloader.notification.BabelArtifactConverter; +import org.onap.aai.modelloader.notification.EventCallback; +import org.onap.aai.modelloader.notification.NotificationPublisher; +import org.onap.aai.modelloader.restclient.BabelServiceClient; +import org.onap.aai.modelloader.restclient.BabelServiceClientException; import org.onap.aai.modelloader.util.ArtifactTestUtils; +import org.onap.sdc.api.IDistributionClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; @@ -40,41 +54,59 @@ import org.springframework.test.context.TestPropertySource; * Tests for the ModelLoaderService class. * */ -@SpringBootTest(classes = {BeanConfig.class, ModelController.class, MockBabelServiceClientFactory.class}) +@SpringBootTest @TestPropertySource(properties = {"CONFIG_HOME=src/test/resources",}) -public class TestModelLoaderService { +public class TestModelController { - @Autowired - private ModelController service; + @Autowired IDistributionClient iDistributionClient; + @Autowired ModelLoaderConfig modelLoaderConfig; + @Autowired EventCallback eventCallback; + @Autowired ArtifactDeploymentManager artifactDeploymentManager; + @Autowired BabelArtifactConverter babelArtifactConverter; + @Autowired NotificationPublisher notificationPublisher; + @Autowired VnfCatalogExtractor vnfCatalogExtractor; + + @Mock BabelServiceClientFactory clientFactory; + @Mock BabelServiceClient babelServiceClient; + + private ModelController modelController; + + @BeforeEach + public void init() throws BabelServiceClientException { + when(clientFactory.create(any())).thenReturn(babelServiceClient); + when(babelServiceClient.postArtifact(any(), any(), any(), any())).thenReturn(Collections.emptyList()); + ArtifactDownloadManager artifactDownloadManager = new ArtifactDownloadManager(iDistributionClient, modelLoaderConfig, clientFactory, babelArtifactConverter, notificationPublisher, vnfCatalogExtractor); + this.modelController = new ModelController(iDistributionClient, modelLoaderConfig, eventCallback, artifactDeploymentManager, artifactDownloadManager); + } @AfterEach public void shutdown() { - service.preShutdownOperations(); + modelController.preShutdownOperations(); } @Test public void testLoadModel() { - Response response = service.loadModel(""); + Response response = modelController.loadModel(""); assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); } @Test public void testSaveModel() { - Response response = service.saveModel("", ""); + Response response = modelController.saveModel("", ""); assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); } @Test public void testIngestModel() throws IOException { byte[] csarPayload = new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar"); - Response response = service.ingestModel("model-name", "", Base64.getEncoder().encodeToString(csarPayload)); + Response response = modelController.ingestModel("model-name", "", Base64.getEncoder().encodeToString(csarPayload)); assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); } @Test public void testIngestModelMissingName() throws IOException { byte[] csarPayload = new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar"); - Response response = service.ingestModel("", "", Base64.getEncoder().encodeToString(csarPayload)); + Response response = modelController.ingestModel("", "", Base64.getEncoder().encodeToString(csarPayload)); assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); } diff --git a/src/test/java/org/onap/aai/modelloader/service/TestModelLoaderServiceWithSdc.java b/src/test/java/org/onap/aai/modelloader/service/TestModelLoaderServiceWithSdc.java index 0b68cc0..a8501be 100644 --- a/src/test/java/org/onap/aai/modelloader/service/TestModelLoaderServiceWithSdc.java +++ b/src/test/java/org/onap/aai/modelloader/service/TestModelLoaderServiceWithSdc.java @@ -30,7 +30,6 @@ import javax.ws.rs.core.Response; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import org.onap.aai.modelloader.config.BeanConfig; import org.onap.aai.modelloader.util.ArtifactTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -40,7 +39,7 @@ import org.springframework.test.context.TestPropertySource; * Tests for the ModelLoaderService class. * */ -@SpringBootTest(classes = {BeanConfig.class, ModelController.class, HttpsBabelServiceClientFactory.class}) +@SpringBootTest @TestPropertySource(properties = {"CONFIG_HOME=src/test/resources",}) public class TestModelLoaderServiceWithSdc {