@Override public void enrich(Object testCase) { List<Field> cubeFields = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), Cube.class); if (cubeFields.size() > 0) { for (Field cubeField : cubeFields) { try { logger.fine(String.format("Creating Container Object for field %s", cubeField.getName())); enrichField(testCase, cubeField); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } } }
@Override public void enrich(Object testCase) { List<Field> cubeFields = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), Cube.class); if (cubeFields.size() > 0) { for (Field cubeField : cubeFields) { try { logger.fine(String.format("Creating Container Object for field %s", cubeField.getName())); enrichField(testCase, cubeField); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } } }
private void startNetworks(Object testCase) { List<Field> networkFields = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), DockerNetwork.class); for (Field field : networkFields) { try { final Object object = field.get(testCase); if (Network.class.isAssignableFrom(object.getClass())) { Network networkObject = (Network) object; startNetwork(networkObject); } else { throw new IllegalArgumentException(String.format("Object %s is not assignable to %s.", object.getClass(), Network.class.getName())); } } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } }
private void startNetworks(Object testCase) { List<Field> networkFields = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), DockerNetwork.class); for (Field field : networkFields) { try { final Object object = field.get(testCase); if (Network.class.isAssignableFrom(object.getClass())) { Network networkObject = (Network) object; startNetwork(networkObject); } else { throw new IllegalArgumentException(String.format("Object %s is not assignable to %s.", object.getClass(), Network.class.getName())); } } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } }
private <T extends Annotation> void enrichAnnotatedPortBuildingFields(Class<T> annotationType, BiFunction<T, HasPortBindings, ?> fieldEnricher) throws IllegalAccessException { final List<Field> annotatedFields = ReflectionUtil.getFieldsWithAnnotation(containerObjectClass, annotationType); if (annotatedFields.isEmpty()) return; final HasPortBindings portBindings = dockerCube.getMetadata(HasPortBindings.class); if (portBindings == null) { throw new IllegalArgumentException(String.format( "Container Object %s contains fields annotated with %s but no ports are exposed by the container", containerObjectClass.getSimpleName(), annotationType.getSimpleName())); } for (Field annotatedField : annotatedFields) { final T annotation = annotatedField.getAnnotation(annotationType); try { annotatedField.set(containerObjectInstance, fieldEnricher.apply(annotation, portBindings)); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException( String.format("Container Object %s contains field %s annotated with %s, with error: %s", containerObjectClass.getSimpleName(), annotatedField.getName(), annotationType.getSimpleName(), ex.getLocalizedMessage()), ex); } } } }
private <T extends Annotation> void enrichAnnotatedPortBuildingFields(Class<T> annotationType, BiFunction<T, HasPortBindings, ?> fieldEnricher) throws IllegalAccessException { final List<Field> annotatedFields = ReflectionUtil.getFieldsWithAnnotation(containerObjectClass, annotationType); if (annotatedFields.isEmpty()) return; final HasPortBindings portBindings = dockerCube.getMetadata(HasPortBindings.class); if (portBindings == null) { throw new IllegalArgumentException(String.format( "Container Object %s contains fields annotated with %s but no ports are exposed by the container", containerObjectClass.getSimpleName(), annotationType.getSimpleName())); } for (Field annotatedField : annotatedFields) { final T annotation = annotatedField.getAnnotation(annotationType); try { annotatedField.set(containerObjectInstance, fieldEnricher.apply(annotation, portBindings)); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException( String.format("Container Object %s contains field %s annotated with %s, with error: %s", containerObjectClass.getSimpleName(), annotatedField.getName(), annotationType.getSimpleName(), ex.getLocalizedMessage()), ex); } } } }
@Override public void enrich(Object testCase) { if (hostUriContext.get() != null) { List<Field> fieldsWithAnnotation = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), HostIp.class); for (Field dockerHostIpField : fieldsWithAnnotation) { if (!dockerHostIpField.isAccessible()) { dockerHostIpField.setAccessible(true); } if (String.class.isAssignableFrom(dockerHostIpField.getType())) { try { dockerHostIpField.set(testCase, hostUriContext.get().getHost()); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } } } }
private void startContainers(Object testCase) { final List<Field> containerFields = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), DockerContainer.class); Collections.sort(containerFields, Comparator.comparingInt(f -> f.getAnnotation(DockerContainer.class).order())); Collections.reverse(containerFields); for (Field field : containerFields) { try { final Object object = field.get(testCase); if (Container.class.isAssignableFrom(object.getClass())) { Container containerObject = (Container) object; final Container enrichedContainer = injectorInstance.get().inject(containerObject); field.set(testCase, enrichedContainer); startContainer(enrichedContainer, testCase.getClass()); } else { throw new IllegalArgumentException(String.format("Object %s is not assignable to %s.", object.getClass(), Container.class.getName())); } } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } }
private void startContainers(Object testCase) { final List<Field> containerFields = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), DockerContainer.class); Collections.sort(containerFields, Comparator.comparingInt(f -> f.getAnnotation(DockerContainer.class).order())); Collections.reverse(containerFields); for (Field field : containerFields) { try { final Object object = field.get(testCase); if (Container.class.isAssignableFrom(object.getClass())) { Container containerObject = (Container) object; final Container enrichedContainer = injectorInstance.get().inject(containerObject); field.set(testCase, enrichedContainer); startContainer(enrichedContainer, testCase.getClass()); } else { throw new IllegalArgumentException(String.format("Object %s is not assignable to %s.", object.getClass(), Container.class.getName())); } } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } }
@Override public void enrich(Object testCase) { if (hostUriContext.get() != null) { List<Field> fieldsWithAnnotation = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), HostIp.class); for (Field dockerHostIpField : fieldsWithAnnotation) { if (!dockerHostIpField.isAccessible()) { dockerHostIpField.setAccessible(true); } if (String.class.isAssignableFrom(dockerHostIpField.getType())) { try { dockerHostIpField.set(testCase, hostUriContext.get().getHost()); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } } } }
@Override public void enrich(Object testCase) { for (Field field : ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), RouteURL.class)) { Object url; AwaitRoute await; try { if (!field.isAccessible()) { field.setAccessible(true); } RouteURL routeURL = getAnnotation(RouteURL.class, field.getAnnotations()); url = lookup(routeURL, field.getType()); field.set(testCase, url); await = getAnnotation(AwaitRoute.class, field.getAnnotations()); } catch (Exception e) { throw new RuntimeException("Could not set RouteURL value on field " + field, e); } configureAwaitRoute(url, await); } }
@Override public void enrich(Object testCase) { if (cubeRegistryInstance.get() != null) { List<Field> fieldsWithAnnotation = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), HostPort.class); for (Field dockerHostPortField : fieldsWithAnnotation) { if (!dockerHostPortField.isAccessible()) { dockerHostPortField.setAccessible(true); } if (int.class.isAssignableFrom(dockerHostPortField.getType()) || Integer.class.isAssignableFrom( dockerHostPortField.getType())) { try { final HostPort hostPortAnnotation = dockerHostPortField.getAnnotation(HostPort.class); String containerName = hostPortAnnotation.containerName(); int exposedPort = hostPortAnnotation.value(); int bindPort = getBindingPort(containerName, exposedPort); if (bindPort > 0) { dockerHostPortField.set(testCase, bindPort); } else { logger.log(Level.WARNING, String.format("There is no container with id %s.", containerName)); } } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } } } }
@Override public void enrich(Object testCase) { if (cubeRegistryInstance.get() != null) { List<Field> fieldsWithAnnotation = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), CubeIp.class); for (Field dockerContainerIpField : fieldsWithAnnotation) { if (!dockerContainerIpField.isAccessible()) { dockerContainerIpField.setAccessible(true); } if (String.class.isAssignableFrom(dockerContainerIpField.getType())) { try { final CubeIp cubeIpAnnotation = dockerContainerIpField.getAnnotation(CubeIp.class); String containerName = cubeIpAnnotation.containerName(); boolean internal = cubeIpAnnotation.internal(); String ip = getContainerIp(containerName, internal); if (ip != null) { dockerContainerIpField.set(testCase, ip); } else { logger.log(Level.WARNING, String.format("There is no container with id %s.", containerName)); } } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } } } }
@Override public void enrich(Object testCase) { if (cubeRegistryInstance.get() != null) { List<Field> fieldsWithAnnotation = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), HostPort.class); for (Field dockerHostPortField : fieldsWithAnnotation) { if (!dockerHostPortField.isAccessible()) { dockerHostPortField.setAccessible(true); } if (int.class.isAssignableFrom(dockerHostPortField.getType()) || Integer.class.isAssignableFrom( dockerHostPortField.getType())) { try { final HostPort hostPortAnnotation = dockerHostPortField.getAnnotation(HostPort.class); String containerName = hostPortAnnotation.containerName(); int exposedPort = hostPortAnnotation.value(); int bindPort = getBindingPort(containerName, exposedPort); if (bindPort > 0) { dockerHostPortField.set(testCase, bindPort); } else { logger.log(Level.WARNING, String.format("There is no container with id %s.", containerName)); } } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } } } }
@Override public void enrich(Object testCase) { if (cubeRegistryInstance.get() != null) { List<Field> fieldsWithAnnotation = ReflectionUtil.getFieldsWithAnnotation(testCase.getClass(), CubeIp.class); for (Field dockerContainerIpField : fieldsWithAnnotation) { if (!dockerContainerIpField.isAccessible()) { dockerContainerIpField.setAccessible(true); } if (String.class.isAssignableFrom(dockerContainerIpField.getType())) { try { final CubeIp cubeIpAnnotation = dockerContainerIpField.getAnnotation(CubeIp.class); String containerName = cubeIpAnnotation.containerName(); boolean internal = cubeIpAnnotation.internal(); String ip = getContainerIp(containerName, internal); if (ip != null) { dockerContainerIpField.set(testCase, ip); } else { logger.log(Level.WARNING, String.format("There is no container with id %s.", containerName)); } } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } } } }
private static Void objectContainerEnricherMockEnrich(InvocationOnMock invocation) throws Throwable { // simulate ContainerObjectTestEnricher by setting every field annotated with @Cube with a new instance Object containerObject = invocation.getArguments()[0]; ReflectionUtil.getFieldsWithAnnotation(containerObject.getClass(), Cube.class) .stream().forEach(field -> { try { field.set(containerObject, field.getType().newInstance()); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } }); return null; }
List<Link> links = ReflectionUtil.getFieldsWithAnnotation(containerObjectClass, Cube.class) .stream() .map(DockerContainerObjectBuilder::linkFromCubeAnnotatedField)
List<Link> links = ReflectionUtil.getFieldsWithAnnotation(containerObjectClass, Cube.class) .stream() .map(DockerContainerObjectBuilder::linkFromCubeAnnotatedField)