Improve testing stability
[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 static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26
27 import fj.data.Either;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Optional;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.mockito.invocation.InvocationOnMock;
42 import org.mockito.stubbing.Answer;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.model.category.CategoryDefinition;
47 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
48 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
49 import org.openecomp.sdc.exception.ResponseFormat;
50
51 public class CategoriesImportManagerTest {
52     @InjectMocks
53     static CategoriesImportManager importManager = new CategoriesImportManager();
54     public static final IElementOperation elementOperation = Mockito.mock(IElementOperation.class);
55     public static final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class);
56     private static SubCategoryDefinition subcategory;
57
58     @BeforeClass
59     public static void beforeClass() throws IOException {
60         subcategory = new SubCategoryDefinition();
61         subcategory.setUniqueId("123");
62
63         when(elementOperation.createCategory(Mockito.any(CategoryDefinition.class), Mockito.any(NodeTypeEnum.class))).thenAnswer((Answer<Either<CategoryDefinition, ActionStatus>>) invocation -> {
64             Object[] args = invocation.getArguments();
65             CategoryDefinition category = (CategoryDefinition) args[0];
66             category.setUniqueId("123");
67             return Either.left(category);
68         });
69         when(elementOperation.createSubCategory(Mockito.any(String.class), Mockito.any(SubCategoryDefinition.class), Mockito.any(NodeTypeEnum.class))).thenAnswer(new Answer<Either<SubCategoryDefinition, ActionStatus>>() {
70             public Either<SubCategoryDefinition, ActionStatus> answer(InvocationOnMock invocation) {
71                 Object[] args = invocation.getArguments();
72                 // subcategory.setName(((SubCategoryDefinition)args[0]).getName());
73                 return Either.left(subcategory);
74             }
75
76         });
77
78         // when(Mockito.any(SubCategoryDefinition.class).getUniqueId()).thenReturn("123");
79     }
80
81     @Before
82     public void initMocks() {
83         MockitoAnnotations.openMocks(this);
84     }
85
86     @Test
87     public void importCategoriesTest() throws IOException {
88         String ymlContent = getYmlContent();
89         Either<Map<String, List<CategoryDefinition>>, ResponseFormat> createCapabilityTypes = importManager.createCategories(ymlContent);
90         
91         assertTrue(createCapabilityTypes.isLeft());        
92         final Map<String, List<CategoryDefinition>> categories = createCapabilityTypes.left().value();
93         final Optional<CategoryDefinition> categoryVoIPCallControl = categories.get("services").stream().filter(category -> category.getName().equals("VoIP Call Control")).findAny();
94         final Optional<CategoryDefinition> categoryWithServiceSubstitutionTrue = categories.get("services").stream().filter(category -> category.getName().equals("Category With Service Substitution True")).findAny();
95         final Optional<CategoryDefinition> categoryWithServiceSubstitutionFalse = categories.get("services").stream().filter(category -> category.getName().equals("Category With Service Substitution False")).findAny();
96
97         
98         
99         assertTrue(categoryVoIPCallControl.isPresent());
100         assertFalse(categoryVoIPCallControl.get().isUseServiceSubstitutionForNestedServices());
101         assertTrue(categoryWithServiceSubstitutionTrue.isPresent());
102         assertTrue(categoryWithServiceSubstitutionTrue.get().isUseServiceSubstitutionForNestedServices());
103         assertTrue(categoryWithServiceSubstitutionFalse.isPresent());
104         assertFalse(categoryWithServiceSubstitutionFalse.get().isUseServiceSubstitutionForNestedServices());
105     }
106
107     private String getYmlContent() throws IOException {
108         Path filePath = Paths.get("src/test/resources/types/categoryTypes.yml");
109         byte[] fileContent = Files.readAllBytes(filePath);
110         return new String(fileContent);
111     }
112 }