package org.onap.vid.testUtils;
import static com.fasterxml.jackson.module.kotlin.ExtensionsKt.jacksonObjectMapper;
-import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
-import static java.util.stream.Collectors.toMap;
import static org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptors;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.text.CharacterPredicates.DIGITS;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Iterator;
+import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.vid.asdc.AsdcCatalogException;
import org.onap.vid.asdc.beans.Service;
+import org.onap.vid.model.VidNotions;
+import org.onap.vid.model.VidNotions.InstantiationType;
+import org.onap.vid.model.VidNotions.InstantiationUI;
+import org.onap.vid.model.VidNotions.ModelCategory;
import org.onap.vid.mso.model.CloudConfiguration;
import org.springframework.core.env.Environment;
import org.testng.annotations.DataProvider;
}
public static String[] allPropertiesOf(Class<?> aClass) {
- return Arrays.stream(getPropertyDescriptors(aClass))
+ return getPropertyDescriptorsRecursively(aClass).stream()
.map(PropertyDescriptor::getDisplayName)
+ .distinct()
.toArray(String[]::new);
}
- private static <T> List<String> allStringPropertiesOf(T object) {
- return Arrays.stream(getPropertyDescriptors(object.getClass()))
- .filter(descriptor -> descriptor.getPropertyType().isAssignableFrom(String.class))
- .map(PropertyDescriptor::getDisplayName)
+ private static List<PropertyDescriptor> getPropertyDescriptorsRecursively(Class<?> aClass) {
+ List<PropertyDescriptor> result = new LinkedList<>();
+
+ for (Class<?> i = aClass; i != null && i != Object.class; i = i.getSuperclass()) {
+ Collections.addAll(result, getPropertyDescriptors(i));
+ }
+
+ return result;
+ }
+
+ private static <T> List<String> allStringFieldsOf(T object) {
+ return FieldUtils.getAllFieldsList(object.getClass()).stream()
+ .filter(field -> field.getType().isAssignableFrom(String.class))
+ .map(Field::getName)
+ .distinct()
.collect(toList());
}
* @param <T>
* @return The modified object
*/
- public static <T> T setStringsInStringProperties(T object) {
- try {
- final List<String> stringFields = allStringPropertiesOf(object);
-
- BeanUtils.populate(object, stringFields.stream()
- .collect(toMap(identity(), identity())));
+ public static <T> T setStringsInStringFields(T object) {
+ allStringFieldsOf(object).forEach(it -> {
+ try {
+ FieldUtils.writeField(object, it, it, true);
+ } catch (IllegalAccessException e) {
+ // YOLO
+ }
+ });
- return object;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
+ return object;
}
public static void registerCloudConfigurationValueGenerator() {
), CloudConfiguration.class);
}
+ public static void registerVidNotionsValueGenerator() {
+ BeanMatchers.registerValueGenerator(() -> new VidNotions(
+ randomEnum(InstantiationUI.class), randomEnum(ModelCategory.class),
+ randomEnum(InstantiationUI.class), randomEnum(InstantiationType.class)
+ ), VidNotions.class);
+ }
+
+ private static <T> T randomEnum(Class<T> enumClass) {
+ T[] values = enumClass.getEnumConstants();
+ return values[RandomUtils.nextInt(0, values.length)];
+ }
+
public static OngoingStubbing<InputStream> mockGetRawBodyWithStringBody(HttpResponse<String> httpResponse, String body) {
try {
return when(httpResponse.getRawBody()).thenReturn(IOUtils.toInputStream(body, StandardCharsets.UTF_8.name()));
return new Object[][]{{true}, {false}};
}
+ @DataProvider
+ public static Object[][] trueAndFalseAndNull() {
+ return new Boolean[][]{{Boolean.TRUE}, {Boolean.FALSE}, {null}};
+ }
+
}