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.Matchers.anyInt;
25 import static org.mockito.Matchers.anyObject;
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.runners.MockitoJUnitRunner;
38 import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister;
39 import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader;
40 import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder;
41 import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters;
42 import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator;
43 import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator;
44 import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator;
46 @RunWith(MockitoJUnitRunner.class)
47 public class NameGeneratorDependencyEarlierTest {
49 private PolicyParameters policyParams = mock(PolicyParameters.class);
51 private PolicyFinder policyFinder = mock(PolicyFinder.class);
53 private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class);
55 private DbNameValidator dbValidator = mock(DbNameValidator.class);
57 private AaiNameValidator aaiValidator = mock(AaiNameValidator.class);
59 private NamePersister namePresister = mock(NamePersister.class);
60 private Map<String, Map<String, String>> earlierNames = new HashMap<>();
61 private Map<String, Map<String, ?>> policyCache = new HashMap<>();
64 protected Map<String, String> makeVnfRequest(String policy) {
65 Map<String, String> requestElement = new HashMap<>();
66 requestElement.put("external-key", "123456");
67 requestElement.put("policy-instance-name", policy);
68 requestElement.put("complex", "abcdeasdf");
69 requestElement.put("naming-type", "VNF");
70 requestElement.put("nf-naming-code", "ve1");
71 requestElement.put("resource-name", "vnf-name");
72 return requestElement;
75 protected Map<String, String> makeVmRequest(String policy) {
76 Map<String, String> requestElement = new HashMap<>();
77 requestElement.put("external-key", "923456");
78 requestElement.put("policy-instance-name", policy);
79 requestElement.put("naming-type", "VM");
80 requestElement.put("resource-name", "vm-name");
81 return requestElement;
85 * Setup params related data.
88 public void setupPolicyParams() throws Exception {
89 when(policyParams.mapFunction("substr")).thenReturn("substring");
90 when(policyParams.mapFunction("to_lower_case")).thenReturn("toLowerCase");
91 when(policyParams.mapFunction("to_upper_case")).thenReturn("toUpperCase");
95 public void generate() throws Exception {
96 String policyName = "SDNC_Policy.Config_MS_VNF_VM_NamingPolicy";
97 Map<String, String> requestElement1 = makeVnfRequest(policyName);
98 Map<String, String> requestElement2 = makeVmRequest(policyName);
99 List<Map<String, String>> allElements = new ArrayList<>();
100 allElements.add(requestElement1);
101 allElements.add(requestElement2);
103 Map<String, Object> policy = new FilePolicyReader("vnf_and_vm_policy.json").getPolicy();
104 when(policyFinder.findPolicy(policyName)).thenReturn(policy);
105 when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true);
106 when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true);
107 when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), anyInt())).thenReturn(1L);
109 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
110 namePresister, requestElement1, allElements, earlierNames, policyCache, new ArrayList<>());
112 Map<String, String> resp = gen.generate();
113 assertEquals("vnf-name", resp.get("resource-name"));
114 assertEquals("123456", resp.get("external-key"));
115 assertEquals("abcde001ve1", resp.get("resource-value"));
117 NameGenerator gen2 = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
118 namePresister, requestElement2, allElements, earlierNames, policyCache, new ArrayList<>());
120 Map<String, String> resp2 = gen2.generate();
121 assertEquals("vm-name", resp2.get("resource-name"));
122 assertEquals("923456", resp2.get("external-key"));
123 assertEquals("abcde001ve1mts001", resp2.get("resource-value"));