aboutsummaryrefslogtreecommitdiffstats
path: root/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorSequenceTest.java
blob: ce990b73b81c9cb007beb8827b454d92b10c61c5 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP : CCSDK.apps
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. 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.onap.ccsdk.apps.ms.neng.core.gen;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister;
import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader;
import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder;
import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters;
import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator;
import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator;
import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator;

@RunWith(MockitoJUnitRunner.class)
public class NameGeneratorSequenceTest {
    @Mock
    private PolicyParameters policyParams = mock(PolicyParameters.class);
    @Mock
    private PolicyFinder policyFinder = mock(PolicyFinder.class);
    @Mock
    private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class);
    @Mock
    private DbNameValidator dbValidator = mock(DbNameValidator.class);
    @Mock
    private AaiNameValidator aaiValidator = mock(AaiNameValidator.class);
    @Mock
    private NamePersister namePresister = mock(NamePersister.class);
    private Map<String, Map<String, String>> earlierNames = new HashMap<>();
    private Map<String, Map<String, ?>> policyCache = new HashMap<>();

    protected Map<String, String> makeOneRequest(String policy) {
        Map<String, String> requestElement = new HashMap<>();
        requestElement.put("external-key", "123456");
        requestElement.put("policy-instance-name", policy);
        requestElement.put("complex", "abcdeasdf");
        requestElement.put("naming-type", "VNF");
        requestElement.put("nf-naming-code", "ve1");
        requestElement.put("resource-name", "vnf-name");
        return requestElement;
    }

    /**
     * Setup for policy params.
     */
    @Before
    public void setupPolicyParams() throws Exception {
        Mockito.lenient().when(policyParams.mapFunction("substr")).thenReturn("substring");
        Mockito.lenient().when(policyParams.mapFunction("to_lower_case")).thenReturn("toLowerCase");
        Mockito.lenient().when(policyParams.mapFunction("to_upper_case")).thenReturn("toUpperCase");
    }

    @Test
    public void generate() throws Exception {
        String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
        Map<String, String> requestElement = makeOneRequest(policyName);
        List<Map<String, String>> allElements = new ArrayList<>();
        allElements.add(requestElement);

        Map<String, Object> policy = new FilePolicyReader("vnf_policy_seq.json").getPolicy();
        Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(policy);
        Mockito.lenient().when(aaiValidator.validate(any(), any())).thenReturn(true);
        Mockito.lenient().when(dbValidator.validate(any(), any())).thenReturn(true);
        Mockito.lenient().when(sequenceGenerator.generate(any(), any(), any(), any(), anyInt())).thenReturn(1L);

        NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
                        namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());

        Map<String, String> resp = gen.generate();
        assertEquals("vnf-name", resp.get("resource-name"));
        assertEquals("123456", resp.get("external-key"));
        assertEquals("abcde001ve1", resp.get("resource-value"));
    }
}