Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -21,13 +24,17 @@ public class UserBeansGraphService {
public static final String UNKNOWN_DEPENDENCY = "UNKNOWN";
public static final String UNKNOWN_PACKAGE = "UNKNOWN";
private final UserBeansService userBeansService;

private final ApplicationContext applicationContext;

private final ClasspathDependencyReader classpathDependencyReader;

private final WebDocumentReader webDocumentReader;

// @formatter:off
public UserBeansGraphService(UserBeansService userBeansService) {
public UserBeansGraphService(UserBeansService userBeansService, ApplicationContext applicationContext) {
this.userBeansService = userBeansService;
this.applicationContext = applicationContext;
this.classpathDependencyReader = new ClasspathDependencyReader();
this.webDocumentReader = new WebDocumentReader();
}
Expand Down Expand Up @@ -113,7 +120,18 @@ List<Edge> getEdges() {
if (!dd.beanDependencies().isEmpty()) {
// @formatter:off
return dd.beanDependencies().stream()
.map(dep -> new Edge(sourceNode, new BeanEdge(dep, UNKNOWN_PACKAGE, UNKNOWN_DEPENDENCY)));
.map(dep -> {
BeanEdge targetNode;
try {
Class<?> myClass = applicationContext.getBean(dep).getClass();
String beanName = (myClass.getSimpleName().length() == 0) ? myClass.getName() : myClass.getSimpleName();
String beanPackage = myClass.getPackageName();
targetNode = new BeanEdge(beanName, beanPackage, UNKNOWN_DEPENDENCY);
} catch (NoSuchBeanDefinitionException e) {
targetNode = new BeanEdge(dep, UNKNOWN_PACKAGE, UNKNOWN_DEPENDENCY);
}
return new Edge(sourceNode, targetNode);
});
// @formatter:on
} else {
//TODO False edge; an evidence to redesign the graph to be consumed for D3.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

import static org.assertj.core.api.Assertions.assertThat;

import io.github.jabrena.support.SupportController;
import io.github.jabrena.support.TestApplication;
import java.util.Comparator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

@SpringBootTest(
classes = { TestApplication.class, SupportController.class },
properties = { "management.endpoints.web.exposure.include=beans,userbeans" }
)
@SpringBootTest(classes = { TestApplication.class }, properties = { "management.endpoints.web.exposure.include=beans,userbeans" })
class UserBeansServiceTests {

@Autowired
private UserBeansService userBeansService;

@Autowired
private ApplicationContext applicationContext;

// @formatter:off
@Test
void shouldReturnsAllBeansInformation() {
Expand Down Expand Up @@ -58,4 +59,37 @@ void shouldNoReturnAnyUnnamedBean() {
//Then
assertThat(beanList).hasSize(expectedUnnamedBeanCounter);
}

@Test
void shouldRetrieveAllBeans() {
//Given
var expectedUnnamedBeanCounter = 0;

record BeanDocument(String beanName, String beanPackage) {}

//When
var beanList = userBeansService
.getBeansDocuments()
.stream()
.filter(bd -> bd.dependencies().size() > 0)
.flatMap(bd -> bd.dependencies().stream())
.distinct()
.map(str -> {
try {
Class<?> myClass = applicationContext.getBean(str).getClass();
String beanName = (myClass.getSimpleName().length() == 0) ? myClass.getName() : myClass.getSimpleName();
String beanPackage = myClass.getPackageName();
return new BeanDocument(beanName, beanPackage);
} catch (NoSuchBeanDefinitionException e) {
//Empty on purpose
}
return new BeanDocument(str, "UNKNOWN");
})
.sorted(Comparator.comparing(BeanDocument::beanName))
.peek(System.out::println)
.toList();

//Then
assertThat(beanList).hasSizeGreaterThan(expectedUnnamedBeanCounter);
}
}