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.Mockito;
38 import org.mockito.runners.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 NameGeneratorSequenceTest {
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<>();
64 protected Map<String, String> makeOneRequest(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;
76 * Setup for policy params.
79 public void setupPolicyParams() throws Exception {
80 Mockito.lenient().when(policyParams.mapFunction("substr")).thenReturn("substring");
81 Mockito.lenient().when(policyParams.mapFunction("to_lower_case")).thenReturn("toLowerCase");
82 Mockito.lenient().when(policyParams.mapFunction("to_upper_case")).thenReturn("toUpperCase");
86 public void generate() throws Exception {
87 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
88 Map<String, String> requestElement = makeOneRequest(policyName);
89 List<Map<String, String>> allElements = new ArrayList<>();
90 allElements.add(requestElement);
92 Map<String, Object> policy = new FilePolicyReader("vnf_policy_seq.json").getPolicy();
93 Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(policy);
94 Mockito.lenient().when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true);
95 Mockito.lenient().when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true);
96 Mockito.lenient().when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), anyInt())).thenReturn(1L);
98 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
99 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
101 Map<String, String> resp = gen.generate();
102 assertEquals("vnf-name", resp.get("resource-name"));
103 assertEquals("123456", resp.get("external-key"));
104 assertEquals("abcde001ve1", resp.get("resource-value"));