a28d364b2a610b18ea54fc5e6ced8bbce0704a7c
[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.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;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class NameGeneratorExcMissingDataTest {
49     @Mock
50     private PolicyParameters policyParams = mock(PolicyParameters.class);
51     @Mock
52     private PolicyFinder policyFinder = mock(PolicyFinder.class);
53     @Mock
54     private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class);
55     @Mock
56     private DbNameValidator dbValidator = mock(DbNameValidator.class);
57     @Mock
58     private AaiNameValidator aaiValidator = mock(AaiNameValidator.class);
59     @Mock
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<>();
63
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;
73     }
74
75     @Test
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);
81
82         Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(null);
83
84         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
85                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
86
87         try {
88             gen.generate();
89             fail("Expecting exception");
90         } catch (Exception e) {
91             assertEquals("Could not find the policy data for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq",
92                             e.getMessage());
93             return;
94         }
95         fail("Expecting exception");
96     }
97
98     @Test
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);
104
105         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
106                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
107
108         try {
109             gen.generate();
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",
114                             e.getMessage());
115             return;
116         }
117         fail("Expecting exception");
118     }
119
120     @Test
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);
126
127         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
128                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
129
130         try {
131             gen.generate();
132             fail("Expecting exception");
133         } catch (Exception e) {
134             assertEquals("Could not find policy name in the request", e.getMessage());
135             return;
136         }
137         fail("Expecting exception");
138     }
139
140     @Test
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);
147
148         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
149                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
150
151         try {
152             gen.generate();
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",
157                             e.getMessage());
158             return;
159         }
160         fail("Expecting exception");
161     }
162
163     @Test
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);
169
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);
175
176         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
177                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
178
179         try {
180             gen.generate();
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",
185                             e.getMessage());
186             return;
187         }
188         fail("Expecting exception");
189     }
190
191     @Test
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);
198
199         Map<String, Object> policy = new FilePolicyReader("vnf_policy_seq.json").getPolicy();
200         Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(policy);
201
202         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
203                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
204
205         try {
206             gen.generate();
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());
212             return;
213         }
214         fail("Expecting exception");
215     }
216
217     @Test
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);
224
225         Map<String, Object> policy = new FilePolicyReader("long_policy.json").getPolicy();
226         Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(policy);
227
228         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
229                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
230
231         try {
232             gen.generate();
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());
238             return;
239         }
240         fail("Expecting exception");
241     }
242
243     @Test
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);
250
251         Map<String, Object> policy = new FilePolicyReader("long_policy.json").getPolicy();
252         Mockito.lenient().when(policyFinder.findPolicy(policyName)).thenReturn(policy);
253
254         NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator,
255                         namePresister, requestElement, allElements, earlierNames, policyCache, new ArrayList<>());
256
257         try {
258             gen.generate();
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());
264             return;
265         }
266         fail("Expecting exception");
267     }
268 }
269