Support for Nested/Hierarchical Services
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / CategoriesImportManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.mockito.invocation.InvocationOnMock;
31 import org.mockito.stubbing.Answer;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.model.category.CategoryDefinition;
36 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
37 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
38 import org.openecomp.sdc.exception.ResponseFormat;
39
40 import java.io.IOException;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Optional;
47 import java.util.stream.Stream;
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertFalse;
50 import static org.junit.Assert.assertTrue;
51 import static org.mockito.Mockito.when;
52
53 public class CategoriesImportManagerTest {
54     @InjectMocks
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;
59
60     @BeforeClass
61     public static void beforeClass() throws IOException {
62         subcategory = new SubCategoryDefinition();
63         subcategory.setUniqueId("123");
64
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);
70         });
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);
76             }
77
78         });
79
80         // when(Mockito.any(SubCategoryDefinition.class).getUniqueId()).thenReturn("123");
81     }
82
83     @Before
84     public void initMocks() {
85         MockitoAnnotations.initMocks(this);
86     }
87
88     @Test
89     public void importCategoriesTest() throws IOException {
90         String ymlContent = getYmlContent();
91         Either<Map<String, List<CategoryDefinition>>, ResponseFormat> createCapabilityTypes = importManager.createCategories(ymlContent);
92         
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();
98
99         
100         
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());
107     }
108
109     private String getYmlContent() throws IOException {
110         Path filePath = Paths.get("src/test/resources/types/categoryTypes.yml");
111         byte[] fileContent = Files.readAllBytes(filePath);
112         return new String(fileContent);
113     }
114 }