2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.apps.ms.neng.core.gen;
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;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
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;
47 @RunWith(MockitoJUnitRunner.class)
48 public class NameGeneratorDependencyEarlierTest {
50 private PolicyParameters policyParams = mock(PolicyParameters.class);
52 private PolicyFinder policyFinder = mock(PolicyFinder.class);
54 private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class);
56 private DbNameValidator dbValidator = mock(DbNameValidator.class);
58 private AaiNameValidator aaiValidator = mock(AaiNameValidator.class);
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<>();
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;
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;
86 * Setup params related data.
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");
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);
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);
110 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
111 namePresister, requestElement1, allElements, earlierNames, policyCache, new ArrayList<>());
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"));
118 NameGenerator gen2 = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
119 namePresister, requestElement2, allElements, earlierNames, policyCache, new ArrayList<>());
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"));