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 static org.apache.commons.text.CharacterPredicates.LETTERS;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.RETURNS_DEFAULTS;
import static org.mockito.Mockito.mock;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.code.beanmatchers.BeanMatchers;
import com.google.common.collect.ImmutableList;
import java.beans.PropertyDescriptor;
import java.io.ByteArrayInputStream;
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.reflect.MethodUtils;
+import org.apache.commons.text.RandomStringGenerator;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
-import org.onap.portalsdk.core.domain.support.DomainVo;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.vid.asdc.beans.Service;
-import org.springframework.mock.env.MockEnvironment;
+import org.onap.vid.mso.model.CloudConfiguration;
+import org.springframework.core.env.Environment;
+import org.testng.annotations.DataProvider;
/**
* Created by Oren on 6/7/17.
return readJsonResourceFileAsObject(pathInResource, valueType, false);
}
- public static <T> T readJsonResourceFileAsObject(String pathInResource, Class<T> valueType, boolean ignoreUnknownProperties)
- throws IOException {
- ObjectMapper objectMapper = new ObjectMapper();
- objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, ignoreUnknownProperties);
+ public static <T> T readJsonResourceFileAsObject(String pathInResource, Class<T> valueType,
+ boolean failOnUnknownProperties)
+ throws IOException {
+ ObjectMapper objectMapper = jacksonObjectMapper()
+ .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
return objectMapper.readValue(
- TestUtils.class.getResource(pathInResource),
- valueType);
+ TestUtils.class.getResource(pathInResource),
+ valueType);
}
- public static String[] allPropertiesOf(Class<DomainVo> aClass) {
+ public static String readFileAsString(String pathInResource) {
+ try {
+ return IOUtils.toString(TestUtils.class.getResource(pathInResource), "UTF-8");
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static String[] allPropertiesOf(Class<?> aClass) {
return Arrays.stream(getPropertyDescriptors(aClass))
.map(PropertyDescriptor::getDisplayName)
.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)
+ .collect(toList());
+ }
+
+ /**
+ * Sets each String property with a value equal to the name of
+ * the property; e.g.: { name: "name", city: "city" }
+ * @param object
+ * @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())));
+
+ return object;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static void registerCloudConfigurationValueGenerator() {
+ BeanMatchers.registerValueGenerator(() -> new CloudConfiguration(
+ randomAlphabetic(7), randomAlphabetic(7), randomAlphabetic(7)
+ ), CloudConfiguration.class);
+ }
+
public static class JavaxRsClientMocks {
private final javax.ws.rs.client.Client fakeClient;
}
- //Please use resetSystemProperties after using this method, so other test won't be affected
- public static void mockSystemPropertyWithKeyValue(String key, String value) {
- MockEnvironment mockEnvironment = new MockEnvironment();
- mockEnvironment.setProperty(key, value);
+ public interface Test {
- SystemProperties systemProperties = new SystemProperties();
- systemProperties.setEnvironment(mockEnvironment);
+ void apply();
}
- public static void resetSystemProperties() {
+ public static void testWithSystemProperty(String key, String value, Test test) throws Exception {
SystemProperties systemProperties = new SystemProperties();
- systemProperties.setEnvironment(null);
+ //use reflection to invoke protected method
+ Environment originalEnvironment = (Environment) MethodUtils
+ .invokeMethod(systemProperties, true, "getEnvironment");
+
+ try {
+ Environment environment = mock(Environment.class);
+ systemProperties.setEnvironment(environment);
+ when(environment.getRequiredProperty(key)).thenReturn(value);
+ when(environment.containsProperty(key)).thenReturn(true);
+ test.apply();
+ }
+ finally {
+ systemProperties.setEnvironment(originalEnvironment);
+ }
+ }
+
+ private static RandomStringGenerator generator = new RandomStringGenerator.Builder()
+ .withinRange('0', 'z')
+ .filteredBy(LETTERS, DIGITS)
+ .build();
+
+ public static String generateRandomAlphaNumeric(int length) {
+ return generator.generate(length);
+ }
+
+ @DataProvider
+ public static Object[][] trueAndFalse() {
+ return new Object[][]{{true}, {false}};
}
}