Update third party versions in ccsdk/apps
[ccsdk/apps.git] / ms / neng / src / test / java / org / onap / ccsdk / apps / ms / neng / core / gen / NameGeneratorDependencyEarlierTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps
4  * ================================================================================
5  * Copyright (C) 2018 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.onap.ccsdk.apps.ms.neng.core.gen;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyInt;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister;
40 import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader;
41 import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder;
42 import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters;
43 import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator;
44 import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator;
45 import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class NameGeneratorDependencyEarlierTest {
49     @Mock
50     private PolicyParameters policyParams = mock(PolicyParameters.class);
51     @Mock
52     private PolicyFinder policyFinder = mock(PolicyFinder.class);
53     @Mock
54     private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class);
55     @Mock
56     private DbNameValidator dbValidator = mock(DbNameValidator.class);
57     @Mock
58     private AaiNameValidator aaiValidator = mock(AaiNameValidator.class);
59     @Mock
60     private NamePersister namePresister = mock(NamePersister.class);
61     private Map<String, Map<String, String>> earlierNames = new HashMap<>();
62     private Map<String, Map<String, ?>> policyCache = new HashMap<>();
63
64
65     protected Map<String, String> makeVnfRequest(String policy) {
66         Map<String, String> requestElement = new HashMap<>();
67         requestElement.put("external-key", "123456");
68         requestElement.put("policy-instance-name", policy);
69         requestElement.put("complex", "abcdeasdf");
70         requestElement.put("naming-type", "VNF");
71         requestElement.put("nf-naming-code", "ve1");
72         requestElement.put("resource-name", "vnf-name");
73         return requestElement;
74     }
75
76     protected Map<String, String> makeVmRequest(String policy) {
77         Map<String, String> requestElement = new HashMap<>();
78         requestElement.put("external-key", "923456");
79         requestElement.put("policy-instance-name", policy);
80         requestElement.put("naming-type", "VM");
81         requestElement.put("resource-name", "vm-name");
82         return requestElement;
83     }
84
85     /**
86      * Setup params related data.
87      */
88     @Before
89     public void setupPolicyParams() throws Exception {
90         Mockito.lenient().when(policyParams.mapFunction("substr")).thenReturn("substring");
91         Mockito.lenient().when(policyParams.mapFunction("to_lower_case")).thenReturn("toLowerCase");
92         Mockito.lenient().when(policyParams.mapFunction("to_upper_case")).thenReturn("toUpperCase");
93     }
94
95     @Test
96     public void generate() throws Exception {
97         String policyName = "SDNC_Policy.Config_MS_VNF_VM_NamingPolicy";
98         Map<String, String> requestElement1 = makeVnfRequest(policyName);
99         Map<String, String> requestElement2 = makeVmRequest(policyName);
100         List<Map<String, String>> allElements = new ArrayList<>();
101         allElements.add(requestElement1);
102         allElements.add(requestElement2);
103
104         Map<String, Object> policy = new FilePolicyReader("vnf_and_vm_policy.json").getPolicy();
105         Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(policy);
106         Mockito.lenient().when(aaiValidator.validate(any(), any())).thenReturn(true);
107         Mockito.lenient().when(dbValidator.validate(any(), any())).thenReturn(true);
108         Mockito.lenient().when(sequenceGenerator.generate(any(), any(), any(), any(), anyInt())).thenReturn(1L);
109
110         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
111                         namePresister, requestElement1, allElements, earlierNames, policyCache, new ArrayList<>());
112
113         Map<String, String> resp = gen.generate();
114         assertEquals("vnf-name", resp.get("resource-name"));
115         assertEquals("123456", resp.get("external-key"));
116         assertEquals("abcde001ve1", resp.get("resource-value"));
117
118         NameGenerator gen2 = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
119                         namePresister, requestElement2, allElements, earlierNames, policyCache, new ArrayList<>());
120
121         Map<String, String> resp2 = gen2.generate();
122         assertEquals("vm-name", resp2.get("resource-name"));
123         assertEquals("923456", resp2.get("external-key"));
124         assertEquals("abcde001ve1mts001", resp2.get("resource-value"));
125     }
126 }
127