diff options
author | BT2983 <BT2983@att.com> | 2018-07-23 15:17:35 -0600 |
---|---|---|
committer | BT2983 <BT2983@att.com> | 2018-07-23 15:17:35 -0600 |
commit | 3437d0aa56f7c0630cbd1b9074579f7e0f8a44ae (patch) | |
tree | 09f0f0223f240185bd5704c64877756501f94cbf /ms/neng/src/test/java/org | |
parent | 5a398289dee4b666528aa77f4e38f9c68d35b254 (diff) |
Adding naming micro-service code - tests.
Unit tests.
Change-Id: I14176a8b47764d5e308258fd9b24bf3e301d04d5
Issue-ID: CCSDK-342
Signed-off-by: BT2983 <BT2983@att.com>
Diffstat (limited to 'ms/neng/src/test/java/org')
10 files changed, 1296 insertions, 0 deletions
diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorDependencyEarlierTest.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorDependencyEarlierTest.java new file mode 100644 index 00000000..ccbd4913 --- /dev/null +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorDependencyEarlierTest.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.gen; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister; +import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters; +import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator; +import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator; +import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator; + +@RunWith(MockitoJUnitRunner.class) +public class NameGeneratorDependencyEarlierTest { + @Mock + private PolicyParameters policyParams = mock(PolicyParameters.class); + @Mock + private PolicyFinder policyFinder = mock(PolicyFinder.class); + @Mock + private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class); + @Mock + private DbNameValidator dbValidator = mock(DbNameValidator.class); + @Mock + private AaiNameValidator aaiValidator = mock(AaiNameValidator.class); + @Mock + private NamePersister namePresister = mock(NamePersister.class); + private Map<String, Map<String, String>> earlierNames = new HashMap<>(); + private Map<String, Map<String, ?>> policyCache = new HashMap<>(); + + + protected Map<String, String> makeVnfRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "123456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("complex", "abcdeasdf"); + requestElement.put("naming-type", "VNF"); + requestElement.put("nf-naming-code", "ve1"); + requestElement.put("resource-name", "vnf-name"); + return requestElement; + } + + protected Map<String, String> makeVmRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "923456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("naming-type", "VM"); + requestElement.put("resource-name", "vm-name"); + return requestElement; + } + + /** + * Setup params related data. + */ + @Before + public void setupPolicyParams() throws Exception { + when(policyParams.mapFunction("substr")).thenReturn("substring"); + when(policyParams.mapFunction("to_lower_case")).thenReturn("toLowerCase"); + when(policyParams.mapFunction("to_upper_case")).thenReturn("toUpperCase"); + } + + @Test + public void generate() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNF_VM_NamingPolicy"; + Map<String, String> requestElement1 = makeVnfRequest(policyName); + Map<String, String> requestElement2 = makeVmRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement1); + allElements.add(requestElement2); + + Map<String, Object> policy = new FilePolicyReader("vnf_and_vm_policy.json").getPolicy(); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), anyInt())).thenReturn(1L); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement1, allElements, earlierNames, policyCache); + + Map<String, String> resp = gen.generate(); + assertEquals("vnf-name", resp.get("resource-name")); + assertEquals("123456", resp.get("external-key")); + assertEquals("abcde001ve1", resp.get("resource-value")); + + NameGenerator gen2 = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement2, allElements, earlierNames, policyCache); + + Map<String, String> resp2 = gen2.generate(); + assertEquals("vm-name", resp2.get("resource-name")); + assertEquals("923456", resp2.get("external-key")); + assertEquals("abcde001ve1mts001", resp2.get("resource-value")); + } +} + diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorDependencyLaterTest.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorDependencyLaterTest.java new file mode 100644 index 00000000..009db5da --- /dev/null +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorDependencyLaterTest.java @@ -0,0 +1,125 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.gen; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister; +import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters; +import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator; +import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator; +import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator; + +@RunWith(MockitoJUnitRunner.class) +public class NameGeneratorDependencyLaterTest { + @Mock + private PolicyParameters policyParams = mock(PolicyParameters.class); + @Mock + private PolicyFinder policyFinder = mock(PolicyFinder.class); + @Mock + private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class); + @Mock + private DbNameValidator dbValidator = mock(DbNameValidator.class); + @Mock + private AaiNameValidator aaiValidator = mock(AaiNameValidator.class); + @Mock + private NamePersister namePresister = mock(NamePersister.class); + private Map<String, Map<String, String>> earlierNames = new HashMap<>(); + private Map<String, Map<String, ?>> policyCache = new HashMap<>(); + + protected Map<String, String> makeVnfRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "123456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("complex", "abcdeasdf"); + requestElement.put("naming-type", "VNF"); + requestElement.put("nf-naming-code", "ve1"); + requestElement.put("resource-name", "vnf-name"); + return requestElement; + } + + protected Map<String, String> makeVmRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "923456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("naming-type", "VM"); + requestElement.put("resource-name", "vm-name"); + return requestElement; + } + + /** + * Setup params related data. + */ + @Before + public void setupPolicyParams() throws Exception { + when(policyParams.mapFunction("substr")).thenReturn("substring"); + when(policyParams.mapFunction("to_lower_case")).thenReturn("toLowerCase"); + when(policyParams.mapFunction("to_upper_case")).thenReturn("toUpperCase"); + } + + @Test + public void generate() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNF_VM_NamingPolicy"; + Map<String, String> requestElement1 = makeVmRequest(policyName); + Map<String, String> requestElement2 = makeVnfRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement1); + allElements.add(requestElement2); + + Map<String, Object> policy = new FilePolicyReader("vnf_and_vm_policy.json").getPolicy(); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), anyInt())).thenReturn(1L); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement1, allElements, earlierNames, policyCache); + + Map<String, String> resp = gen.generate(); + assertEquals("vm-name", resp.get("resource-name")); + assertEquals("923456", resp.get("external-key")); + assertEquals("abcde001ve1mts001", resp.get("resource-value")); + + NameGenerator gen2 = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement2, allElements, earlierNames, policyCache); + + Map<String, String> resp2 = gen2.generate(); + assertEquals("vnf-name", resp2.get("resource-name")); + assertEquals("123456", resp2.get("external-key")); + assertEquals("abcde001ve1", resp2.get("resource-value")); + } +} + diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorDependencyOnUnnamedTest.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorDependencyOnUnnamedTest.java new file mode 100644 index 00000000..e4f3ddc2 --- /dev/null +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorDependencyOnUnnamedTest.java @@ -0,0 +1,136 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.gen; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister; +import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters; +import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator; +import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator; +import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator; +import org.onap.ccsdk.apps.ms.neng.persistence.entity.GeneratedName; + +@RunWith(MockitoJUnitRunner.class) +public class NameGeneratorDependencyOnUnnamedTest { + @Mock + private PolicyParameters policyParams = mock(PolicyParameters.class); + @Mock + private PolicyFinder policyFinder = mock(PolicyFinder.class); + @Mock + private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class); + @Mock + private DbNameValidator dbValidator = mock(DbNameValidator.class); + @Mock + private AaiNameValidator aaiValidator = mock(AaiNameValidator.class); + @Mock + private NamePersister namePresister = mock(NamePersister.class); + private Map<String, Map<String, String>> earlierNames = new HashMap<>(); + private Map<String, Map<String, ?>> policyCache = new HashMap<>(); + + /** + * Setup policy params. + */ + @Before + public void setupPolicyParams() throws Exception { + when(policyParams.mapFunction("substr")).thenReturn("substring"); + when(policyParams.mapFunction("to_lower_case")).thenReturn("toLowerCase"); + when(policyParams.mapFunction("to_upper_case")).thenReturn("toUpperCase"); + } + + protected Map<String, String> makeVmRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "923456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("naming-type", "VM"); + requestElement.put("resource-name", "vm-name"); + requestElement.put("complex", "abcdeasdf"); + return requestElement; + } + + @Test + public void generate() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNF_VM_NamingPolicy"; + Map<String, String> requestElement2 = makeVmRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement2); + + Map<String, Object> policy = new FilePolicyReader("vnf_and_vm_policy.json").getPolicy(); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), anyInt())).thenReturn(1L); + + final List<Object> savedNames = new ArrayList<>(); + doAnswer(new Answer<Void>() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + savedNames.add(invocation.getArguments()[0]); + return null; + } + }).when(namePresister).persist(anyObject()); + + NameGenerator gen2 = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement2, allElements, earlierNames, policyCache); + + Map<String, String> resp2 = gen2.generate(); + assertEquals("vm-name", resp2.get("resource-name")); + assertEquals("923456", resp2.get("external-key")); + assertEquals("abcde001ve1mts001", resp2.get("resource-value")); + + assertEquals(2, savedNames.size()); + + assertEquals("abcde001ve1", ((GeneratedName) savedNames.get(0)).getName()); + assertEquals(null, ((GeneratedName) savedNames.get(0)).getExternalId()); + assertEquals("VNF", ((GeneratedName) savedNames.get(0)).getElementType()); + assertEquals("abcde", ((GeneratedName) savedNames.get(0)).getPrefix()); + assertEquals(1, ((GeneratedName) savedNames.get(0)).getSequenceNumber().longValue()); + assertEquals("001", ((GeneratedName) savedNames.get(0)).getSequenceNumberEnc()); + assertEquals("ve1", ((GeneratedName) savedNames.get(0)).getSuffix()); + + assertEquals("abcde001ve1mts001", ((GeneratedName) savedNames.get(1)).getName()); + assertEquals("923456", ((GeneratedName) savedNames.get(1)).getExternalId()); + assertEquals("VM", ((GeneratedName) savedNames.get(1)).getElementType()); + assertEquals("abcde001ve1mts", ((GeneratedName) savedNames.get(1)).getPrefix()); + assertEquals(1, ((GeneratedName) savedNames.get(1)).getSequenceNumber().longValue()); + assertEquals("001", ((GeneratedName) savedNames.get(1)).getSequenceNumberEnc()); + assertEquals(null, ((GeneratedName) savedNames.get(1)).getSuffix()); + } +} + diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorExcMissingData.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorExcMissingData.java new file mode 100644 index 00000000..4b1d1eac --- /dev/null +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorExcMissingData.java @@ -0,0 +1,268 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.gen; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister; +import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters; +import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator; +import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator; +import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator; + +@RunWith(MockitoJUnitRunner.class) +public class NameGeneratorExcMissingData { + @Mock + private PolicyParameters policyParams = mock(PolicyParameters.class); + @Mock + private PolicyFinder policyFinder = mock(PolicyFinder.class); + @Mock + private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class); + @Mock + private DbNameValidator dbValidator = mock(DbNameValidator.class); + @Mock + private AaiNameValidator aaiValidator = mock(AaiNameValidator.class); + @Mock + private NamePersister namePresister = mock(NamePersister.class); + private Map<String, Map<String, String>> earlierNames = new HashMap<>(); + private Map<String, Map<String, ?>> policyCache = new HashMap<>(); + + protected Map<String, String> makeOneRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "123456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("complex", "dlstxasdf"); + requestElement.put("naming-type", "VNF"); + requestElement.put("nf-naming-code", "ve1"); + requestElement.put("resource-name", "vnf-name"); + return requestElement; + } + + @Test + public void missingPolicy() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq"; + Map<String, String> requestElement = makeOneRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + when(policyFinder.findPolicy(policyName)).thenReturn(null); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + try { + gen.generate(); + fail("Expecting exception"); + } catch (Exception e) { + assertEquals("Could not find the policy data for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq", + e.getMessage()); + return; + } + fail("Expecting exception"); + } + + @Test + public void missingPolicyData() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq"; + Map<String, String> requestElement = makeOneRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + try { + gen.generate(); + fail("Expecting exception"); + } catch (Exception e) { + assertEquals("Could not find the policy data for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq " + + "and naming-type VNF", + e.getMessage()); + return; + } + fail("Expecting exception"); + } + + @Test + public void missingPolicyName() throws Exception { + String policyName = null; + Map<String, String> requestElement = makeOneRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + try { + gen.generate(); + fail("Expecting exception"); + } catch (Exception e) { + assertEquals("Could not find policy name in the request", e.getMessage()); + return; + } + fail("Expecting exception"); + } + + @Test + public void missingNamingType() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq"; + Map<String, String> requestElement = makeOneRequest(policyName); + requestElement.remove("naming-type"); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + try { + gen.generate(); + fail("Expecting exception"); + } catch (Exception e) { + assertEquals("Could not find naming type in the request for policy " + + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq", + e.getMessage()); + return; + } + fail("Expecting exception"); + } + + @Test + public void missingRecipe() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq"; + Map<String, String> requestElement = makeOneRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + Map<String, Object> policy = new FilePolicyReader("bad_policy_missing_recipe.json").getPolicy(); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), anyInt())).thenReturn(1L); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + try { + gen.generate(); + fail("Expecting exception"); + } catch (Exception e) { + assertEquals("Could not find the recipe for SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq " + + "and naming-type VNF", + e.getMessage()); + return; + } + fail("Expecting exception"); + } + + @Test + public void missingRecipeOneField() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq"; + Map<String, String> requestElement = makeOneRequest(policyName); + requestElement.remove("complex"); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + Map<String, Object> policy = new FilePolicyReader("vnf_policy_seq.json").getPolicy(); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + try { + gen.generate(); + fail("Expecting exception"); + } catch (Exception e) { + assertEquals("Could not find data for recipe item COMPLEX in policy " + + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq " + + "and naming-type VNF", e.getMessage()); + return; + } + fail("Expecting exception"); + } + + @Test + public void missingRecipeMultipleFields() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq"; + Map<String, String> requestElement = makeOneRequest(policyName); + requestElement.remove("complex"); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + Map<String, Object> policy = new FilePolicyReader("long_policy.json").getPolicy(); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + try { + gen.generate(); + fail("Expecting exception"); + } catch (Exception e) { + assertEquals("Could not find data for recipe items COMPLEX, Field2, Field3 and Field4 in policy " + + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq " + + "and naming-type VNF", e.getMessage()); + return; + } + fail("Expecting exception"); + } + + @Test + public void missingRecipeTwoFields() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq"; + Map<String, String> requestElement = makeOneRequest(policyName); + requestElement.put("Field2", "f2"); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + Map<String, Object> policy = new FilePolicyReader("long_policy.json").getPolicy(); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + try { + gen.generate(); + fail("Expecting exception"); + } catch (Exception e) { + assertEquals("Could not find data for recipe items Field3 and Field4 in policy " + + "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq " + + "and naming-type VNF", e.getMessage()); + return; + } + fail("Expecting exception"); + } +} + diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorNoSequenceTest.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorNoSequenceTest.java new file mode 100644 index 00000000..e46ab083 --- /dev/null +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorNoSequenceTest.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.gen; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister; +import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters; +import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator; +import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator; +import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator; + +@RunWith(MockitoJUnitRunner.class) +public class NameGeneratorNoSequenceTest { + @Mock + private PolicyParameters policyParams = mock(PolicyParameters.class); + @Mock + private PolicyFinder policyFinder = mock(PolicyFinder.class); + @Mock + private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class); + @Mock + private DbNameValidator dbValidator = mock(DbNameValidator.class); + @Mock + private AaiNameValidator aaiValidator = mock(AaiNameValidator.class); + @Mock + private NamePersister namePresister = mock(NamePersister.class); + private Map<String, Map<String, String>> earlierNames = new HashMap<>(); + private Map<String, Map<String, ?>> policyCache = new HashMap<>(); + + protected Map<String, String> makeOneRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "123456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("complex", "dlstx"); + requestElement.put("naming-type", "VNF"); + requestElement.put("nf-naming-code", "ve1"); + requestElement.put("resource-name", "vnf-name"); + return requestElement; + } + + @Test + public void generate() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNFCNamingPolicy_no_seq"; + Map<String, String> requestElement = makeOneRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + Map<String, Object> policy = new FilePolicyReader("vnf_policy_no_seq.json").getPolicy(); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + Map<String, String> resp = gen.generate(); + assertEquals("vnf-name", resp.get("resource-name")); + assertEquals("123456", resp.get("external-key")); + assertEquals("dlstxxyz", resp.get("resource-value")); + } +} + diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorSeqGenErrorsTest.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorSeqGenErrorsTest.java new file mode 100644 index 00000000..46f1e942 --- /dev/null +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorSeqGenErrorsTest.java @@ -0,0 +1,227 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.gen; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister; +import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters; +import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator; +import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator; +import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator; +import org.onap.ccsdk.apps.ms.neng.persistence.entity.GeneratedName; + +@RunWith(MockitoJUnitRunner.class) +public class NameGeneratorSeqGenErrorsTest { + @Mock + private PolicyParameters policyParams = mock(PolicyParameters.class); + @Mock + private PolicyFinder policyFinder = mock(PolicyFinder.class); + @Mock + private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class); + @Mock + private DbNameValidator dbValidator = mock(DbNameValidator.class); + @Mock + private AaiNameValidator aaiValidator = mock(AaiNameValidator.class); + @Mock + private NamePersister namePresister = mock(NamePersister.class); + private Map<String, Map<String, String>> earlierNames = new HashMap<>(); + private Map<String, Map<String, ?>> policyCache = new HashMap<>(); + + protected Map<String, String> makeVmRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "923456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("naming-type", "VM"); + requestElement.put("resource-name", "vm-name"); + requestElement.put("complex", "abcdeasdf"); + return requestElement; + } + + protected Map<String, String> makeVnfRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "123456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("naming-type", "VNF"); + requestElement.put("resource-name", "vnf-name"); + requestElement.put("complex", "abcdeasdf"); + return requestElement; + } + + /** + * Setup policy params. + */ + @Before + public void setupPolicyParams() throws Exception { + when(policyParams.mapFunction("substr")).thenReturn("substring"); + when(policyParams.mapFunction("to_lower_case")).thenReturn("toLowerCase"); + when(policyParams.mapFunction("to_upper_case")).thenReturn("toUpperCase"); + } + + @Test + public void someNamesRejectedByAai() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNF_VM_NamingPolicy"; + Map<String, String> requestElement2 = makeVmRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement2); + + Map<String, Object> policy = new FilePolicyReader("vnf_and_vm_policy.json").getPolicy(); + when(policyParams.getMaxGenAttempt()).thenReturn(100); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + + when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(aaiValidator.validate(eq("VNF"), anyObject())).thenReturn(true); + when(aaiValidator.validate(eq("VNF"), eq("abcde001ve1"))).thenReturn(false); + + when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), eq(1))).thenReturn(1L); + when(sequenceGenerator.generate(eq("abcde"), anyObject(), anyObject(), anyObject(), eq(2))).thenReturn(2L); + + final List<Object> savedNames = new ArrayList<>(); + doAnswer(new Answer<Void>() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + savedNames.add(invocation.getArguments()[0]); + return null; + } + }).when(namePresister).persist(anyObject()); + + NameGenerator gen2 = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement2, allElements, earlierNames, policyCache); + + Map<String, String> resp2 = gen2.generate(); + assertEquals("vm-name", resp2.get("resource-name")); + assertEquals("923456", resp2.get("external-key")); + assertEquals("abcde002ve1mts001", resp2.get("resource-value")); + + assertEquals(3, savedNames.size()); + + { + GeneratedName genName = (GeneratedName) savedNames.get(0); + assertEquals("abcde001ve1", genName.getName()); + assertEquals("AAI-BACKPOPULATE", genName.getExternalId()); + assertEquals("VNF", genName.getElementType()); + assertEquals("abcde", genName.getPrefix()); + assertEquals(1, genName.getSequenceNumber().longValue()); + assertEquals("001", genName.getSequenceNumberEnc()); + assertEquals("ve1", genName.getSuffix()); + } + { + GeneratedName genName = (GeneratedName) savedNames.get(1); + assertEquals("abcde002ve1", genName.getName()); + assertEquals(null, genName.getExternalId()); + assertEquals("VNF", genName.getElementType()); + assertEquals("abcde", genName.getPrefix()); + assertEquals(2, genName.getSequenceNumber().longValue()); + assertEquals("002", genName.getSequenceNumberEnc()); + assertEquals("ve1", genName.getSuffix()); + + } + { + GeneratedName genName = (GeneratedName) savedNames.get(2); + assertEquals("abcde002ve1mts001", genName.getName()); + assertEquals("923456", genName.getExternalId()); + assertEquals("VM", genName.getElementType()); + assertEquals("abcde002ve1mts", genName.getPrefix()); + assertEquals(1, genName.getSequenceNumber().longValue()); + assertEquals("001", genName.getSequenceNumberEnc()); + assertEquals(null, genName.getSuffix()); + + } + } + + @Test + public void allRejected() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNF_VM_NamingPolicy"; + Map<String, String> requestElement2 = makeVmRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement2); + + Map<String, Object> policy = new FilePolicyReader("vnf_and_vm_policy.json").getPolicy(); + when(policyParams.getMaxGenAttempt()).thenReturn(3); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + when(dbValidator.validate(anyObject(), anyObject())).thenReturn(false); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), eq(1))).thenReturn(1L); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), eq(2))).thenReturn(2L); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), eq(3))).thenReturn(3L); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement2, allElements, earlierNames, policyCache); + + try { + gen.generate(); + fail("Expecting exception"); + } catch (Exception e) { + assertEquals("Could not generate a name successfully for policy " + + "SDNC_Policy.Config_MS_VNF_VM_NamingPolicy and naming-type VNF " + + "even after 3 attempts.", e.getMessage()); + return; + } + fail("Expecting exception"); + } + + @Test + public void allRejectedSequenceLess() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNF_VM_NamingPolicy"; + Map<String, String> requestElement2 = makeVnfRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement2); + + Map<String, Object> policy = new FilePolicyReader("vnf_policy_no_seq.json").getPolicy(); + when(policyParams.getMaxGenAttempt()).thenReturn(3); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + when(dbValidator.validate(anyObject(), anyObject())).thenReturn(false); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement2, allElements, earlierNames, policyCache); + + try { + gen.generate(); + fail("Expecting exception"); + } catch (Exception e) { + assertEquals("Could not generate a valid name successfully for policy " + + "SDNC_Policy.Config_MS_VNF_VM_NamingPolicy " + + "and naming-type VNF. DB Name validation failed", e.getMessage()); + return; + } + fail("Expecting exception"); + } +} + diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorSequenceMultiTest.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorSequenceMultiTest.java new file mode 100644 index 00000000..41f0531a --- /dev/null +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorSequenceMultiTest.java @@ -0,0 +1,114 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.gen; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister; +import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters; +import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator; +import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator; +import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator; + +@RunWith(MockitoJUnitRunner.class) +public class NameGeneratorSequenceMultiTest { + @Mock + private PolicyParameters policyParams = mock(PolicyParameters.class); + @Mock + private PolicyFinder policyFinder = mock(PolicyFinder.class); + @Mock + private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class); + @Mock + private DbNameValidator dbValidator = mock(DbNameValidator.class); + @Mock + private AaiNameValidator aaiValidator = mock(AaiNameValidator.class); + @Mock + private NamePersister namePresister = mock(NamePersister.class); + private Map<String, Map<String, String>> earlierNames = new HashMap<>(); + private Map<String, Map<String, ?>> policyCache = new HashMap<>(); + + protected Map<String, String> makeOneRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "123456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("complex", "abcdeasdf"); + requestElement.put("naming-type", "VNF"); + requestElement.put("nf-naming-code", "ve1"); + requestElement.put("resource-name", "vnf-name"); + return requestElement; + } + + /** + * Setup for policy params. + */ + @Before + public void setupPolicyParams() throws Exception { + when(policyParams.mapFunction("substr")).thenReturn("substring"); + when(policyParams.mapFunction("to_lower_case")).thenReturn("toLowerCase"); + when(policyParams.mapFunction("to_upper_case")).thenReturn("toUpperCase"); + } + + @Test + public void generate() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNFCNamingPolicy_no_seq"; + Map<String, String> requestElement = makeOneRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + Map<String, Object> policy = new FilePolicyReader("vnf_policy_seq.json").getPolicy(); + when(policyParams.getMaxGenAttempt()).thenReturn(100); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(dbValidator.validate(anyObject(), eq("abcde001ve1"))).thenReturn(false); + when(dbValidator.validate(anyObject(), eq("abcde002ve1"))).thenReturn(false); + when(dbValidator.validate(anyObject(), eq("abcde003ve1"))).thenReturn(false); + when(dbValidator.validate(anyObject(), eq("abcde004ve1"))).thenReturn(true); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), eq(1))).thenReturn(1L); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), eq(2))).thenReturn(2L); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), eq(3))).thenReturn(3L); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), eq(4))).thenReturn(4L); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + Map<String, String> resp = gen.generate(); + assertEquals("vnf-name", resp.get("resource-name")); + assertEquals("123456", resp.get("external-key")); + assertEquals("abcde004ve1", resp.get("resource-value")); + } +} + diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorSequenceTest.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorSequenceTest.java new file mode 100644 index 00000000..9501522d --- /dev/null +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGeneratorSequenceTest.java @@ -0,0 +1,106 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.gen; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister; +import org.onap.ccsdk.apps.ms.neng.core.policy.FilePolicyReader; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters; +import org.onap.ccsdk.apps.ms.neng.core.seq.SequenceGenerator; +import org.onap.ccsdk.apps.ms.neng.core.validator.AaiNameValidator; +import org.onap.ccsdk.apps.ms.neng.core.validator.DbNameValidator; + +@RunWith(MockitoJUnitRunner.class) +public class NameGeneratorSequenceTest { + @Mock + private PolicyParameters policyParams = mock(PolicyParameters.class); + @Mock + private PolicyFinder policyFinder = mock(PolicyFinder.class); + @Mock + private SequenceGenerator sequenceGenerator = mock(SequenceGenerator.class); + @Mock + private DbNameValidator dbValidator = mock(DbNameValidator.class); + @Mock + private AaiNameValidator aaiValidator = mock(AaiNameValidator.class); + @Mock + private NamePersister namePresister = mock(NamePersister.class); + private Map<String, Map<String, String>> earlierNames = new HashMap<>(); + private Map<String, Map<String, ?>> policyCache = new HashMap<>(); + + protected Map<String, String> makeOneRequest(String policy) { + Map<String, String> requestElement = new HashMap<>(); + requestElement.put("external-key", "123456"); + requestElement.put("policy-instance-name", policy); + requestElement.put("complex", "abcdeasdf"); + requestElement.put("naming-type", "VNF"); + requestElement.put("nf-naming-code", "ve1"); + requestElement.put("resource-name", "vnf-name"); + return requestElement; + } + + /** + * Setup for policy params. + */ + @Before + public void setupPolicyParams() throws Exception { + when(policyParams.mapFunction("substr")).thenReturn("substring"); + when(policyParams.mapFunction("to_lower_case")).thenReturn("toLowerCase"); + when(policyParams.mapFunction("to_upper_case")).thenReturn("toUpperCase"); + } + + @Test + public void generate() throws Exception { + String policyName = "SDNC_Policy.Config_MS_VNFNamingPolicy_no_seq"; + Map<String, String> requestElement = makeOneRequest(policyName); + List<Map<String, String>> allElements = new ArrayList<>(); + allElements.add(requestElement); + + Map<String, Object> policy = new FilePolicyReader("vnf_policy_seq.json").getPolicy(); + when(policyFinder.findPolicy(policyName)).thenReturn(policy); + when(aaiValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(dbValidator.validate(anyObject(), anyObject())).thenReturn(true); + when(sequenceGenerator.generate(anyObject(), anyObject(), anyObject(), anyObject(), anyInt())).thenReturn(1L); + + NameGenerator gen = new NameGenerator(policyFinder, policyParams, sequenceGenerator, dbValidator, aaiValidator, + namePresister, requestElement, allElements, earlierNames, policyCache); + + Map<String, String> resp = gen.generate(); + assertEquals("vnf-name", resp.get("resource-name")); + assertEquals("123456", resp.get("external-key")); + assertEquals("abcde001ve1", resp.get("resource-value")); + } +} + diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/SequenceFormatterTest.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/SequenceFormatterTest.java new file mode 100644 index 00000000..df28ccce --- /dev/null +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/gen/SequenceFormatterTest.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.gen; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicySequence; + +public class SequenceFormatterTest { + @Test + public void formatSequence() throws Exception { + PolicySequence poly = new PolicySequence(); + poly.setLength(3);; + assertEquals("001", SequenceFormatter.formatSequence(1, poly)); + poly.setLength(2);; + assertEquals("01", SequenceFormatter.formatSequence(1, poly)); + poly.setLength(4);; + assertEquals("0999", SequenceFormatter.formatSequence(999, poly)); + } + + @Test + public void formatSequenceAlpha() throws Exception { + PolicySequence poly = new PolicySequence(); + poly.setLength(3);; + poly.setType(PolicySequence.Type.ALPHA); + assertEquals("001", SequenceFormatter.formatSequence(1, poly)); + poly.setLength(2);; + assertEquals("01", SequenceFormatter.formatSequence(1, poly)); + poly.setLength(4);; + assertEquals("000b", SequenceFormatter.formatSequence(11, poly)); + } +} diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/policy/FilePolicyReader.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/policy/FilePolicyReader.java new file mode 100644 index 00000000..4056d0e1 --- /dev/null +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/core/policy/FilePolicyReader.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.policy; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Map; + +/** + * Reads policy from a file. + */ +public class FilePolicyReader extends PolicyReader { + private String file; + + public FilePolicyReader(String file) { + this.file = file; + } + + public Map<String, Object> findPolicy(String name) throws Exception { + return getPolicy(); + } + + public Map<String, Object> getPolicy() throws Exception { + return getPolicy(readFromFile(file)); + } + + protected String readFromFile(String name) throws Exception { + String path = "src/test/resources/" + name; + String value = new String(Files.readAllBytes(Paths.get(path))); + return value; + } +} |