|
| 1 | +package com.structurizr.component; |
| 2 | + |
| 3 | +import com.structurizr.Workspace; |
| 4 | +import com.structurizr.component.description.FirstSentenceDescriptionStrategy; |
| 5 | +import com.structurizr.component.filter.ExcludeTypesByRegexFilter; |
| 6 | +import com.structurizr.component.matcher.AnnotationTypeMatcher; |
| 7 | +import com.structurizr.component.matcher.ImplementsTypeMatcher; |
| 8 | +import com.structurizr.component.url.PrefixSourceUrlStrategy; |
| 9 | +import com.structurizr.model.Component; |
| 10 | +import com.structurizr.model.Container; |
| 11 | +import com.structurizr.model.Person; |
| 12 | +import com.structurizr.model.SoftwareSystem; |
| 13 | +import com.structurizr.util.StringUtils; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.*; |
| 19 | + |
| 20 | +public class SpringPetClinicTests { |
| 21 | + |
| 22 | + @Test |
| 23 | + void springPetClinic() { |
| 24 | + String springPetClinicHome = System.getenv().getOrDefault("SPRING_PETCLINIC_HOME", ""); |
| 25 | + System.out.println(springPetClinicHome); |
| 26 | + if (!StringUtils.isNullOrEmpty(springPetClinicHome)) { |
| 27 | + System.out.println("Running Spring PetClinic example..."); |
| 28 | + |
| 29 | + Workspace workspace = new Workspace("Spring PetClinic", "Description"); |
| 30 | + Person clinicEmployee = workspace.getModel().addPerson("Clinic Employee"); |
| 31 | + SoftwareSystem softwareSystem = workspace.getModel().addSoftwareSystem("Spring PetClinic"); |
| 32 | + Container webApplication = softwareSystem.addContainer("Web Application"); |
| 33 | + Container relationalDatabaseSchema = softwareSystem.addContainer("Relational Database Schema"); |
| 34 | + |
| 35 | + ComponentFinder componentFinder = new ComponentFinderBuilder() |
| 36 | + .forContainer(webApplication) |
| 37 | + .fromClasses(new File(springPetClinicHome, "target/spring-petclinic-3.3.0-SNAPSHOT.jar")) |
| 38 | + .fromSource(new File(springPetClinicHome, "src/main/java")) |
| 39 | + .withStrategy( |
| 40 | + new ComponentFinderStrategyBuilder() |
| 41 | + .matchedBy(new AnnotationTypeMatcher("org.springframework.stereotype.Controller")) |
| 42 | + .filteredBy(new ExcludeTypesByRegexFilter(".*.CrashController")) |
| 43 | + .withTechnology("Spring MVC Controller") |
| 44 | + .withUrl(new PrefixSourceUrlStrategy("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java")) |
| 45 | + .forEach((component -> { |
| 46 | + clinicEmployee.uses(component, "Uses"); |
| 47 | + component.addTags(component.getTechnology()); |
| 48 | + })) |
| 49 | + .build() |
| 50 | + ) |
| 51 | + .withStrategy( |
| 52 | + new ComponentFinderStrategyBuilder() |
| 53 | + .matchedBy(new ImplementsTypeMatcher("org.springframework.data.repository.Repository")) |
| 54 | + .withDescription(new FirstSentenceDescriptionStrategy()) |
| 55 | + .withTechnology("Spring Data Repository") |
| 56 | + .withUrl(new PrefixSourceUrlStrategy("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java")) |
| 57 | + .forEach((component -> { |
| 58 | + component.uses(relationalDatabaseSchema, "Reads from and writes to"); |
| 59 | + component.addTags(component.getTechnology()); |
| 60 | + })) |
| 61 | + .build() |
| 62 | + ) |
| 63 | + .build(); |
| 64 | + |
| 65 | + componentFinder.findComponents(); |
| 66 | + assertEquals(7, webApplication.getComponents().size()); |
| 67 | + |
| 68 | + Component welcomeController = webApplication.getComponentWithName("Welcome Controller"); |
| 69 | + assertNotNull(welcomeController); |
| 70 | + assertEquals("org.springframework.samples.petclinic.system.WelcomeController", welcomeController.getProperties().get("component.type")); |
| 71 | + assertEquals("org/springframework/samples/petclinic/system/WelcomeController.java", welcomeController.getProperties().get("component.src")); |
| 72 | + assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java", welcomeController.getUrl()); |
| 73 | + assertTrue(clinicEmployee.hasEfferentRelationshipWith(welcomeController)); |
| 74 | + |
| 75 | + Component ownerController = webApplication.getComponentWithName("Owner Controller"); |
| 76 | + assertNotNull(ownerController); |
| 77 | + assertEquals("org.springframework.samples.petclinic.owner.OwnerController", ownerController.getProperties().get("component.type")); |
| 78 | + assertEquals("org/springframework/samples/petclinic/owner/OwnerController.java", ownerController.getProperties().get("component.src")); |
| 79 | + assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java", ownerController.getUrl()); |
| 80 | + assertTrue(clinicEmployee.hasEfferentRelationshipWith(ownerController)); |
| 81 | + |
| 82 | + Component petController = webApplication.getComponentWithName("Pet Controller"); |
| 83 | + assertNotNull(petController); |
| 84 | + assertEquals("org.springframework.samples.petclinic.owner.PetController", petController.getProperties().get("component.type")); |
| 85 | + assertEquals("org/springframework/samples/petclinic/owner/PetController.java", petController.getProperties().get("component.src")); |
| 86 | + assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/owner/PetController.java", petController.getUrl()); |
| 87 | + assertTrue(clinicEmployee.hasEfferentRelationshipWith(petController)); |
| 88 | + |
| 89 | + Component vetController = webApplication.getComponentWithName("Vet Controller"); |
| 90 | + assertNotNull(vetController); |
| 91 | + assertEquals("org.springframework.samples.petclinic.vet.VetController", vetController.getProperties().get("component.type")); |
| 92 | + assertEquals("org/springframework/samples/petclinic/vet/VetController.java", vetController.getProperties().get("component.src")); |
| 93 | + assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/vet/VetController.java", vetController.getUrl()); |
| 94 | + assertTrue(clinicEmployee.hasEfferentRelationshipWith(vetController)); |
| 95 | + |
| 96 | + Component visitController = webApplication.getComponentWithName("Visit Controller"); |
| 97 | + assertNotNull(visitController); |
| 98 | + assertEquals("org.springframework.samples.petclinic.owner.VisitController", visitController.getProperties().get("component.type")); |
| 99 | + assertEquals("org/springframework/samples/petclinic/owner/VisitController.java", visitController.getProperties().get("component.src")); |
| 100 | + assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java", visitController.getUrl()); |
| 101 | + assertTrue(clinicEmployee.hasEfferentRelationshipWith(visitController)); |
| 102 | + |
| 103 | + Component ownerRepository = webApplication.getComponentWithName("Owner Repository"); |
| 104 | + assertNotNull(ownerRepository); |
| 105 | + assertEquals("Repository class for Owner domain objects All method names are compliant with Spring Data naming conventions so this interface can easily be extended for Spring Data.", ownerRepository.getDescription()); |
| 106 | + assertEquals("org.springframework.samples.petclinic.owner.OwnerRepository", ownerRepository.getProperties().get("component.type")); |
| 107 | + assertEquals("org/springframework/samples/petclinic/owner/OwnerRepository.java", ownerRepository.getProperties().get("component.src")); |
| 108 | + assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java", ownerRepository.getUrl()); |
| 109 | + assertTrue(ownerRepository.hasEfferentRelationshipWith(relationalDatabaseSchema, "Reads from and writes to")); |
| 110 | + |
| 111 | + Component vetRepository = webApplication.getComponentWithName("Vet Repository"); |
| 112 | + assertNotNull(vetRepository); |
| 113 | + assertEquals("Repository class for Vet domain objects All method names are compliant with Spring Data naming conventions so this interface can easily be extended for Spring Data.", vetRepository.getDescription()); |
| 114 | + assertEquals("org.springframework.samples.petclinic.vet.VetRepository", vetRepository.getProperties().get("component.type")); |
| 115 | + assertEquals("org/springframework/samples/petclinic/vet/VetRepository.java", vetRepository.getProperties().get("component.src")); |
| 116 | + assertEquals("https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java", vetRepository.getUrl()); |
| 117 | + assertTrue(vetRepository.hasEfferentRelationshipWith(relationalDatabaseSchema, "Reads from and writes to")); |
| 118 | + |
| 119 | + assertTrue(welcomeController.getRelationships().isEmpty()); |
| 120 | + assertNotNull(petController.getEfferentRelationshipWith(ownerRepository)); |
| 121 | + assertNotNull(visitController.getEfferentRelationshipWith(ownerRepository)); |
| 122 | + assertNotNull(ownerController.getEfferentRelationshipWith(ownerRepository)); |
| 123 | + assertNotNull(vetController.getEfferentRelationshipWith(vetRepository)); |
| 124 | + } else { |
| 125 | + System.out.println("Skipping Spring PetClinic example..."); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments