re base code
[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
47 import static org.junit.Assert.assertTrue;
48 import static org.mockito.Mockito.when;
49
50 public class CategoriesImportManagerTest {
51     @InjectMocks
52     static CategoriesImportManager importManager = new CategoriesImportManager();
53     public static final IElementOperation elementOperation = Mockito.mock(IElementOperation.class);
54     public static final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class);
55     private static SubCategoryDefinition subcategory;
56
57     @BeforeClass
58     public static void beforeClass() throws IOException {
59         subcategory = new SubCategoryDefinition();
60         subcategory.setUniqueId("123");
61
62         when(elementOperation.createCategory(Mockito.any(CategoryDefinition.class), Mockito.any(NodeTypeEnum.class))).thenAnswer((Answer<Either<CategoryDefinition, ActionStatus>>) invocation -> {
63             Object[] args = invocation.getArguments();
64             CategoryDefinition category = (CategoryDefinition) args[0];
65             category.setUniqueId("123");
66             return Either.left(category);
67         });
68         when(elementOperation.createSubCategory(Mockito.any(String.class), Mockito.any(SubCategoryDefinition.class), Mockito.any(NodeTypeEnum.class))).thenAnswer(new Answer<Either<SubCategoryDefinition, ActionStatus>>() {
69             public Either<SubCategoryDefinition, ActionStatus> answer(InvocationOnMock invocation) {
70                 Object[] args = invocation.getArguments();
71                 // subcategory.setName(((SubCategoryDefinition)args[0]).getName());
72                 return Either.left(subcategory);
73             }
74
75         });
76
77         // when(Mockito.any(SubCategoryDefinition.class).getUniqueId()).thenReturn("123");
78     }
79
80     @Before
81     public void initMocks() {
82         MockitoAnnotations.initMocks(this);
83     }
84
85     @Test
86     public void importCategoriesTest() throws IOException {
87         String ymlContent = getYmlContent();
88         Either<Map<String, List<CategoryDefinition>>, ResponseFormat> createCapabilityTypes = importManager.createCategories(ymlContent);
89         assertTrue(createCapabilityTypes.isLeft());
90
91     }
92
93     private String getYmlContent() throws IOException {
94         Path filePath = Paths.get("src/test/resources/types/categoryTypes.yml");
95         byte[] fileContent = Files.readAllBytes(filePath);
96         return new String(fileContent);
97     }
98 }