aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/DataTypeBusinessLogicTest.java
blob: 9219de1aac240771a1bb5142cad0e4928bc61ca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 Fujitsu Limited. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import fj.data.Either;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.components.validation.UserValidations;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.ComponentInstanceInput;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
import org.openecomp.sdc.be.user.IUserBusinessLogic;

public class DataTypeBusinessLogicTest {

    private static final String COMPONENT_INSTANCE_ID = "instanceId";
    private static final String COMPONENT_ID = "componentId";
    private static final String USER_ID = "userId";
    private static final String INSTANCE_INPUT_ID = "inputId";
    private static final String DATATYPE_NAME = "org.onap.datatypes.mytype";

    @Mock
    private ComponentsUtils componentsUtilsMock;

    @Mock
    private IUserBusinessLogic userAdminMock;

    @Mock
    private ToscaOperationFacade toscaOperationFacadeMock;

    @Mock
    private UserValidations userValidations;

    @Mock
    private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;

    @InjectMocks
    private DataTypeBusinessLogic testInstance;

    private Service service;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        service = new Service();
        service.setUniqueId(COMPONENT_INSTANCE_ID);
        ComponentInstance componentInstance = new ComponentInstance();
        componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
        service.setComponentInstances(Collections.singletonList(componentInstance));
        DataTypeDefinition dataType = new DataTypeDefinition();
        dataType.setName(DATATYPE_NAME);
        dataType.setDerivedFromName(ToscaPropertyType.Root.getType());
        List<DataTypeDefinition> dataTypes = Arrays.asList(dataType);
        service.setDataTypes(dataTypes);

        Map<String, List<ComponentInstanceInput>> instanceInputMap = new HashMap<>();
        ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
        componentInstanceInput.setInputId(INSTANCE_INPUT_ID);
        instanceInputMap.put(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceInput));
        instanceInputMap.put("someInputId", Collections.singletonList(new ComponentInstanceInput()));
        service.setComponentInstancesInputs(instanceInputMap);
        when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(new User());
        when(userAdminMock.getUser(USER_ID, false)).thenReturn(Either.left(new User()));
    }

    @Test
    public void test_getPrivateDataTypes() throws Exception {
        setMockitoWhenGetToscaElementCalled();

        Either<List<DataTypeDefinition>, StorageOperationStatus> result = testInstance.getPrivateDataTypes(COMPONENT_ID);
        assertTrue(result.isLeft());
        List<DataTypeDefinition> dataTypes = result.left().value();
        assertEquals(service.getDataTypes(), dataTypes);
    }

    @Test
    public void test_getPrivateDataType() throws Exception {
        setMockitoWhenGetToscaElementCalled();

        Either<DataTypeDefinition, StorageOperationStatus> result =
            testInstance.getPrivateDataType(COMPONENT_ID, DATATYPE_NAME);
        assertTrue(result.isLeft());
        DataTypeDefinition dataType = result.left().value();
        assertEquals(service.getDataTypes().get(0), dataType);
    }

    @Test
    public void test_deletePrivateDataType1() throws Exception {
        setMockitoWhenGetToscaElementCalled();
        when(toscaOperationFacadeMock.deleteDataTypeOfComponent(service, DATATYPE_NAME))
            .thenReturn(StorageOperationStatus.OK);

        Either<DataTypeDefinition, StorageOperationStatus> result =
            testInstance.deletePrivateDataType(COMPONENT_ID, DATATYPE_NAME);
        assertTrue(result.isLeft());
        DataTypeDefinition dataType = result.left().value();
        assertEquals(service.getDataTypes().get(0), dataType);
    }

    @Test
    public void test_deletePrivateDataType2() throws Exception {
        when(toscaOperationFacadeMock.deleteDataTypeOfComponent(service, DATATYPE_NAME))
            .thenReturn(StorageOperationStatus.OK);

        Either<DataTypeDefinition, StorageOperationStatus> result =
            testInstance.deletePrivateDataType(service, DATATYPE_NAME);
        assertTrue(result.isLeft());
        DataTypeDefinition dataType = result.left().value();
        assertEquals(service.getDataTypes().get(0), dataType);
    }

    private void setMockitoWhenGetToscaElementCalled() {
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class)))
                .thenReturn(Either.left(service));
    }
}