2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.when;
28 import fj.data.Either;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.List;
35 import java.util.Optional;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mockito;
41 import org.mockito.MockitoAnnotations;
42 import org.mockito.invocation.InvocationOnMock;
43 import org.mockito.stubbing.Answer;
44 import org.openecomp.sdc.be.dao.api.ActionStatus;
45 import org.openecomp.sdc.be.datatypes.category.MetadataKeyDataDefinition;
46 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
47 import org.openecomp.sdc.be.impl.ComponentsUtils;
48 import org.openecomp.sdc.be.model.category.CategoryDefinition;
49 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
50 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
51 import org.openecomp.sdc.exception.ResponseFormat;
53 class CategoriesImportManagerTest {
55 static CategoriesImportManager importManager = new CategoriesImportManager();
56 public static final IElementOperation elementOperation = Mockito.mock(IElementOperation.class);
57 public static final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class);
58 private static SubCategoryDefinition subcategory;
61 public static void beforeClass() throws IOException {
62 subcategory = new SubCategoryDefinition();
63 subcategory.setUniqueId("123");
65 when(elementOperation.createCategory(Mockito.any(CategoryDefinition.class), Mockito.any(NodeTypeEnum.class))).thenAnswer((Answer<Either<CategoryDefinition, ActionStatus>>) invocation -> {
66 Object[] args = invocation.getArguments();
67 CategoryDefinition category = (CategoryDefinition) args[0];
68 category.setUniqueId("123");
69 return Either.left(category);
71 when(elementOperation.createSubCategory(Mockito.any(String.class), Mockito.any(SubCategoryDefinition.class), Mockito.any(NodeTypeEnum.class))).thenAnswer(new Answer<Either<SubCategoryDefinition, ActionStatus>>() {
72 public Either<SubCategoryDefinition, ActionStatus> answer(InvocationOnMock invocation) {
73 Object[] args = invocation.getArguments();
74 // subcategory.setName(((SubCategoryDefinition)args[0]).getName());
75 return Either.left(subcategory);
80 // when(Mockito.any(SubCategoryDefinition.class).getUniqueId()).thenReturn("123");
84 public void initMocks() {
85 MockitoAnnotations.openMocks(this);
89 void importCategoriesTest() throws IOException {
90 String ymlContent = getYmlContent();
91 Either<Map<String, List<CategoryDefinition>>, ResponseFormat> createCapabilityTypes = importManager.createCategories(ymlContent);
93 assertTrue(createCapabilityTypes.isLeft());
94 final Map<String, List<CategoryDefinition>> categories = createCapabilityTypes.left().value();
95 final Optional<CategoryDefinition> categoryVoIPCallControl = categories.get("services").stream().filter(category -> category.getName().equals("VoIP Call Control")).findAny();
96 final Optional<CategoryDefinition> categoryWithServiceSubstitutionTrue = categories.get("services").stream().filter(category -> category.getName().equals("Category With Service Substitution True")).findAny();
97 final Optional<CategoryDefinition> categoryWithServiceSubstitutionFalse = categories.get("services").stream().filter(category -> category.getName().equals("Category With Service Substitution False")).findAny();
101 assertTrue(categoryVoIPCallControl.isPresent());
102 assertFalse(categoryVoIPCallControl.get().isUseServiceSubstitutionForNestedServices());
103 assertTrue(categoryWithServiceSubstitutionTrue.isPresent());
104 assertTrue(categoryWithServiceSubstitutionTrue.get().isUseServiceSubstitutionForNestedServices());
105 assertTrue(categoryWithServiceSubstitutionFalse.isPresent());
106 assertFalse(categoryWithServiceSubstitutionFalse.get().isUseServiceSubstitutionForNestedServices());
110 void categoriesNameAndDisplayNameTest() throws IOException {
111 final String categoryName = "Category With DisplayName And metadata";
112 final String expectedCategoryDisplayName = "Display Name For Category";
113 final String ymlContent = getYmlContent();
114 final Either<Map<String, List<CategoryDefinition>>, ResponseFormat> createCapabilityTypes = importManager.createCategories(ymlContent);
115 final Map<String, List<CategoryDefinition>> categories = createCapabilityTypes.left().value();
117 final Optional<CategoryDefinition> categoryWithNameAndDisplayName = categories.get("services").stream().filter(category -> category.getName().equals(categoryName)).findAny();
118 final String categoryDisplayName = categoryWithNameAndDisplayName.get().getDisplayName();
120 assertTrue(categoryWithNameAndDisplayName.isPresent());
121 assertEquals(expectedCategoryDisplayName, categoryDisplayName);
125 void getMetadataKeysTest() throws IOException {
126 final String categoryName = "Category With DisplayName And metadata";
127 final String expectedMetadataName = "ETSI Version";
128 final String expectedEtsiVersion = "2.5.1";
129 final String ymlContent = getYmlContent();
130 final Either<Map<String, List<CategoryDefinition>>, ResponseFormat> createCapabilityTypes = importManager.createCategories(ymlContent);
131 final Map<String, List<CategoryDefinition>> categories = createCapabilityTypes.left().value();
133 final Optional<CategoryDefinition> categoryWithMetadata = categories.get("services").stream().filter(category -> category.getName().equals(categoryName)).findAny();
134 final List<MetadataKeyDataDefinition> categoryMetadataList = categoryWithMetadata.get().getMetadataKeys();
135 final MetadataKeyDataDefinition categoryMetadata = categoryMetadataList.get(0);
137 assertEquals(expectedMetadataName, categoryMetadata.getName());
138 assertEquals(expectedEtsiVersion, categoryMetadata.getValidValues().get(0));
139 assertEquals(expectedEtsiVersion, categoryMetadata.getDefaultValue());
140 assertTrue(categoryMetadata.isMandatory());
143 private String getYmlContent() throws IOException {
144 Path filePath = Paths.get("src/test/resources/types/categoryTypes.yml");
145 byte[] fileContent = Files.readAllBytes(filePath);
146 return new String(fileContent);