summaryrefslogtreecommitdiffstats
path: root/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services
diff options
context:
space:
mode:
authorTomasz Golabek <tomasz.golabek@nokia.com>2019-08-29 13:32:30 +0200
committerTomasz Golabek <tomasz.golabek@nokia.com>2019-08-29 13:34:32 +0000
commit0a65bd7d842fb8617b3c6a7b2d4a16af583dc8be (patch)
tree785f6645b7aa2c8ffe4206f3b8dbf721dfd38f02 /openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services
parentfbe82ee87f03ca9f581eeff47101698247cfe8b1 (diff)
unit tests - openecomp-be
Additional junit tests for item-rest-services and unique-type-rest-services Change-Id: I330954ad4eb432c126c7f163f9fdea30d3173cd9 Issue-ID: SDC-2326 Signed-off-by: Tomasz Golabek <tomasz.golabek@nokia.com>
Diffstat (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services')
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/models/SyncEvent.java59
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java102
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ManagersProvider.java48
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java93
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/models/SyncEventTest.java43
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java135
-rw-r--r--openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/VersionsImplTest.java212
7 files changed, 561 insertions, 131 deletions
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/models/SyncEvent.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/models/SyncEvent.java
new file mode 100644
index 0000000000..c057ecf7e0
--- /dev/null
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/models/SyncEvent.java
@@ -0,0 +1,59 @@
+/*-
+ * ============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.sdcrests.item.rest.models;
+
+import java.util.Map;
+import org.openecomp.sdc.notification.dtos.Event;
+
+public class SyncEvent implements Event {
+
+ private String eventType;
+ private String originatorId;
+ private Map<String, Object> attributes;
+ private String entityId;
+
+ public SyncEvent(String eventType, String originatorId, Map<String, Object> attributes, String entityId) {
+ this.eventType = eventType;
+ this.originatorId = originatorId;
+ this.attributes = attributes;
+ this.entityId = entityId;
+ }
+
+ @Override
+ public String getEventType() {
+ return eventType;
+ }
+
+ @Override
+ public String getOriginatorId() {
+ return originatorId;
+ }
+
+ @Override
+ public Map<String, Object> getAttributes() {
+ return attributes;
+ }
+
+ @Override
+ public String getEntityId() {
+ return entityId;
+ }
+
+} \ No newline at end of file
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java
index cdc8615786..4c1a0fb659 100644
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java
@@ -16,23 +16,14 @@
package org.openecomp.sdcrests.item.rest.services;
-import org.openecomp.sdc.activitylog.ActivityLogManager;
-import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
+import com.google.common.annotations.VisibleForTesting;
import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
import org.openecomp.sdc.activitylog.dao.type.ActivityType;
import org.openecomp.sdc.datatypes.model.ItemType;
-import org.openecomp.sdc.itempermissions.PermissionsManager;
-import org.openecomp.sdc.itempermissions.PermissionsManagerFactory;
import org.openecomp.sdc.itempermissions.impl.types.PermissionTypes;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.notification.dtos.Event;
-import org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory;
-import org.openecomp.sdc.notification.services.NotificationPropagationManager;
-import org.openecomp.sdc.versioning.ItemManager;
-import org.openecomp.sdc.versioning.ItemManagerFactory;
-import org.openecomp.sdc.versioning.VersioningManager;
-import org.openecomp.sdc.versioning.VersioningManagerFactory;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.sdc.versioning.dao.types.VersionStatus;
import org.openecomp.sdc.versioning.types.Item;
@@ -40,6 +31,7 @@ import org.openecomp.sdc.versioning.types.ItemStatus;
import org.openecomp.sdc.versioning.types.NotificationEventTypes;
import org.openecomp.sdcrests.item.rest.Items;
import org.openecomp.sdcrests.item.rest.mapping.MapItemToDto;
+import org.openecomp.sdcrests.item.rest.models.SyncEvent;
import org.openecomp.sdcrests.item.rest.services.catalog.notification.Notifier;
import org.openecomp.sdcrests.item.rest.services.catalog.notification.NotifierFactory;
import org.openecomp.sdcrests.item.types.ItemAction;
@@ -68,23 +60,10 @@ import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM
public class ItemsImpl implements Items {
private static final String ONBOARDING_METHOD = "onboardingMethod";
-
- private ItemManager itemManager = ItemManagerFactory.getInstance().createInterface();
-
- private static ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface();
-
- private VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface();
-
- private final PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface();
-
private static final Logger LOGGER = LoggerFactory.getLogger(ItemsImpl.class);
- private NotificationPropagationManager notifier =
- NotificationPropagationManagerFactory.getInstance().createInterface();
-
-
-
private Map<ItemAction, ActionSideAffects> actionSideAffectsMap = new EnumMap<>(ItemAction.class);
+ private ManagersProvider managersProvider;
@PostConstruct
public void initActionSideAffectsMap() {
@@ -94,21 +73,20 @@ public class ItemsImpl implements Items {
.put(ItemAction.RESTORE, new ActionSideAffects(ActivityType.Restore, NotificationEventTypes.RESTORE));
}
-
@Override
public Response actOn(ItemActionRequestDto request, String itemId, String user) {
- Item item = itemManager.get(itemId);
+ Item item = getManagersProvider().getItemManager().get(itemId);
if (item == null) {
return Response.status(Response.Status.NOT_FOUND).entity(new Exception("Item does not exist.")).build();
}
switch (request.getAction()) {
case ARCHIVE:
- itemManager.archive(item);
+ getManagersProvider().getItemManager().archive(item);
break;
case RESTORE:
- itemManager.restore(item);
+ getManagersProvider().getItemManager().restore(item);
break;
default:
}
@@ -134,7 +112,7 @@ public class ItemsImpl implements Items {
GenericCollectionWrapper<ItemDto> results = new GenericCollectionWrapper<>();
MapItemToDto mapper = new MapItemToDto();
- itemManager.list(itemPredicate).stream()
+ getManagersProvider().getItemManager().list(itemPredicate).stream()
.sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime()))
.forEach(item -> results.add(mapper.applyMapping(item, ItemDto.class)));
@@ -144,48 +122,12 @@ public class ItemsImpl implements Items {
@Override
public Response getItem(String itemId, String user) {
- Item item = itemManager.get(itemId);
+ Item item = getManagersProvider().getItemManager().get(itemId);
ItemDto itemDto = new MapItemToDto().applyMapping(item, ItemDto.class);
return Response.ok(itemDto).build();
}
- private class SyncEvent implements Event {
-
- private String eventType;
- private String originatorId;
- private Map<String, Object> attributes;
- private String entityId;
-
- SyncEvent(String eventType, String originatorId, Map<String, Object> attributes, String entityId) {
- this.eventType = eventType;
- this.originatorId = originatorId;
- this.attributes = attributes;
- this.entityId = entityId;
- }
-
- @Override
- public String getEventType() {
- return eventType;
- }
-
- @Override
- public String getOriginatorId() {
- return originatorId;
- }
-
- @Override
- public Map<String, Object> getAttributes() {
- return attributes;
- }
-
- @Override
- public String getEntityId() {
- return entityId;
- }
-
- }
-
private class ActionSideAffects {
private ActivityType activityType;
@@ -198,7 +140,7 @@ public class ItemsImpl implements Items {
}
private Version getLatestVersion(String itemId) {
- List<Version> list = versioningManager.list(itemId);
+ List<Version> list = getManagersProvider().getVersioningManager().list(itemId);
Optional<Version> max = list.stream().max(Version::compareTo);
return max.orElse(null);
@@ -206,21 +148,21 @@ public class ItemsImpl implements Items {
private void execute(Item item, String user) {
notifyUsers(item.getId(), item.getName(), user, this.notificationType);
- activityLogManager.logActivity(
+ getManagersProvider().getActivityLogManager().logActivity(
new ActivityLogEntity(item.getId(), getLatestVersion(item.getId()), this.activityType, user, true,
"", ""));
}
private void notifyUsers(String itemId, String itemName, String userName, NotificationEventTypes eventType) {
Map<String, Object> eventProperties = new HashMap<>();
- eventProperties.put(ITEM_NAME, itemName == null ? itemManager.get(itemId).getName() : itemName);
+ eventProperties.put(ITEM_NAME, itemName == null ? getManagersProvider().getItemManager().get(itemId).getName() : itemName);
eventProperties.put(ITEM_ID, itemId);
eventProperties.put(PERMISSION_USER, userName);
Event syncEvent = new SyncEvent(eventType.getEventName(), itemId, eventProperties, itemId);
try {
- notifier.notifySubscribers(syncEvent, userName);
+ getManagersProvider().getNotificationPropagationManager().notifySubscribers(syncEvent, userName);
} catch (Exception e) {
LOGGER.error("Failed to send sync notification to users subscribed to item '{}'", itemId, e);
}
@@ -282,7 +224,7 @@ public class ItemsImpl implements Items {
String[] permissions = filterValue.split(",");
Set<String> itemIds = new HashSet<>();
for (String permission : permissions) {
- itemIds.addAll(permissionsManager.listUserPermittedItems(user, permission));
+ itemIds.addAll(getManagersProvider().getPermissionsManager().listUserPermittedItems(user, permission));
}
return item -> itemIds.contains(item.getId());
}
@@ -327,4 +269,22 @@ public class ItemsImpl implements Items {
private enum OnboardingMethod {
NetworkPackage, Manual
}
+
+ @VisibleForTesting
+ void setManagersProvider(ManagersProvider managersProvider) {
+ this.managersProvider = managersProvider;
+ }
+
+ @VisibleForTesting
+ Map<ItemAction, ActionSideAffects> getActionSideAffectsMap() {
+ return actionSideAffectsMap;
+ }
+
+ private ManagersProvider getManagersProvider() {
+ if (managersProvider == null){
+ managersProvider = new ManagersProvider();
+ }
+ return managersProvider;
+ }
+
}
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ManagersProvider.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ManagersProvider.java
new file mode 100644
index 0000000000..9d2ca6ef57
--- /dev/null
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ManagersProvider.java
@@ -0,0 +1,48 @@
+/*-
+ * ============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.sdcrests.item.rest.services;
+
+import lombok.Getter;
+import org.openecomp.sdc.activitylog.ActivityLogManager;
+import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
+import org.openecomp.sdc.conflicts.ConflictsManager;
+import org.openecomp.sdc.conflicts.ConflictsManagerFactory;
+import org.openecomp.sdc.itempermissions.PermissionsManager;
+import org.openecomp.sdc.itempermissions.PermissionsManagerFactory;
+import org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory;
+import org.openecomp.sdc.notification.services.NotificationPropagationManager;
+import org.openecomp.sdc.versioning.AsdcItemManager;
+import org.openecomp.sdc.versioning.AsdcItemManagerFactory;
+import org.openecomp.sdc.versioning.ItemManager;
+import org.openecomp.sdc.versioning.ItemManagerFactory;
+import org.openecomp.sdc.versioning.VersioningManager;
+import org.openecomp.sdc.versioning.VersioningManagerFactory;
+
+@Getter
+class ManagersProvider {
+ private PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface();
+ private AsdcItemManager asdcItemManager = AsdcItemManagerFactory.getInstance().createInterface();
+ private VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface();
+ private ConflictsManager conflictsManager = ConflictsManagerFactory.getInstance().createInterface();
+ private ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface();
+ private NotificationPropagationManager notificationPropagationManager = NotificationPropagationManagerFactory
+ .getInstance().createInterface();
+ private ItemManager itemManager = ItemManagerFactory.getInstance().createInterface();
+}
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java
index cd6364f145..114a97a039 100644
--- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/VersionsImpl.java
@@ -16,6 +16,7 @@
package org.openecomp.sdcrests.item.rest.services;
+import com.google.common.annotations.VisibleForTesting;
import org.openecomp.sdc.activitylog.ActivityLogManager;
import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
@@ -45,6 +46,7 @@ import org.openecomp.sdcrests.item.rest.Versions;
import org.openecomp.sdcrests.item.rest.mapping.MapActivityLogEntityToDto;
import org.openecomp.sdcrests.item.rest.mapping.MapRevisionToDto;
import org.openecomp.sdcrests.item.rest.mapping.MapVersionToDto;
+import org.openecomp.sdcrests.item.rest.models.SyncEvent;
import org.openecomp.sdcrests.item.types.*;
import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
import org.springframework.context.annotation.Scope;
@@ -67,20 +69,15 @@ public class VersionsImpl implements Versions {
private static final String COMMIT_ITEM_ACTION = "Commit_Item";
private static final Logger LOGGER = LoggerFactory.getLogger(VersionsImpl.class);
- private PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface();
- private AsdcItemManager asdcItemManager = AsdcItemManagerFactory.getInstance().createInterface();
- private VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface();
- private ConflictsManager conflictsManager = ConflictsManagerFactory.getInstance().createInterface();
- private ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface();
- private NotificationPropagationManager notifier =
- NotificationPropagationManagerFactory.getInstance().createInterface();
+ private ManagersProvider managersProvider;
@Override
public Response list(String itemId, String user) {
GenericCollectionWrapper<VersionDto> results = new GenericCollectionWrapper<>();
MapVersionToDto mapper = new MapVersionToDto();
- versioningManager.list(itemId).forEach(version -> results.add(mapper.applyMapping(version, VersionDto.class)));
+ getManagersProvider().getVersioningManager().list(itemId)
+ .forEach(version -> results.add(mapper.applyMapping(version, VersionDto.class)));
return Response.ok(results).build();
}
@@ -90,13 +87,13 @@ public class VersionsImpl implements Versions {
version.setBaseId(baseVersionId);
version.setDescription(request.getDescription());
- version = versioningManager
- .create(itemId, version, VersionCreationMethod.valueOf(request.getCreationMethod().name()));
+ version = getManagersProvider().getVersioningManager()
+ .create(itemId, version, VersionCreationMethod.valueOf(request.getCreationMethod().name()));
VersionDto versionDto = new MapVersionToDto().applyMapping(version, VersionDto.class);
- activityLogManager
- .logActivity(new ActivityLogEntity(itemId, version, ActivityType.Create_Version, user, true, "", ""));
+ getManagersProvider().getActivityLogManager().logActivity(
+ new ActivityLogEntity(itemId, version, ActivityType.Create_Version, user, true, "", ""));
return Response.ok(versionDto).build();
}
@@ -113,7 +110,7 @@ public class VersionsImpl implements Versions {
GenericCollectionWrapper<ActivityLogDto> results = new GenericCollectionWrapper<>();
MapActivityLogEntityToDto mapper = new MapActivityLogEntityToDto();
- activityLogManager.listLoggedActivities(itemId, new Version(versionId))
+ getManagersProvider().getActivityLogManager().listLoggedActivities(itemId, new Version(versionId))
.forEach(loggedActivity -> results.add(mapper.applyMapping(loggedActivity, ActivityLogDto.class)));
return Response.ok(results).build();
@@ -121,7 +118,7 @@ public class VersionsImpl implements Versions {
@Override
public Response listRevisions(String itemId, String versionId, String user) {
- List<Revision> revisions = versioningManager.listRevisions(itemId, new Version(versionId));
+ List<Revision> revisions = getManagersProvider().getVersioningManager().listRevisions(itemId, new Version(versionId));
filterRevisions(revisions);
GenericCollectionWrapper<RevisionDto> results = new GenericCollectionWrapper<>();
@@ -138,7 +135,7 @@ public class VersionsImpl implements Versions {
sync(itemId, version);
break;
case Commit:
- if (!permissionsManager.isAllowed(itemId, user, COMMIT_ITEM_ACTION)) {
+ if (!getManagersProvider().getPermissionsManager().isAllowed(itemId, user, COMMIT_ITEM_ACTION)) {
return Response.status(Response.Status.FORBIDDEN)
.entity(new Exception(Messages.PERMISSIONS_ERROR.getErrorMessage())).build();
}
@@ -148,7 +145,7 @@ public class VersionsImpl implements Versions {
revert(request.getRevisionRequest(), itemId, versionId);
break;
case Clean:
- versioningManager.clean(itemId, version);
+ getManagersProvider().getVersioningManager().clean(itemId, version);
break;
default:
throw new UnsupportedOperationException(String.format("Action %s not supported.", request.getAction()));
@@ -157,29 +154,29 @@ public class VersionsImpl implements Versions {
}
private Version getVersion(String itemId, Version version) {
- Version retrievedVersion = versioningManager.get(itemId, version);
+ Version retrievedVersion = getManagersProvider().getVersioningManager().get(itemId, version);
if (retrievedVersion.getState().getSynchronizationState() != SynchronizationState.Merging &&
// looks for sdc applicative conflicts
- conflictsManager.isConflicted(itemId, retrievedVersion)) {
+ getManagersProvider().getConflictsManager().isConflicted(itemId, retrievedVersion)) {
retrievedVersion.getState().setSynchronizationState(SynchronizationState.Merging);
}
return retrievedVersion;
}
private void sync(String itemId, Version version) {
- versioningManager.sync(itemId, version);
- conflictsManager.finalizeMerge(itemId, version);
+ getManagersProvider().getVersioningManager().sync(itemId, version);
+ getManagersProvider().getConflictsManager().finalizeMerge(itemId, version);
}
private void commit(CommitRequestDto request, String itemId, Version version, String user) {
String message = request == null ? "" : request.getMessage();
- versioningManager.publish(itemId, version, message);
+ getManagersProvider().getVersioningManager().publish(itemId, version, message);
notifyUsers(itemId, version, message, user, NotificationEventTypes.COMMIT);
- activityLogManager
- .logActivity(new ActivityLogEntity(itemId, version, ActivityType.Commit, user, true, "", message));
+ getManagersProvider().getActivityLogManager().logActivity(
+ new ActivityLogEntity(itemId, version, ActivityType.Commit, user, true, "", message));
}
private void revert(RevisionRequestDto request, String itemId, String versionId) {
@@ -187,7 +184,7 @@ public class VersionsImpl implements Versions {
throw new CoreException(new RevisionIdNotFoundErrorBuilder().build());
}
- versioningManager.revert(itemId, new Version(versionId), request.getRevisionId());
+ getManagersProvider().getVersioningManager().revert(itemId, new Version(versionId), request.getRevisionId());
}
private void filterRevisions(List<Revision> revisions) {
@@ -208,10 +205,10 @@ public class VersionsImpl implements Versions {
private void notifyUsers(String itemId, Version version, String message, String userName,
NotificationEventTypes eventType) {
Map<String, Object> eventProperties = new HashMap<>();
- eventProperties.put(ITEM_NAME, asdcItemManager.get(itemId).getName());
+ eventProperties.put(ITEM_NAME, getManagersProvider().getAsdcItemManager().get(itemId).getName());
eventProperties.put(ITEM_ID, itemId);
- Version ver = versioningManager.get(itemId, version);
+ Version ver = getManagersProvider().getVersioningManager().get(itemId, version);
eventProperties.put(VERSION_NAME, ver.getName());
eventProperties.put(VERSION_ID, ver.getId());
@@ -220,46 +217,22 @@ public class VersionsImpl implements Versions {
Event syncEvent = new SyncEvent(eventType.getEventName(), itemId, eventProperties, itemId);
try {
- notifier.notifySubscribers(syncEvent, userName);
+ getManagersProvider().getNotificationPropagationManager().notifySubscribers(syncEvent, userName);
} catch (Exception e) {
LOGGER.error("Failed to send sync notification to users subscribed o item '" + itemId);
}
}
- private class SyncEvent implements Event {
-
- private String eventType;
- private String originatorId;
- private Map<String, Object> attributes;
-
- private String entityId;
-
- public SyncEvent(String eventType, String originatorId, Map<String, Object> attributes, String entityId) {
- this.eventType = eventType;
- this.originatorId = originatorId;
- this.attributes = attributes;
- this.entityId = entityId;
- }
-
- @Override
- public String getEventType() {
- return eventType;
- }
-
- @Override
- public String getOriginatorId() {
- return originatorId;
- }
-
- @Override
- public Map<String, Object> getAttributes() {
- return attributes;
- }
+ @VisibleForTesting
+ void setManagersProvider(ManagersProvider managersProvider) {
+ this.managersProvider = managersProvider;
+ }
- @Override
- public String getEntityId() {
- return entityId;
+ private ManagersProvider getManagersProvider() {
+ if (managersProvider == null){
+ managersProvider = new ManagersProvider();
}
-
+ return managersProvider;
}
+
}
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/models/SyncEventTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/models/SyncEventTest.java
new file mode 100644
index 0000000000..a023f4a2a8
--- /dev/null
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/models/SyncEventTest.java
@@ -0,0 +1,43 @@
+/*-
+ * ============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.sdcrests.item.rest.models;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Collections;
+import java.util.Map;
+import org.junit.Test;
+
+public class SyncEventTest {
+
+ private static final String EVENT = "EVENT";
+ private static final String ORIGINATOR = "ORIGINATOR";
+ private static final String ENTITY_ID = "ENTITY";
+ private static final Map<String, Object> ATTRIBUTES = Collections.emptyMap();
+
+ @Test
+ public void shouldHaveValidGetters() {
+ SyncEvent syncEvent = new SyncEvent(EVENT, ORIGINATOR, ATTRIBUTES, ENTITY_ID);
+ assertEquals(syncEvent.getAttributes(), ATTRIBUTES);
+ assertEquals(syncEvent.getEntityId(), ENTITY_ID);
+ assertEquals(syncEvent.getEventType(), EVENT);
+ assertEquals(syncEvent.getOriginatorId(), ORIGINATOR);
+ }
+} \ No newline at end of file
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java
new file mode 100644
index 0000000000..d7e9c81823
--- /dev/null
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java
@@ -0,0 +1,135 @@
+/*-
+ * ============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.sdcrests.item.rest.services;
+
+import static org.junit.Assert.assertEquals;
+import static org.openecomp.sdcrests.item.types.ItemAction.ARCHIVE;
+import static org.openecomp.sdcrests.item.types.ItemAction.RESTORE;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.ws.rs.core.Response;
+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.activitylog.ActivityLogManager;
+import org.openecomp.sdc.versioning.ItemManager;
+import org.openecomp.sdc.versioning.VersioningManager;
+import org.openecomp.sdc.versioning.dao.types.Version;
+import org.openecomp.sdc.versioning.types.Item;
+import org.openecomp.sdcrests.item.types.ItemActionRequestDto;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ItemsImplTest {
+
+ private static final String ITEM_ID = "ITEM_ID";
+ private static final String USER = "USER";
+
+ @Mock
+ private ManagersProvider managersProvider;
+ @Mock
+ private ItemManager itemManager;
+ @Mock
+ private ItemActionRequestDto request;
+ @Mock
+ private Item item;
+ @Mock
+ private VersioningManager versioningManager;
+ @Mock
+ private ActivityLogManager activityManager;
+
+ @Test
+ public void shouldInitActionSideAffectsMap() {
+ ItemsImpl items = new ItemsImpl();
+ items.initActionSideAffectsMap();
+ assertEquals(items.getActionSideAffectsMap().size(),2);
+ }
+
+ @Test
+ public void shouldActOnEmptyItem() {
+ ItemsImpl items = new ItemsImpl();
+ items.initActionSideAffectsMap();
+ items.setManagersProvider(managersProvider);
+ Mockito.when(managersProvider.getItemManager()).thenReturn(itemManager);
+ Mockito.when(itemManager.get(Mockito.any())).thenReturn(null);
+ Response response = items.actOn(request, ITEM_ID, USER);
+ assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
+ }
+
+ @Test
+ public void shouldActOnARCHIVE() {
+ ItemsImpl items = new ItemsImpl();
+ items.initActionSideAffectsMap();
+ items.setManagersProvider(managersProvider);
+ Mockito.when(itemManager.get(Mockito.any())).thenReturn(item);
+ Mockito.when(request.getAction()).thenReturn(ARCHIVE);
+ Mockito.when(managersProvider.getItemManager()).thenReturn(itemManager);
+ Mockito.when(managersProvider.getVersioningManager()).thenReturn(versioningManager);
+ Mockito.when(versioningManager.list(Mockito.any())).thenReturn(getVersions());
+ Mockito.when(managersProvider.getActivityLogManager()).thenReturn(activityManager);
+ items.actOn(request, ITEM_ID, USER);
+ Mockito.verify(itemManager).archive(Mockito.any());
+ }
+
+ @Test
+ public void shouldActOnRESTORE() {
+ ItemsImpl items = new ItemsImpl();
+ items.initActionSideAffectsMap();
+ items.setManagersProvider(managersProvider);
+ Mockito.when(itemManager.get(Mockito.any())).thenReturn(item);
+ Mockito.when(request.getAction()).thenReturn(RESTORE);
+ Mockito.when(managersProvider.getItemManager()).thenReturn(itemManager);
+ Mockito.when(managersProvider.getVersioningManager()).thenReturn(versioningManager);
+ Mockito.when(versioningManager.list(Mockito.any())).thenReturn(getVersions());
+ Mockito.when(managersProvider.getActivityLogManager()).thenReturn(activityManager);
+ items.actOn(request, ITEM_ID, USER);
+ Mockito.verify(itemManager).restore(Mockito.any());
+ }
+
+ @Test
+ public void shouldGetItem() {
+ ItemsImpl items = new ItemsImpl();
+ items.initActionSideAffectsMap();
+ items.setManagersProvider(managersProvider);
+ Mockito.when(managersProvider.getItemManager()).thenReturn(itemManager);
+ Response response = items.getItem(ITEM_ID, USER);
+ assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
+ }
+
+ @Test
+ public void shouldList() {
+ ItemsImpl items = new ItemsImpl();
+ items.initActionSideAffectsMap();
+ items.setManagersProvider(managersProvider);
+ Mockito.when(managersProvider.getItemManager()).thenReturn(itemManager);
+ Response response = items.list(null, null, null, null, null, USER);
+ assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
+ }
+
+ private List<Version> getVersions() {
+ List<Version> versions = new ArrayList<>();
+ versions.add(new Version("1"));
+ versions.add(new Version("2"));
+ versions.add(new Version("3"));
+ return versions;
+ }
+} \ No newline at end of file
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/VersionsImplTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/VersionsImplTest.java
new file mode 100644
index 0000000000..8115051e7c
--- /dev/null
+++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/VersionsImplTest.java
@@ -0,0 +1,212 @@
+/*-
+ * ============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.sdcrests.item.rest.services;
+
+import static org.junit.Assert.assertEquals;
+import static org.openecomp.sdcrests.item.types.VersionAction.Clean;
+import static org.openecomp.sdcrests.item.types.VersionAction.Commit;
+import static org.openecomp.sdcrests.item.types.VersionAction.Reset;
+import static org.openecomp.sdcrests.item.types.VersionAction.Revert;
+import static org.openecomp.sdcrests.item.types.VersionAction.Sync;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+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.activitylog.ActivityLogManager;
+import org.openecomp.sdc.common.errors.CoreException;
+import org.openecomp.sdc.conflicts.ConflictsManager;
+import org.openecomp.sdc.itempermissions.PermissionsManager;
+import org.openecomp.sdc.notification.services.NotificationPropagationManager;
+import org.openecomp.sdc.versioning.AsdcItemManager;
+import org.openecomp.sdc.versioning.VersioningManager;
+import org.openecomp.sdc.versioning.dao.types.Revision;
+import org.openecomp.sdc.versioning.dao.types.SynchronizationState;
+import org.openecomp.sdc.versioning.dao.types.Version;
+import org.openecomp.sdc.versioning.dao.types.VersionState;
+import org.openecomp.sdc.versioning.types.Item;
+import org.openecomp.sdcrests.item.types.RevisionRequestDto;
+import org.openecomp.sdcrests.item.types.VersionActionRequestDto;
+
+@RunWith(MockitoJUnitRunner.class)
+public class VersionsImplTest {
+
+ private static final String ITEM_ID = "ITEM_ID";
+ private static final String VERSION_ID = "VERSION_ID";
+ private static final String USER = "USER";
+ private static final String REVISION_ID = "REVISION_ID";
+
+ @Mock
+ private ManagersProvider managersProvider;
+ @Mock
+ private VersionActionRequestDto request;
+ @Mock
+ private RevisionRequestDto revisionRequest;
+ @Mock
+ private VersioningManager versioningManager;
+ @Mock
+ private PermissionsManager permManager;
+ @Mock
+ private ActivityLogManager activityManager;
+ @Mock
+ private AsdcItemManager asdcManager;
+ @Mock
+ private Item item;
+ @Mock
+ private Version version;
+ @Mock
+ private NotificationPropagationManager notificationManager;
+ @Mock
+ private ConflictsManager conflictsManager;
+ @Mock
+ private VersionState state;
+
+ @Test
+ public void shouldActOnSync() {
+ VersionsImpl versions = new VersionsImpl();
+ versions.setManagersProvider(managersProvider);
+ Mockito.when(request.getAction()).thenReturn(Sync);
+ Mockito.when(managersProvider.getVersioningManager()).thenReturn(versioningManager);
+ Mockito.when(managersProvider.getConflictsManager()).thenReturn(conflictsManager);
+ versions.actOn(request, ITEM_ID, VERSION_ID, USER);
+ Mockito.verify(versioningManager).sync(Mockito.any(), Mockito.any());
+ Mockito.verify(conflictsManager).finalizeMerge(Mockito.any(), Mockito.any());
+ }
+
+ @Test
+ public void shouldActOnCommitWhenAllowed() {
+ VersionsImpl versions = new VersionsImpl();
+ versions.setManagersProvider(managersProvider);
+ Mockito.when(request.getAction()).thenReturn(Commit);
+ Mockito.when(managersProvider.getPermissionsManager()).thenReturn(permManager);
+ Mockito.when(permManager.isAllowed(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
+ Mockito.when(managersProvider.getVersioningManager()).thenReturn(versioningManager);
+ Mockito.when(managersProvider.getActivityLogManager()).thenReturn(activityManager);
+ Mockito.when(managersProvider.getAsdcItemManager()).thenReturn(asdcManager);
+ Mockito.when(asdcManager.get(Mockito.any())).thenReturn(item);
+ Mockito.when(versioningManager.get(Mockito.any(), Mockito.any())).thenReturn(version);
+ Mockito.when(managersProvider.getNotificationPropagationManager()).thenReturn(notificationManager);
+ versions.actOn(request, ITEM_ID, VERSION_ID, USER);
+ Mockito.verify(versioningManager).publish(Mockito.any(), Mockito.any(), Mockito.any());
+ Mockito.verify(notificationManager).notifySubscribers(Mockito.any(), Mockito.any());
+ Mockito.verify(activityManager).logActivity(Mockito.any());
+ }
+
+ @Test
+ public void shouldActOnCommitWhenNotAllowed() {
+ VersionsImpl versions = new VersionsImpl();
+ versions.setManagersProvider(managersProvider);
+ Mockito.when(request.getAction()).thenReturn(Commit);
+ Mockito.when(managersProvider.getPermissionsManager()).thenReturn(permManager);
+ Mockito.when(permManager.isAllowed(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(false);
+
+ Response response = versions.actOn(request, ITEM_ID, VERSION_ID, USER);
+ assertEquals(response.getStatus(), Response.Status.FORBIDDEN.getStatusCode());
+ }
+
+ @Test
+ public void shouldActOnClean() {
+ VersionsImpl versions = new VersionsImpl();
+ versions.setManagersProvider(managersProvider);
+ Mockito.when(request.getAction()).thenReturn(Clean);
+ Mockito.when(managersProvider.getVersioningManager()).thenReturn(versioningManager);
+ versions.actOn(request, ITEM_ID, VERSION_ID, USER);
+ Mockito.verify(versioningManager).clean(Mockito.any(), Mockito.any());
+ }
+
+ @Test
+ public void shouldActOnRevert() {
+ VersionsImpl versions = new VersionsImpl();
+ versions.setManagersProvider(managersProvider);
+ Mockito.when(request.getAction()).thenReturn(Revert);
+ Mockito.when(request.getRevisionRequest()).thenReturn(revisionRequest);
+ Mockito.when(revisionRequest.getRevisionId()).thenReturn(REVISION_ID);
+ Mockito.when(managersProvider.getVersioningManager()).thenReturn(versioningManager);
+ versions.actOn(request, ITEM_ID, VERSION_ID, USER);
+ Mockito.verify(versioningManager).revert(Mockito.any(), Mockito.any(), Mockito.any());
+ }
+
+ @Test(expected = CoreException.class)
+ public void shouldActOnRevertAndEmptyRevisionId() {
+ VersionsImpl versions = new VersionsImpl();
+ versions.setManagersProvider(managersProvider);
+ Mockito.when(request.getAction()).thenReturn(Revert);
+ Mockito.when(request.getRevisionRequest()).thenReturn(revisionRequest);
+ Mockito.when(revisionRequest.getRevisionId()).thenReturn(null);
+ versions.actOn(request, ITEM_ID, VERSION_ID, USER);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void shouldActOnOther() {
+ VersionsImpl versions = new VersionsImpl();
+ versions.setManagersProvider(managersProvider);
+ Mockito.when(request.getAction()).thenReturn(Reset);
+ versions.actOn(request, ITEM_ID, VERSION_ID, USER);
+ }
+
+ @Test
+ public void shouldListRevision() {
+ VersionsImpl versions = new VersionsImpl();
+ versions.setManagersProvider(managersProvider);
+ Mockito.when(managersProvider.getVersioningManager()).thenReturn(versioningManager);
+ List<Revision> revisions = getRevisions();
+ Mockito.when(versioningManager.listRevisions(Mockito.any(), Mockito.any())).thenReturn(revisions);
+ Response response = versions.listRevisions(ITEM_ID, VERSION_ID, USER);
+ Mockito.verify(versioningManager).listRevisions(Mockito.any(), Mockito.any());
+ assertEquals(response.getStatus(), Status.OK.getStatusCode());
+ }
+
+ @Test
+ public void shouldGetActivityLog() {
+ VersionsImpl versions = new VersionsImpl();
+ versions.setManagersProvider(managersProvider);
+ Mockito.when(managersProvider.getActivityLogManager()).thenReturn(activityManager);
+ Mockito.when(activityManager.listLoggedActivities(Mockito.any(), Mockito.any())).thenReturn(Collections.emptyList());
+ Response activityLog = versions.getActivityLog(ITEM_ID, VERSION_ID, USER);
+ assertEquals(activityLog.getStatus(), Status.OK.getStatusCode());
+ }
+
+ @Test
+ public void shouldGet() {
+ VersionsImpl versions = new VersionsImpl();
+ versions.setManagersProvider(managersProvider);
+ Mockito.when(managersProvider.getVersioningManager()).thenReturn(versioningManager);
+ Mockito.when(versioningManager.get(Mockito.any(), Mockito.any())).thenReturn(version);
+ Mockito.when(version.getState()).thenReturn(state);
+ Mockito.when(state.getSynchronizationState()).thenReturn(SynchronizationState.Merging);
+ Response response = versions.get(ITEM_ID, VERSION_ID, USER);
+ assertEquals(response.getStatus(), Status.OK.getStatusCode());
+ }
+
+ private List<Revision> getRevisions() {
+ List<Revision> revisions = new ArrayList<>();
+ Revision revision = new Revision();
+ revision.setMessage("Initial TEST:TEST");
+ revisions.add(revision);
+ revisions.add(new Revision());
+ return revisions;
+ }
+} \ No newline at end of file