1 package it.perproduct;
2
3 import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService;
4 import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
5 import com.atlassian.plugin.spring.scanner.test.servlet.ComponentStatusServlet;
6 import com.atlassian.plugin.spring.scanner.test.servlet.ManageDynamicContextServlet;
7 import com.google.common.collect.ImmutableList;
8 import it.allproducts.ComponentExpectations;
9 import it.allproducts.ComponentExpectations.AbstractExpectedComponent;
10 import org.hamcrest.Matcher;
11 import org.hamcrest.Matchers;
12 import org.junit.Test;
13 import org.springframework.stereotype.Component;
14
15 import java.io.IOException;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.stream.Collectors;
19
20 import static org.junit.Assert.assertThat;
21
22
23
24
25
26
27
28
29
30
31
32
33 public abstract class AbstractComponentsInProductTest extends AbstractInProductTest {
34
35
36
37
38 private static final String COMPONENT_STATUS_URL = BASEURL + "/plugins/servlet/component-status?components";
39
40
41
42
43 private static final String SERVICE_STATUS_URL = BASEURL + "/plugins/servlet/component-status?services";
44
45
46
47
48 private static final String START_DYNAMIC_CONTEXT_URL = BASEURL + "/plugins/servlet/manage-dynamic-contexts?startup";
49 private static final String STOP_DYNAMIC_CONTEXT_URL = BASEURL + "/plugins/servlet/manage-dynamic-contexts?shutdown";
50
51
52
53
54 @Test
55 public void testComponents() throws Exception {
56 List<String> actualComponentsInProduct = readStringList(COMPONENT_STATUS_URL);
57 List<AbstractExpectedComponent> expected = getExpectedComponentsWithDefaultProfile();
58
59 assertThat(actualComponentsInProduct, hasComponents(expected));
60 }
61
62
63
64
65 @Test
66 public void testServices() throws Exception {
67 List<String> actualServicesInProduct = readStringList(SERVICE_STATUS_URL);
68 List<AbstractExpectedComponent> expected = getExpectedServiceExportsWithDefaultProfile();
69
70 assertThat(actualServicesInProduct, hasComponents(expected));
71 }
72
73
74
75
76 @Test
77 public void testDynamicContext() throws Exception {
78
79
80 List<AbstractExpectedComponent> expectedComponentsDefaultProfile = getExpectedComponentsWithDefaultProfile();
81 List<AbstractExpectedComponent> expectedServicesDefaultProfile = getExpectedServiceExportsWithDefaultProfile();
82
83 assertComponentsAndServices(expectedComponentsDefaultProfile, expectedServicesDefaultProfile);
84
85
86 getUrl(START_DYNAMIC_CONTEXT_URL);
87
88
89
90 List<AbstractExpectedComponent> expectedDynamicComponents =
91 ImmutableList.<AbstractExpectedComponent>builder()
92 .addAll(expectedComponentsDefaultProfile)
93 .addAll(ComponentExpectations.COMMON.getExpectedScannerCreatedDynamicComponents())
94 .addAll(ComponentExpectations.COMMON.getExpectedScannerCreatedDynamicComponentImports())
95 .build();
96
97 assertComponentsAndServices(expectedDynamicComponents, expectedServicesDefaultProfile);
98
99
100 getUrl(STOP_DYNAMIC_CONTEXT_URL);
101
102 assertComponentsAndServices(expectedComponentsDefaultProfile, expectedServicesDefaultProfile);
103 }
104
105 private void assertComponentsAndServices(List<AbstractExpectedComponent> expectedComponents,
106 List<AbstractExpectedComponent> expectedServices)
107 throws IOException {
108 List<String> actualComponents = readStringList(COMPONENT_STATUS_URL);
109 List<String> actualServices = readStringList(SERVICE_STATUS_URL);
110
111 assertThat(actualComponents, hasComponents(expectedComponents));
112 assertThat(actualServices, hasComponents(expectedServices));
113 }
114
115 abstract ComponentExpectations getProductSpecificExpectations();
116
117 private List<AbstractExpectedComponent> getExpectedComponentsWithDefaultProfile() {
118 final ComponentExpectations productSpecificExpectations = getProductSpecificExpectations();
119
120 return ImmutableList.<AbstractExpectedComponent>builder()
121
122 .addAll(productSpecificExpectations.getExpectedScannerCreatedComponents())
123 .addAll(productSpecificExpectations.getExpectedScannerCreatedComponentImports())
124 .addAll(productSpecificExpectations.getExpectedAutoAddedComponents())
125
126 .addAll(productSpecificExpectations.getProductSpecificScannerCreatedComponents())
127 .addAll(productSpecificExpectations.getProductSpecificScannerCreatedComponentImports())
128 .addAll(productSpecificExpectations.getProductSpecificAutoAddedComponents())
129 .build();
130 }
131
132 private List<AbstractExpectedComponent> getExpectedServiceExportsWithDefaultProfile() {
133
134 final ComponentExpectations expectations = ComponentExpectations.COMMON;
135
136 final ImmutableList.Builder<AbstractExpectedComponent> builder =
137 ImmutableList.<AbstractExpectedComponent>builder()
138 .addAll(expectations.getExpectedScannerCreatedExports())
139 .addAll(expectations.getExpectedAutoAddedExports());
140
141 if (Boolean.parseBoolean(System.getProperty("atlassian.dev.mode", "true"))) {
142 builder.addAll(expectations.getExpectedScannerCreatedDevExports());
143 }
144 return builder.build();
145 }
146
147 private static Matcher<Iterable<? extends String>> hasComponents(List<AbstractExpectedComponent> expectedComponents) {
148 Collection<Matcher<? super String>> componentMatchers =
149 expectedComponents.stream()
150 .map(AbstractExpectedComponent::getRuntimeComponentEntryMatcher)
151 .collect(Collectors.toList());
152 return Matchers.containsInAnyOrder(componentMatchers);
153 }
154 }