fdbf5d3030c4e228b1ac09d38854c3a59e3c4e62
[ccsdk/apps.git] / ms / neng / src / test / java / org / onap / ccsdk / apps / ms / neng / core / gen / NameGeneratorExcMissingDataTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.ccsdk.apps.ms.neng.core.gen;
22
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;
29
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
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;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class NameGeneratorExcMissingDataTest {
48     @Mock
49     private PolicyParameters policyParams = mock(PolicyParameters.class);
50     @Mock
51     private PolicyFinder policyFinder = mock(PolicyFinder.class);
52     @Mock
53     private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class);
54     @Mock
55     private DbNameValidator dbValidator = mock(DbNameValidator.class);
56     @Mock
57     private AaiNameValidator aaiValidator = mock(AaiNameValidator.class);
58     @Mock
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<>();
62
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;
72     }
73
74     @Test
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);
80
81         when(policyFinder.findPolicy(policyName)).thenReturn(null);
82
83         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
84                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
85
86         try {
87             gen.generate();
88             fail("Expecting exception");
89         } catch (Exception e) {
90             assertEquals("Could not find the policy data for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq",
91                             e.getMessage());
92             return;
93         }
94         fail("Expecting exception");
95     }
96
97     @Test
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);
103
104         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
105                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
106
107         try {
108             gen.generate();
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",
113                             e.getMessage());
114             return;
115         }
116         fail("Expecting exception");
117     }
118
119     @Test
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);
125
126         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
127                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
128
129         try {
130             gen.generate();
131             fail("Expecting exception");
132         } catch (Exception e) {
133             assertEquals("Could not find policy name in the request", e.getMessage());
134             return;
135         }
136         fail("Expecting exception");
137     }
138
139     @Test
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);
146
147         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
148                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
149
150         try {
151             gen.generate();
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",
156                             e.getMessage());
157             return;
158         }
159         fail("Expecting exception");
160     }
161
162     @Test
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);
168
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);
174
175         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
176                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
177
178         try {
179             gen.generate();
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",
184                             e.getMessage());
185             return;
186         }
187         fail("Expecting exception");
188     }
189
190     @Test
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);
197
198         Map<String, Object> policy = new FilePolicyReader("vnf_policy_seq.json").getPolicy();
199         when(policyFinder.findPolicy(policyName)).thenReturn(policy);
200
201         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
202                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
203
204         try {
205             gen.generate();
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());
211             return;
212         }
213         fail("Expecting exception");
214     }
215
216     @Test
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);
223
224         Map<String, Object> policy = new FilePolicyReader("long_policy.json").getPolicy();
225         when(policyFinder.findPolicy(policyName)).thenReturn(policy);
226
227         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
228                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
229
230         try {
231             gen.generate();
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());
237             return;
238         }
239         fail("Expecting exception");
240     }
241
242     @Test
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);
249
250         Map<String, Object> policy = new FilePolicyReader("long_policy.json").getPolicy();
251         when(policyFinder.findPolicy(policyName)).thenReturn(policy);
252
253         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
254                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
255
256         try {
257             gen.generate();
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());
263             return;
264         }
265         fail("Expecting exception");
266     }
267 }
268