1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
package org.openecomp.sdc.be.servlets;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import fj.data.Either;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.openecomp.sdc.be.DummyConfigurationManager;
import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
import org.openecomp.sdc.be.components.utils.GroupTypeBuilder;
import org.openecomp.sdc.be.components.validation.UserValidations;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.GroupTypeDefinition;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.operations.StorageException;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class GroupTypesEndpointTest extends JerseySpringBaseTest {
private static final String USER_ID = "a001";
private static final String INVALID_USER_ID = "a002";
private static final GroupTypeDefinition EMPTY_GROUP_TYPE = new GroupTypeDefinition();
private static final String COMPONENT_TYPE = "VF";
private static final HashSet<String> EXCLUDED_TYPES = Sets.newHashSet("Root", "Heat");
private static ComponentsUtils componentsUtils;
private static GroupTypeOperation groupTypeOperation;
private static TitanDao titanDao;
private static UserValidations userValidations;
private static User user;
@Configuration
@Import(BaseTestConfig.class)
static class GroupTypesTestConfig {
@Bean
GroupTypesEndpoint groupTypesEndpoint() {
return new GroupTypesEndpoint(groupTypeBusinessLogic());
}
@Bean
GroupTypeBusinessLogic groupTypeBusinessLogic() {
return new GroupTypeBusinessLogic(groupTypeOperation, titanDao, userValidations);
}
}
@BeforeClass
public static void initClass() {
componentsUtils = mock(ComponentsUtils.class);
groupTypeOperation = mock(GroupTypeOperation.class);
titanDao = mock(TitanDao.class);
userValidations = mock(UserValidations.class);
user = mock(User.class);
}
@Before
public void init() {
new DummyConfigurationManager();
when(ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedGroupTypesMapping()).thenReturn(buildExcludeGroupTypesMap());
ResponseFormatManager responseFormatManager = ResponseFormatManager.getInstance();
when(userValidations.validateUserExists(eq(USER_ID), anyString(), anyBoolean())).thenReturn(Either.left(user));
// TODO: handle for invalid user test
// when(userValidations.validateUserExists(eq(INVALID_USER_ID), anyString(), anyBoolean())).thenReturn(Either.right(???)));
}
@Override
protected void configureClient(ClientConfig config) {
final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
config.register(jacksonJsonProvider);
}
@Override
protected ResourceConfig configure() {
return super.configure(GroupTypesTestConfig.class)
.register(GroupTypesEndpoint.class);
}
@Test
public void getGroupTypes_validUser_Success() {
List<GroupTypeDefinition> testConfigGroupTypes = buildGroupTypesList();
when(groupTypeOperation.getAllGroupTypes(EXCLUDED_TYPES)).thenReturn(buildGroupTypesList());
List<GroupTypeDefinition> fetchedGroupTypes = buildGetGroupTypesCall(USER_ID).get(new GenericType<List<GroupTypeDefinition>>(){});
verifyGroupTypesList(testConfigGroupTypes, fetchedGroupTypes);
}
@Test
public void getGroupTypes_whenNoInteranlComponentType_passEmptyAsExcludedTypes() {
List<GroupTypeDefinition> testConfigGroupTypes = buildGroupTypesList();
when(groupTypeOperation.getAllGroupTypes(null)).thenReturn(buildGroupTypesList());
List<GroupTypeDefinition> fetchedGroupTypes = buildGetGroupTypesCallNoInternalComponent(USER_ID).get(new GenericType<List<GroupTypeDefinition>>(){});
verifyGroupTypesList(testConfigGroupTypes, fetchedGroupTypes);
}
@Ignore
public void getGroupTypes_dbError() {
when(groupTypeOperation.getAllGroupTypes(EXCLUDED_TYPES)).thenThrow(new StorageException(StorageOperationStatus.NOT_FOUND));
when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND)).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
when(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND)).thenReturn(new ResponseFormat(HttpStatus.NOT_FOUND.value()));
Response response = buildGetGroupTypesCall(USER_ID).get();
assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
}
// TODO: simulate proper error with configuration error mapping and eactivate the test
@Ignore
public void getGroupTypes_invalidUser_Failure() {
when(groupTypeOperation.getAllGroupTypes(EXCLUDED_TYPES)).thenReturn(buildGroupTypesList());
Response response = buildGetGroupTypesCall(INVALID_USER_ID).get();
assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
}
private void verifyGroupTypesList(List<GroupTypeDefinition> groupTypes, List<GroupTypeDefinition> fetchedGroupTypes) {
assertThat(fetchedGroupTypes)
.usingElementComparatorOnFields("version", "type", "uniqueId")
.isEqualTo(groupTypes);
verifyOnlySpecificFieldsInResponse(fetchedGroupTypes, "version", "type", "uniqueId");
}
private void verifyOnlySpecificFieldsInResponse(List<GroupTypeDefinition> fetchedGroupTypes, String ... fields) {
assertThat(fetchedGroupTypes)
.usingElementComparatorIgnoringFields(fields)
.containsOnly(EMPTY_GROUP_TYPE);
}
private Invocation.Builder buildGetGroupTypesCall(String userId) {
return target("/v1/catalog/groupTypes")
.queryParam("internalComponentType", COMPONENT_TYPE)
.request(MediaType.APPLICATION_JSON)
.header(Constants.USER_ID_HEADER, userId);
}
private Invocation.Builder buildGetGroupTypesCallNoInternalComponent(String userId) {
return target("/v1/catalog/groupTypes")
.request(MediaType.APPLICATION_JSON)
.header(Constants.USER_ID_HEADER, userId);
}
private Map<String, Set<String>> buildExcludeGroupTypesMap() {
return new ImmutableMap.Builder<String, Set<String>>()
.put("CR", Sets.newHashSet("VFModule", "Root", "Heat"))
.put(COMPONENT_TYPE, EXCLUDED_TYPES)
.build();
}
private List<GroupTypeDefinition> buildGroupTypesList() {
GroupTypeDefinition type1 = GroupTypeBuilder.create()
.setDerivedFrom("root")
.setType("VFModule")
.setUniqueId("id1")
.setVersion("1.0")
.build();
GroupTypeDefinition type2 = GroupTypeBuilder.create().setDerivedFrom("root").setType("Heat").setUniqueId("id2").build();
return asList(type1, type2);
}
private GroupTypeDefinition[] listOfEmptyGroupTypes(int size) {
return Stream.generate(GroupTypeDefinition::new).limit(size).toArray(GroupTypeDefinition[]::new);
}
}
|