2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.sdc.workflow.persistence.impl;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.doAnswer;
21 import static org.onap.sdc.common.zusammen.services.ZusammenElementUtil.ELEMENT_TYPE_PROPERTY;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
25 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
26 import com.amdocs.zusammen.datatypes.Id;
27 import com.amdocs.zusammen.datatypes.item.Action;
28 import com.amdocs.zusammen.datatypes.item.ElementContext;
29 import com.amdocs.zusammen.datatypes.item.Info;
30 import com.amdocs.zusammen.datatypes.item.ItemVersion;
31 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.Optional;
37 import java.util.UUID;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.sdc.common.versioning.persistence.zusammen.ZusammenSessionContextCreator;
46 import org.onap.sdc.common.zusammen.services.ZusammenAdaptor;
47 import org.onap.sdc.workflow.persistence.impl.ActivitySpecRepositoryImpl.InfoPropertyName;
48 import org.onap.sdc.workflow.persistence.impl.types.ActivitySpecData;
49 import org.onap.sdc.workflow.persistence.impl.types.ActivitySpecElementType;
50 import org.onap.sdc.workflow.persistence.types.ActivitySpecEntity;
51 import org.onap.sdc.workflow.persistence.types.ActivitySpecParameter;
52 import org.onap.sdc.workflow.services.utilities.JsonUtil;
54 @RunWith(MockitoJUnitRunner.class)
55 public class ActivitySpecRepositoryImplTest {
57 private static final String versionId = "1234";
58 private static final String itemId = "5678";
61 private ZusammenAdaptor zusammenAdaptor;
63 private ZusammenSessionContextCreator contextCreator;
65 private ActivitySpecRepositoryImpl daoImpl;
67 private ActivitySpecEntity entity;
68 private final Map<String, Element> elementMap = new HashMap<>();
69 private String elementId;
73 daoImpl = new ActivitySpecRepositoryImpl(zusammenAdaptor, contextCreator);
74 entity = new ActivitySpecEntity();
75 entity = new ActivitySpecEntity();
78 entity.setVersionId(versionId);
79 entity.setName("activitySpec");
80 List<String> categoryList = new ArrayList<>();
81 categoryList.add("category1");
82 entity.setCategoryList(categoryList);
83 ActivitySpecParameter inputParams = new ActivitySpecParameter("dbhost", "String", null);
84 inputParams.setValue("localhost");
85 List<ActivitySpecParameter> inputs = new ArrayList<>();
86 inputs.add(inputParams);
87 entity.setInputs(inputs);
90 mockZusammenAdapter();
94 public void testCreate() {
95 ItemVersion itemVersionmock = new ItemVersion();
96 itemVersionmock.setId(new Id());
98 daoImpl.create(entity);
99 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersionId());
100 Optional<ElementInfo> testElementInfo = zusammenAdaptor
101 .getElementInfoByName(contextCreator.create(), elementContext,
102 Id.ZERO, ActivitySpecElementType.ACTIVITYSPEC.name());
103 Assert.assertTrue(testElementInfo.isPresent());
104 Assert.assertEquals(testElementInfo.get().getInfo().getName(), ActivitySpecElementType.ACTIVITYSPEC.name());
105 Assert.assertEquals(testElementInfo.get().getInfo()
106 .getProperty(ActivitySpecRepositoryImpl.InfoPropertyName.DESCRIPTION.getValue()),
107 entity.getDescription());
108 Assert.assertEquals(testElementInfo.get().getInfo().getProperty(InfoPropertyName.CATEGORY.getValue()),
109 entity.getCategoryList());
110 Assert.assertEquals(testElementInfo.get().getInfo()
111 .getProperty(ActivitySpecRepositoryImpl.InfoPropertyName.NAME.getValue()),
114 final Optional<Element> testElement =
115 zusammenAdaptor.getElement(contextCreator.create(), elementContext, new Id(elementId));
116 final InputStream data = testElement.get().getData();
117 final ActivitySpecData activitySpecData = JsonUtil.json2Object(data, ActivitySpecData.class);
118 Assert.assertEquals(activitySpecData.getInputs().get(0).getName(), entity.getInputs().get(0).getName());
122 public void testGet() {
123 final ActivitySpecEntity retrieved = daoImpl.get(entity);
124 Assert.assertEquals(retrieved.getName(), entity.getName());
125 Assert.assertEquals(retrieved.getDescription(), entity.getDescription());
126 Assert.assertEquals(retrieved.getCategoryList(), entity.getCategoryList());
130 public void testUpdate() {
131 entity.setDescription("Update AS version1");
132 daoImpl.update(entity);
133 final ActivitySpecEntity retrieved = daoImpl.get(entity);
134 Assert.assertEquals(retrieved.getName(), entity.getName());
135 Assert.assertEquals(retrieved.getDescription(), entity.getDescription());
136 Assert.assertEquals(retrieved.getCategoryList(), entity.getCategoryList());
140 private void mockZusammenAdapter() {
142 doAnswer(invocationOnMock -> {
143 Id elementId = invocationOnMock.getArgument(2);
144 return Optional.of(elementMap.get(elementId.getValue()));
145 }).when(zusammenAdaptor).getElement(any(), any(), any());
147 doAnswer(invocationOnMock -> {
148 ZusammenElement element = new ZusammenElement();
149 Info info = new Info();
150 element.setElementId(Id.ZERO);
151 info.addProperty("name", entity.getName());
152 info.addProperty("description", entity.getDescription());
153 info.addProperty("category", entity.getCategoryList());
154 element.setInfo(info);
155 return Optional.of(element);
156 }).when(zusammenAdaptor).getElementByName(any(), any(), any(), any());
158 doAnswer(invocationOnMock -> {
159 String elementName = invocationOnMock.getArgument(3);
160 return elementMap.values().stream()
161 .filter(element -> elementName.equals(element.getInfo().getProperty(ELEMENT_TYPE_PROPERTY)))
163 ElementInfo elementInfo = new ElementInfo();
164 elementInfo.setId(element.getElementId());
165 elementInfo.setInfo(element.getInfo());
168 }).when(zusammenAdaptor).getElementInfoByName(any(), any(), any(), any());
170 doAnswer(invocationOnMock -> {
171 ZusammenElement element = invocationOnMock.getArgument(2);
172 if (element.getAction().equals(Action.CREATE) || element.getAction().equals(Action.UPDATE)) {
173 element.setElementId(new Id(UUID.randomUUID().toString()));
175 elementMap.put(element.getElementId().getValue(), element);
176 elementId = element.getElementId().getValue();
178 }).when(zusammenAdaptor).saveElement(any(), any(), any(), any());