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.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 NameGeneratorExcMissingDataTest {
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", "dlstxasdf");
69 requestElement.put("naming-type", "VNF");
70 requestElement.put("nf-naming-code", "ve1");
71 requestElement.put("resource-name", "vnf-name");
72 return requestElement;
76 public void missingPolicy() throws Exception {
77 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
78 Map<String, String> requestElement = makeOneRequest(policyName);
79 List<Map<String, String>> allElements = new ArrayList<>();
80 allElements.add(requestElement);
82 Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(null);
84 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
85 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
89 fail("Expecting exception");
90 } catch (Exception e) {
91 assertEquals("Could not find the policy data for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq",
95 fail("Expecting exception");
99 public void missingPolicyData() throws Exception {
100 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
101 Map<String, String> requestElement = makeOneRequest(policyName);
102 List<Map<String, String>> allElements = new ArrayList<>();
103 allElements.add(requestElement);
105 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
106 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
110 fail("Expecting exception");
111 } catch (Exception e) {
112 assertEquals("Could not find the policy data for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq "
113 + "and naming-type VNF",
117 fail("Expecting exception");
121 public void missingPolicyName() throws Exception {
122 String policyName = null;
123 Map<String, String> requestElement = makeOneRequest(policyName);
124 List<Map<String, String>> allElements = new ArrayList<>();
125 allElements.add(requestElement);
127 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
128 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
132 fail("Expecting exception");
133 } catch (Exception e) {
134 assertEquals("Could not find policy name in the request", e.getMessage());
137 fail("Expecting exception");
141 public void missingNamingType() throws Exception {
142 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
143 Map<String, String> requestElement = makeOneRequest(policyName);
144 requestElement.remove("naming-type");
145 List<Map<String, String>> allElements = new ArrayList<>();
146 allElements.add(requestElement);
148 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
149 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
153 fail("Expecting exception");
154 } catch (Exception e) {
155 assertEquals("Could not find naming type in the request for policy "
156 + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq",
160 fail("Expecting exception");
164 public void missingRecipe() throws Exception {
165 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
166 Map<String, String> requestElement = makeOneRequest(policyName);
167 List<Map<String, String>> allElements = new ArrayList<>();
168 allElements.add(requestElement);
170 Map<String, Object> policy = new FilePolicyReader("bad_policy_missing_recipe.json").getPolicy();
171 Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(policy);
172 Mockito.lenient().when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true);
173 Mockito.lenient().when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true);
174 Mockito.lenient().when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), anyInt())).thenReturn(1L);
176 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
177 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
181 fail("Expecting exception");
182 } catch (Exception e) {
183 assertEquals("Could not find the recipe for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq "
184 + "and naming-type VNF",
188 fail("Expecting exception");
192 public void missingRecipeOneField() throws Exception {
193 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
194 Map<String, String> requestElement = makeOneRequest(policyName);
195 requestElement.remove("complex");
196 List<Map<String, String>> allElements = new ArrayList<>();
197 allElements.add(requestElement);
199 Map<String, Object> policy = new FilePolicyReader("vnf_policy_seq.json").getPolicy();
200 Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(policy);
202 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
203 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
207 fail("Expecting exception");
208 } catch (Exception e) {
209 assertEquals("Could not find data for recipe item COMPLEX in policy "
210 + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq "
211 + "and naming-type VNF", e.getMessage());
214 fail("Expecting exception");
218 public void missingRecipeMultipleFields() throws Exception {
219 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
220 Map<String, String> requestElement = makeOneRequest(policyName);
221 requestElement.remove("complex");
222 List<Map<String, String>> allElements = new ArrayList<>();
223 allElements.add(requestElement);
225 Map<String, Object> policy = new FilePolicyReader("long_policy.json").getPolicy();
226 Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(policy);
228 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
229 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
233 fail("Expecting exception");
234 } catch (Exception e) {
235 assertEquals("Could not find data for recipe items COMPLEX, Field2, Field3 and Field4 in policy "
236 + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq "
237 + "and naming-type VNF", e.getMessage());
240 fail("Expecting exception");
244 public void missingRecipeTwoFields() throws Exception {
245 String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq";
246 Map<String, String> requestElement = makeOneRequest(policyName);
247 requestElement.put("Field2", "f2");
248 List<Map<String, String>> allElements = new ArrayList<>();
249 allElements.add(requestElement);
251 Map<String, Object> policy = new FilePolicyReader("long_policy.json").getPolicy();
252 Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(policy);
254 NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
255 namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
259 fail("Expecting exception");
260 } catch (Exception e) {
261 assertEquals("Could not find data for recipe items Field3 and Field4 in policy "
262 + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq "
263 + "and naming-type VNF", e.getMessage());
266 fail("Expecting exception");