2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
19 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.core.Is.is;
23 import static org.hamcrest.core.IsNull.notNullValue;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import static org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.EtsiNfvNsCsarEntryGenerator.ETSI_NS_COMPONENT_CATEGORY;
30 import static org.openecomp.sdc.common.api.ArtifactTypeEnum.ONBOARDED_PACKAGE;
32 import fj.data.Either;
34 import java.nio.charset.StandardCharsets;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.Optional;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.openecomp.sdc.be.csar.security.model.CertificateInfoImpl;
46 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
47 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
48 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
49 import org.openecomp.sdc.be.model.ArtifactDefinition;
50 import org.openecomp.sdc.be.model.ComponentInstance;
51 import org.openecomp.sdc.be.model.Service;
52 import org.openecomp.sdc.be.model.category.CategoryDefinition;
53 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException;
54 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.VnfDescriptorException;
55 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.factory.NsDescriptorGeneratorFactory;
56 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.config.EtsiVersion;
57 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.config.NsDescriptorConfig;
58 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.Nsd;
59 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.NsdCsar;
60 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.VnfDescriptor;
61 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.security.NsdCsarEtsiOption2Signer;
62 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.security.exception.NsdSignatureException;
63 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
65 class EtsiNfvNsdCsarGeneratorImplTest {
67 private static final String SERVICE_NORMALIZED_NAME = "normalizedName";
69 private VnfDescriptorGenerator vnfDescriptorGenerator;
71 private NsDescriptorGenerator nsDescriptorGeneratorImpl;
73 private NsDescriptorGeneratorFactory nsDescriptorGeneratorFactory;
75 private ArtifactCassandraDao artifactCassandraDao;
77 private NsdCsarEtsiOption2Signer nsdCsarEtsiOption2Signer;
79 private Service service;
80 private EtsiNfvNsdCsarGeneratorImpl etsiNfvNsdCsarGenerator;
84 MockitoAnnotations.openMocks(this);
85 final EtsiVersion version2_5_1 = EtsiVersion.VERSION_2_5_1;
86 etsiNfvNsdCsarGenerator = new EtsiNfvNsdCsarGeneratorImpl(new NsDescriptorConfig(version2_5_1),
87 vnfDescriptorGenerator, nsDescriptorGeneratorFactory, artifactCassandraDao, nsdCsarEtsiOption2Signer);
88 when(nsDescriptorGeneratorFactory.create()).thenReturn(nsDescriptorGeneratorImpl);
92 void generateNsdCsarSuccessfulTest() throws VnfDescriptorException, NsdException {
93 mockServiceComponent();
94 mockServiceComponentArtifacts();
95 final NsdCsar nsdCsar = etsiNfvNsdCsarGenerator.generateNsdCsar(service);
96 assertThat("The NSD CSAR should not be null", nsdCsar, is(notNullValue()));
97 assertThat("The NSD CSAR should not be signed", nsdCsar.isSigned(), is(false));
98 assertThat("The NSD CSAR content should not be null", nsdCsar.getCsarPackage(), is(notNullValue()));
102 void generateSignedNsdCsarSuccessfulTest() throws VnfDescriptorException, NsdException, NsdSignatureException {
103 mockServiceComponent();
104 mockServiceComponentArtifacts();
105 when(nsdCsarEtsiOption2Signer.isCertificateConfigured()).thenReturn(true);
106 final String path = getClass().getClassLoader().getResource("aFile.txt").getPath();
107 System.out.println(path);
108 final CertificateInfoImpl certificateInfo = new CertificateInfoImpl(new File(path), null);
109 when(nsdCsarEtsiOption2Signer.getSigningCertificate()).thenReturn(Optional.of(certificateInfo));
110 when(nsdCsarEtsiOption2Signer.sign(any(byte[].class))).thenReturn("signedCsar".getBytes(StandardCharsets.UTF_8));
111 final NsdCsar nsdCsar = etsiNfvNsdCsarGenerator.generateNsdCsar(service);
112 verify(nsdCsarEtsiOption2Signer).signArtifacts(any(NsdCsar.class));
113 assertThat("The NSD CSAR should not be null", nsdCsar, is(notNullValue()));
114 assertThat("The NSD CSAR should be signed", nsdCsar.isSigned(), is(true));
115 assertThat("The NSD CSAR content should not be null", nsdCsar.getCsarPackage(), is(notNullValue()));
116 assertThat("The NSD CSAR name should be as expected", nsdCsar.getFileName(), is(SERVICE_NORMALIZED_NAME));
117 assertThat("The NSD CSAR name should be as expected", nsdCsar.isEmpty(), is(false));
121 void invalidComponentTest() {
122 assertThrows(NsdException.class, () -> etsiNfvNsdCsarGenerator.generateNsdCsar(null));
125 private void mockServiceComponent() throws VnfDescriptorException, NsdException {
126 when(service.getNormalizedName()).thenReturn(SERVICE_NORMALIZED_NAME);
127 when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE);
128 final String componentInstance1Name = "componentInstance1";
129 final ComponentInstance componentInstance1 = mockServiceComponentInstance(componentInstance1Name);
130 final ArtifactDefinition instanceArtifact1 = mockComponentInstanceArtifact(componentInstance1, "instanceArtifact1");
131 final VnfDescriptor vnfDescriptor1 = new VnfDescriptor();
132 final List<VnfDescriptor> vnfDescriptorList = Collections.singletonList(vnfDescriptor1);
133 final Nsd nsd = new Nsd();
134 when(vnfDescriptorGenerator.generate(componentInstance1Name, instanceArtifact1)).thenReturn(Optional.of(vnfDescriptor1));
135 when(nsDescriptorGeneratorImpl.generate(service, vnfDescriptorList)).thenReturn(Optional.of(nsd));
137 final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
138 final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition();
139 nsComponentCategoryDefinition.setName(ETSI_NS_COMPONENT_CATEGORY);
140 categoryDefinitionList.add(nsComponentCategoryDefinition);
141 when(service.getCategories()).thenReturn(categoryDefinitionList);
144 private void mockServiceComponentArtifacts() {
145 final Map<String, ArtifactDefinition> allArtifactsMap = new HashMap<>();
146 final String artifact1Id = "artifact1";
147 final ArtifactDefinition artifact1 = mockArtifactDefinition(artifact1Id);
148 final byte[] artifact1Bytes = new byte[1];
149 allArtifactsMap.put(artifact1Id, artifact1);
150 when(service.getAllArtifacts()).thenReturn(allArtifactsMap);
151 final DAOArtifactData artifact1Data = new DAOArtifactData();
152 artifact1Data.setDataAsArray(artifact1Bytes);
153 when(artifactCassandraDao.getArtifact(artifact1Id)).thenReturn(Either.left(artifact1Data));
156 private ComponentInstance mockServiceComponentInstance(final String componentInstanceName) {
157 final Map<String, ArtifactDefinition> deploymentArtifactMap = new HashMap<>();
158 final String instanceArtifact1Id = "instanceArtifact1";
159 final ArtifactDefinition instanceArtifact1 = mockArtifactDefinition(instanceArtifact1Id);
160 instanceArtifact1.setToscaPresentationValue(JsonPresentationFields.ARTIFACT_TYPE, ONBOARDED_PACKAGE.getType());
161 deploymentArtifactMap.put(instanceArtifact1Id, instanceArtifact1);
162 DAOArtifactData instanceArtifact1Data = new DAOArtifactData();
163 final byte[] instanceArtifact1Bytes = new byte[1];
164 instanceArtifact1Data.setDataAsArray(instanceArtifact1Bytes);
165 when(artifactCassandraDao.getArtifact(instanceArtifact1Id)).thenReturn(Either.left(instanceArtifact1Data));
166 final ComponentInstance componentInstance = mock(ComponentInstance.class);
167 when(componentInstance.getDeploymentArtifacts()).thenReturn(deploymentArtifactMap);
168 when(componentInstance.getName()).thenReturn(componentInstanceName);
169 final List<ComponentInstance> componentInstanceList = new ArrayList<>();
170 componentInstanceList.add(componentInstance);
171 when(service.getComponentInstances()).thenReturn(componentInstanceList);
172 return componentInstance;
175 private ArtifactDefinition mockComponentInstanceArtifact(final ComponentInstance componentInstance, final String instanceArtifactId) {
176 final Map<String, ArtifactDefinition> deploymentArtifactMap = new HashMap<>();
177 when(componentInstance.getDeploymentArtifacts()).thenReturn(deploymentArtifactMap);
178 final ArtifactDefinition instanceArtifact1 = mockArtifactDefinition(instanceArtifactId);
179 instanceArtifact1.setToscaPresentationValue(JsonPresentationFields.ARTIFACT_TYPE, ONBOARDED_PACKAGE.getType());
180 deploymentArtifactMap.put(instanceArtifactId, instanceArtifact1);
181 DAOArtifactData instanceArtifact1Data = new DAOArtifactData();
182 final byte[] instanceArtifact1Bytes = new byte[1];
183 instanceArtifact1Data.setDataAsArray(instanceArtifact1Bytes);
184 when(artifactCassandraDao.getArtifact(instanceArtifactId)).thenReturn(Either.left(instanceArtifact1Data));
185 return instanceArtifact1;
188 private ArtifactDefinition mockArtifactDefinition(final String artifactId) {
189 final ArtifactDefinition artifact = new ArtifactDefinition();
190 artifact.setEsId(artifactId);