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.junit.Assert.fail;
25 import static org.mockito.Matchers.anyInt;
26 import static org.mockito.Matchers.anyObject;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
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 NameGeneratorExcMissingDataTest {
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<>();
63 protected Map<String, String> makeOneRequest(String policy) {
64 Map<String, String> requestElement = new HashMap<>();
65 requestElement.put("external-key", "123456");
66 requestElement.put("policy-instance-name", policy);
67 requestElement.put("complex", "dlstxasdf");
68 requestElement.put("naming-type", "VNF");
69 requestElement.put("nf-naming-code", "ve1");
70 requestElement.put("resource-name", "vnf-name");
71 return requestElement;
75 public void missingPolicy() throws Exception {
76 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
77 Map<String, String> requestElement = makeOneRequest(policyName);
78 List<Map<String, String>> allElements = new ArrayList<>();
79 allElements.add(requestElement);
81 when(policyFinder.findPolicy(policyName)).thenReturn(null);
83 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
84 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
88 fail("Expecting exception");
89 } catch (Exception e) {
90 assertEquals("Could not find the policy data for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq",
94 fail("Expecting exception");
98 public void missingPolicyData() throws Exception {
99 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
100 Map<String, String> requestElement = makeOneRequest(policyName);
101 List<Map<String, String>> allElements = new ArrayList<>();
102 allElements.add(requestElement);
104 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
105 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
109 fail("Expecting exception");
110 } catch (Exception e) {
111 assertEquals("Could not find the policy data for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq "
112 + "and naming-type VNF",
116 fail("Expecting exception");
120 public void missingPolicyName() throws Exception {
121 String policyName = null;
122 Map<String, String> requestElement = makeOneRequest(policyName);
123 List<Map<String, String>> allElements = new ArrayList<>();
124 allElements.add(requestElement);
126 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
127 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
131 fail("Expecting exception");
132 } catch (Exception e) {
133 assertEquals("Could not find policy name in the request", e.getMessage());
136 fail("Expecting exception");
140 public void missingNamingType() throws Exception {
141 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
142 Map<String, String> requestElement = makeOneRequest(policyName);
143 requestElement.remove("naming-type");
144 List<Map<String, String>> allElements = new ArrayList<>();
145 allElements.add(requestElement);
147 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
148 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
152 fail("Expecting exception");
153 } catch (Exception e) {
154 assertEquals("Could not find naming type in the request for policy "
155 + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq",
159 fail("Expecting exception");
163 public void missingRecipe() throws Exception {
164 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
165 Map<String, String> requestElement = makeOneRequest(policyName);
166 List<Map<String, String>> allElements = new ArrayList<>();
167 allElements.add(requestElement);
169 Map<String, Object> policy = new FilePolicyReader("bad_policy_missing_recipe.json").getPolicy();
170 when(policyFinder.findPolicy(policyName)).thenReturn(policy);
171 when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true);
172 when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true);
173 when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), anyInt())).thenReturn(1L);
175 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
176 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
180 fail("Expecting exception");
181 } catch (Exception e) {
182 assertEquals("Could not find the recipe for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq "
183 + "and naming-type VNF",
187 fail("Expecting exception");
191 public void missingRecipeOneField() throws Exception {
192 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
193 Map<String, String> requestElement = makeOneRequest(policyName);
194 requestElement.remove("complex");
195 List<Map<String, String>> allElements = new ArrayList<>();
196 allElements.add(requestElement);
198 Map<String, Object> policy = new FilePolicyReader("vnf_policy_seq.json").getPolicy();
199 when(policyFinder.findPolicy(policyName)).thenReturn(policy);
201 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
202 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
206 fail("Expecting exception");
207 } catch (Exception e) {
208 assertEquals("Could not find data for recipe item COMPLEX in policy "
209 + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq "
210 + "and naming-type VNF", e.getMessage());
213 fail("Expecting exception");
217 public void missingRecipeMultipleFields() throws Exception {
218 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
219 Map<String, String> requestElement = makeOneRequest(policyName);
220 requestElement.remove("complex");
221 List<Map<String, String>> allElements = new ArrayList<>();
222 allElements.add(requestElement);
224 Map<String, Object> policy = new FilePolicyReader("long_policy.json").getPolicy();
225 when(policyFinder.findPolicy(policyName)).thenReturn(policy);
227 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
228 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
232 fail("Expecting exception");
233 } catch (Exception e) {
234 assertEquals("Could not find data for recipe items COMPLEX, Field2, Field3 and Field4 in policy "
235 + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq "
236 + "and naming-type VNF", e.getMessage());
239 fail("Expecting exception");
243 public void missingRecipeTwoFields() throws Exception {
244 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
245 Map<String, String> requestElement = makeOneRequest(policyName);
246 requestElement.put("Field2", "f2");
247 List<Map<String, String>> allElements = new ArrayList<>();
248 allElements.add(requestElement);
250 Map<String, Object> policy = new FilePolicyReader("long_policy.json").getPolicy();
251 when(policyFinder.findPolicy(policyName)).thenReturn(policy);
253 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
254 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
258 fail("Expecting exception");
259 } catch (Exception e) {
260 assertEquals("Could not find data for recipe items Field3 and Field4 in policy "
261 + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq "
262 + "and naming-type VNF", e.getMessage());
265 fail("Expecting exception");