diff options
author | Tomasz Golabek <tomasz.golabek@nokia.com> | 2019-08-23 10:48:28 +0200 |
---|---|---|
committer | Tomasz Golabek <tomasz.golabek@nokia.com> | 2019-08-23 11:44:42 +0000 |
commit | 3c3782f1622ddc60c9e17dd6b166e476c38f001b (patch) | |
tree | 17fb5856b3d7eeb95a93eddeb3f07ef70fcfd864 /catalog-be/src/test/java | |
parent | 9b795c16772a1517172250cdbfaa0bbf903cf322 (diff) |
unit tests - catalog-be
Additional junit tests
Change-Id: Id83ab89115bd7bec471f42d3b96a2054ff4ead53
Issue-ID: SDC-2326
Signed-off-by: Tomasz Golabek <tomasz.golabek@nokia.com>
Diffstat (limited to 'catalog-be/src/test/java')
7 files changed, 505 insertions, 0 deletions
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/distribution/AuditDistributionEngineEventFactoryManagerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/distribution/AuditDistributionEngineEventFactoryManagerTest.java new file mode 100644 index 0000000000..b7b6cb9ab4 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/distribution/AuditDistributionEngineEventFactoryManagerTest.java @@ -0,0 +1,80 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.openecomp.sdc.be.auditing.impl.distribution; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum.ADD_KEY_TO_TOPIC_ACL; +import static org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum.CREATE_DISTRIBUTION_TOPIC; +import static org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum.GET_UEB_CLUSTER; +import static org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum.REMOVE_KEY_FROM_TOPIC_ACL; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.be.auditing.api.AuditEventFactory; +import org.openecomp.sdc.be.resources.data.auditing.model.DistributionTopicData; + +@RunWith(MockitoJUnitRunner.class) +public class AuditDistributionEngineEventFactoryManagerTest { + + @Mock + private DistributionTopicData distributionTopicData; + private static final String STATUS = "status"; + private static final String APIKEY = "apikey"; + private static final String ROLE = "role"; + private static final String ENVNAME = "envname"; + + @Test + public void shouldCreateDistributionEngineEventFactoryForCreateDistributionTopic() { + AuditEventFactory distributionEngineEventFactory = AuditDistributionEngineEventFactoryManager + .createDistributionEngineEventFactory(CREATE_DISTRIBUTION_TOPIC, + ENVNAME, distributionTopicData, ROLE, APIKEY, STATUS); + + assertThat(distributionEngineEventFactory, instanceOf(AuditCreateTopicDistributionEngineEventFactory.class)); + } + + @Test + public void shouldCreateDistributionEngineEventFactoryForAddKeyToTopicAcl() { + AuditEventFactory distributionEngineEventFactory = AuditDistributionEngineEventFactoryManager + .createDistributionEngineEventFactory(ADD_KEY_TO_TOPIC_ACL, + ENVNAME, distributionTopicData, ROLE, APIKEY, STATUS); + + assertThat(distributionEngineEventFactory, instanceOf(AuditAddRemoveKeyDistributionEngineEventFactory.class)); + } + + + @Test + public void shouldCreateDistributionEngineEventFactoryForRemovekeyFromTopicAcl() { + AuditEventFactory distributionEngineEventFactory = AuditDistributionEngineEventFactoryManager + .createDistributionEngineEventFactory(REMOVE_KEY_FROM_TOPIC_ACL, + ENVNAME, distributionTopicData, ROLE, APIKEY, STATUS); + + assertThat(distributionEngineEventFactory, instanceOf(AuditAddRemoveKeyDistributionEngineEventFactory.class)); + } + + @Test(expected = UnsupportedOperationException.class) + public void shouldThrowExceptionOnDifferentAuditingActionEnum() { + AuditDistributionEngineEventFactoryManager + .createDistributionEngineEventFactory(GET_UEB_CLUSTER, + ENVNAME, distributionTopicData, ROLE, APIKEY, STATUS); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/distribution/AuditDistributionEngineEventMigrationFactoryTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/distribution/AuditDistributionEngineEventMigrationFactoryTest.java new file mode 100644 index 0000000000..6d4dd6d823 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/auditing/impl/distribution/AuditDistributionEngineEventMigrationFactoryTest.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.openecomp.sdc.be.auditing.impl.distribution; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData; +import org.openecomp.sdc.be.resources.data.auditing.model.DistributionTopicData; + +@RunWith(MockitoJUnitRunner.class) +public class AuditDistributionEngineEventMigrationFactoryTest { + + @Mock + private CommonAuditData commonFields; + @Mock + private DistributionTopicData distributionTopicData; + + @Test + public void shouldBuildUserNameExtended() { + User user = new User("firstName", "lastName", "userId", "emailAddress", "role", + 1L); + String userName = AuditDistributionEngineEventMigrationFactory.buildUserNameExtended(user); + assertEquals(userName, "userId, firstName lastName, emailAddress, role"); + } + + @Test + public void shouldReturnDefaultValues() { + AuditDistributionEngineEventMigrationFactory auditDistributionEngineEventMigrationFactory = new AuditDistributionEngineEventMigrationFactory( + AuditingActionEnum.ACTIVATE_SERVICE_BY_API, commonFields, distributionTopicData, + "consumerId", "apiKey", "envName", "role", "1"); + assertEquals(auditDistributionEngineEventMigrationFactory.getLogMessageParams().length, 0); + assertEquals(auditDistributionEngineEventMigrationFactory.getLogPattern(), ""); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/MapUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/MapUtilsTest.java new file mode 100644 index 0000000000..5f8c5ec27c --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/MapUtilsTest.java @@ -0,0 +1,114 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.openecomp.sdc.be.components.utils; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Test; + +public class MapUtilsTest { + + private static final String KEY_1 = "key1"; + private static final String KEY_2 = "key2"; + private static final String VALUE_1 = "value1"; + private static final String VALUE_2 = "value2"; + + @Test + public void shouldCompareMapsOfNulls() { + assertTrue(MapUtils.compareMaps(null, null)); + } + + @Test + public void shouldCompareMapIsTrue() { + Map<String, Object> map1 = new HashMap<>(); + Map<String, Object> map2 = new HashMap<>(); + map1.put(KEY_1, VALUE_1); + map2.put(KEY_1, VALUE_1); + assertTrue(MapUtils.compareMaps(map1, map2)); + } + + @Test + public void shouldCompareMapWithNull() { + assertFalse(MapUtils.compareMaps(null, Collections.emptyMap())); + } + + @Test + public void shouldCompareMapsIsFalse() { + Map<String, Object> map1 = new HashMap<>(); + Map<String, Object> map2 = new HashMap<>(); + map1.put(KEY_1, VALUE_1); + map2.put(KEY_2, VALUE_1); + assertFalse(MapUtils.compareMaps(map1, map2)); + } + + @Test + public void shouldHandleSourceAndTargetObjectsOfDifferentValues() { + assertFalse(MapUtils.handleSourceAndTargetObjects(VALUE_1, VALUE_2)); + } + + @Test + public void shouldHandleSourceAndTargetObjectsOfSameValues() { + assertTrue(MapUtils.handleSourceAndTargetObjects(VALUE_1, VALUE_1)); + } + + @Test + public void shouldHandleSourceAndTargetObjectsOfNullValues() { + assertTrue(MapUtils.handleSourceAndTargetObjects(null, null)); + } + + @Test + public void shouldHandleSourceAndTargetObjectsOfNullValueAndNotNullValue() { + assertFalse(MapUtils.handleSourceAndTargetObjects(null, "")); + } + + @Test + public void shouldCompareListsOfNulls() { + assertTrue(MapUtils.compareLists(null, null)); + } + + @Test + public void shouldCompareListsIsTrue() { + List<Object> list1 = new ArrayList<>(); + List<Object> list2 = new ArrayList<>(); + list1.add(VALUE_1); + list2.add(VALUE_1); + assertTrue(MapUtils.compareLists(list1, list2)); + } + + @Test + public void shouldCompareListWithNull() { + assertFalse(MapUtils.compareLists(null, Collections.emptyList())); + } + + @Test + public void shouldCompareListsIsFalse() { + List<Object> list1 = new ArrayList<>(); + List<Object> list2 = new ArrayList<>(); + list1.add(VALUE_1); + list2.add(VALUE_2); + assertFalse(MapUtils.compareLists(list1, list2)); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PropertyValidatorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PropertyValidatorTest.java new file mode 100644 index 0000000000..98d2f8cc7c --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PropertyValidatorTest.java @@ -0,0 +1,90 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.openecomp.sdc.be.components.validation; + +import static org.junit.Assert.assertTrue; + +import fj.data.Either; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; +import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; +import org.openecomp.sdc.exception.ResponseFormat; + +@RunWith(MockitoJUnitRunner.class) +public class PropertyValidatorTest { + + private static final String TEST = "test"; + private static final String VALUE = "VALUE"; + private static final String TYPE = "TYPE"; + + @Mock + private PropertyOperation propertyOperation; + @Mock + private ExceptionUtils exceptionUtils; + @Mock + private ComponentsUtils componentsUtils; + @Mock + private ApplicationDataTypeCache applicationDataTypeCache; + + @Test + public void shouldValidateThinProperties() { + Mockito.when(propertyOperation.validateAndUpdatePropertyValue( + Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Either.left("")); + + PropertyValidator propertyValidator = new PropertyValidator(propertyOperation, componentsUtils, + applicationDataTypeCache, exceptionUtils); + List<PropertyDefinition> props = new ArrayList<>(); + List<PropertyDefinition> dbProps = new ArrayList<>(); + PropertyDefinition prop = new PropertyDefinition(); + prop.setName(TEST); + prop.setValue(VALUE); + prop.setType(TYPE); + props.add(prop); + dbProps.add(prop); + propertyValidator.thinPropertiesValidator(props, dbProps, Collections.emptyMap()); + + Mockito.verify(propertyOperation).validateAndUpdatePropertyValue(TYPE, VALUE, null, Collections.emptyMap()); + } + + @Test + public void shouldIterateOverPropertiesOnInvalidType() { + PropertyValidator propertyValidator = new PropertyValidator(propertyOperation, componentsUtils, + applicationDataTypeCache, exceptionUtils); + List<PropertyDefinition> props = new ArrayList<>(); + PropertyDefinition prop = new PropertyDefinition(); + prop.setName(TEST); + prop.setValue(VALUE); + prop.setType(TYPE); + props.add(prop); + Either<Boolean, ResponseFormat> booleanResponseFormatEither = propertyValidator.iterateOverProperties(props); + + assertTrue(booleanResponseFormatEither.isRight()); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/distribution/AuditHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/distribution/AuditHandlerTest.java new file mode 100644 index 0000000000..d6e122f319 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/distribution/AuditHandlerTest.java @@ -0,0 +1,67 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.openecomp.sdc.be.distribution; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.be.components.distribution.engine.CambriaErrorResponse; +import org.openecomp.sdc.be.components.distribution.engine.SubscriberTypeEnum; +import org.openecomp.sdc.be.distribution.api.client.RegistrationRequest; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; +import org.openecomp.sdc.be.resources.data.auditing.model.DistributionTopicData; + +@RunWith(MockitoJUnitRunner.class) +public class AuditHandlerTest { + + private static final String INSTANCEID = "INSTANCEID"; + + private AuditHandler auditHandler; + @Mock + private RegistrationRequest registrationRequest; + @Mock + private ComponentsUtils componentsUtils; + @Mock + private DistributionTopicData distributionTopicData; + @Mock + private CambriaErrorResponse registerResponse; + + @Before + public void setUp() throws Exception { + auditHandler = new AuditHandler(componentsUtils, INSTANCEID, registrationRequest); + } + + @Test + public void verifyAuditRegisterACLCalls() { + auditHandler.auditRegisterACL(registerResponse, SubscriberTypeEnum.CONSUMER, distributionTopicData); + Mockito.verify(componentsUtils).auditDistributionEngine(AuditingActionEnum.ADD_KEY_TO_TOPIC_ACL, null, distributionTopicData, SubscriberTypeEnum.CONSUMER.name(), null, "0"); + } + + @Test + public void verifyAuditUnRegisterACLCalls() { + auditHandler.auditUnRegisterACL(registerResponse, SubscriberTypeEnum.PRODUCER, distributionTopicData); + Mockito.verify(componentsUtils).auditDistributionEngine(AuditingActionEnum.REMOVE_KEY_FROM_TOPIC_ACL, null, distributionTopicData, SubscriberTypeEnum.PRODUCER.name(), null, "0"); + } + +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/externalupload/utils/ServiceUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/externalupload/utils/ServiceUtilsTest.java new file mode 100644 index 0000000000..95d4b81420 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/externalupload/utils/ServiceUtilsTest.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.openecomp.sdc.externalupload.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.junit.Test; + +public class ServiceUtilsTest { + + private static final String INVALID_MODEL = "Invalid model"; + private static final String OBJ_1 = "obj1"; + private static final String PROP = "prop"; + + @Test + public void shouldCreateObjectUsingSetters() throws Exception { + TestModel testModel = getTestModel(); + Optional<TestModel> objectUsingSetters = ServiceUtils.createObjectUsingSetters(testModel, TestModel.class); + assertNotEquals(objectUsingSetters.orElseThrow(() -> new Exception(INVALID_MODEL)), testModel); + assertEquals(objectUsingSetters.orElseThrow(() -> new Exception(INVALID_MODEL)).getProp(), testModel.getProp()); + } + + @Test + public void shouldGetObjectAsMap() { + TestModel testModel = getTestModel(); + Map<String, Object> objectAsMap = ServiceUtils.getObjectAsMap(testModel); + assertEquals(objectAsMap.size(), 1); + assertEquals(objectAsMap.get(PROP), OBJ_1); + } + + @Test + public void shouldGetClassFieldNames() { + Set<String> classFieldNames = ServiceUtils.getClassFieldNames(TestModel.class); + assertTrue(classFieldNames.contains(PROP)); + } + + private TestModel getTestModel() { + TestModel testModel = new TestModel(); + testModel.setProp(OBJ_1); + return testModel; + } + +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/externalupload/utils/TestModel.java b/catalog-be/src/test/java/org/openecomp/sdc/externalupload/utils/TestModel.java new file mode 100644 index 0000000000..9551bd335b --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/externalupload/utils/TestModel.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. 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.openecomp.sdc.externalupload.utils; + +public class TestModel { + private String prop; + + public String getProp() { + return prop; + } + + public void setProp(String prop) { + this.prop = prop; + } +} |