Skip to content

Commit 5012368

Browse files
authored
Remove Powermock from Spring Libraries (Azure#26895)
Remove Powermock from Spring Libraries
1 parent 35bf9ac commit 5012368

File tree

16 files changed

+164
-254
lines changed

16 files changed

+164
-254
lines changed

sdk/appconfiguration/azure-spring-cloud-appconfiguration-config-web/pom.xml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<version>2.4.0-beta.1</version><!-- {x-version-update;com.azure.spring:azure-spring-cloud-appconfiguration-config-web;current} -->
1616
<name>Azure Spring Cloud App Configuration Config Web</name>
1717
<description>Integration of Spring Cloud Config and Azure App Configuration Service</description>
18-
18+
1919
<properties>
2020
<codesnippet.skip>false</codesnippet.skip>
2121
<javadocDoclet></javadocDoclet>
@@ -75,18 +75,6 @@
7575
<version>2.6.2</version> <!-- {x-version-update;org.springframework.boot:spring-boot-starter-test;external_dependency} -->
7676
<scope>test</scope>
7777
</dependency>
78-
<dependency>
79-
<groupId>org.powermock</groupId>
80-
<artifactId>powermock-module-junit4</artifactId>
81-
<version>2.0.9</version> <!-- {x-version-update;org.powermock:powermock-module-junit4;external_dependency} -->
82-
<scope>test</scope>
83-
</dependency>
84-
<dependency>
85-
<groupId>org.powermock</groupId>
86-
<artifactId>powermock-api-mockito2</artifactId>
87-
<version>2.0.9</version> <!-- {x-version-update;org.powermock:powermock-api-mockito2;external_dependency} -->
88-
<scope>test</scope>
89-
</dependency>
9078
</dependencies>
9179

9280
<build>

sdk/appconfiguration/azure-spring-cloud-feature-management/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@
5151
<version>2.6.2</version> <!-- {x-version-update;org.springframework.boot:spring-boot-starter-test;external_dependency} -->
5252
<scope>test</scope>
5353
</dependency>
54-
<dependency>
55-
<groupId>org.powermock</groupId>
56-
<artifactId>powermock-module-junit4</artifactId>
57-
<version>2.0.9</version> <!-- {x-version-update;org.powermock:powermock-module-junit4;external_dependency} -->
58-
<scope>test</scope>
59-
</dependency>
60-
<dependency>
61-
<groupId>org.powermock</groupId>
62-
<artifactId>powermock-api-mockito2</artifactId>
63-
<version>2.0.9</version> <!-- {x-version-update;org.powermock:powermock-api-mockito2;external_dependency} -->
64-
<scope>test</scope>
65-
</dependency>
6654
</dependencies>
6755
<build>
6856
<plugins>

sdk/appconfiguration/spring-cloud-azure-appconfiguration-config-web/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,6 @@
6464
<version>2.6.2</version> <!-- {x-version-update;org.springframework.boot:spring-boot-starter-test;external_dependency} -->
6565
<scope>test</scope>
6666
</dependency>
67-
<dependency>
68-
<groupId>org.powermock</groupId>
69-
<artifactId>powermock-module-junit4</artifactId>
70-
<version>2.0.9</version> <!-- {x-version-update;org.powermock:powermock-module-junit4;external_dependency} -->
71-
<scope>test</scope>
72-
</dependency>
73-
<dependency>
74-
<groupId>org.powermock</groupId>
75-
<artifactId>powermock-api-mockito2</artifactId>
76-
<version>2.0.9</version> <!-- {x-version-update;org.powermock:powermock-api-mockito2;external_dependency} -->
77-
<scope>test</scope>
78-
</dependency>
7967
</dependencies>
8068

8169
<build>

sdk/appconfiguration/spring-cloud-azure-appconfiguration-config/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,6 @@
127127
<version>2.6.2</version> <!-- {x-version-update;org.springframework.boot:spring-boot-starter-test;external_dependency} -->
128128
<scope>test</scope>
129129
</dependency>
130-
<dependency>
131-
<groupId>org.powermock</groupId>
132-
<artifactId>powermock-module-junit4</artifactId>
133-
<version>2.0.9</version> <!-- {x-version-update;org.powermock:powermock-module-junit4;external_dependency} -->
134-
<scope>test</scope>
135-
</dependency>
136-
<dependency>
137-
<groupId>org.powermock</groupId>
138-
<artifactId>powermock-api-mockito2</artifactId>
139-
<version>2.0.9</version> <!-- {x-version-update;org.powermock:powermock-api-mockito2;external_dependency} -->
140-
<scope>test</scope>
141-
</dependency>
142130
<!-- Added this dependency to include necessary annotations used by reactor core.
143131
Without this dependency, javadoc throws a warning as it cannot find enum When.MAYBE
144132
which is used in @Nullable annotation in reactor core classes -->

sdk/appconfiguration/spring-cloud-azure-appconfiguration-config/src/main/java/com/microsoft/azure/spring/cloud/config/AppConfigurationPropertySource.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,6 @@
22
// Licensed under the MIT License.
33
package com.microsoft.azure.spring.cloud.config;
44

5-
import static com.microsoft.azure.spring.cloud.config.Constants.FEATURE_FLAG_CONTENT_TYPE;
6-
import static com.microsoft.azure.spring.cloud.config.Constants.FEATURE_FLAG_PREFIX;
7-
import static com.microsoft.azure.spring.cloud.config.Constants.FEATURE_MANAGEMENT_KEY;
8-
import static com.microsoft.azure.spring.cloud.config.Constants.KEY_VAULT_CONTENT_TYPE;
9-
10-
import java.io.IOException;
11-
import java.net.URI;
12-
import java.net.URISyntaxException;
13-
import java.util.Date;
14-
import java.util.HashMap;
15-
import java.util.LinkedHashMap;
16-
import java.util.List;
17-
import java.util.Map;
18-
import java.util.Set;
19-
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
import org.springframework.core.env.EnumerablePropertySource;
23-
import org.springframework.util.ReflectionUtils;
24-
255
import com.azure.data.appconfiguration.ConfigurationClient;
266
import com.azure.data.appconfiguration.models.ConfigurationSetting;
277
import com.azure.data.appconfiguration.models.SettingSelector;
@@ -35,6 +15,25 @@
3515
import com.microsoft.azure.spring.cloud.config.stores.ClientStore;
3616
import com.microsoft.azure.spring.cloud.config.stores.ConfigStore;
3717
import com.microsoft.azure.spring.cloud.config.stores.KeyVaultClient;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
import org.springframework.core.env.EnumerablePropertySource;
21+
import org.springframework.util.ReflectionUtils;
22+
23+
import java.io.IOException;
24+
import java.net.URI;
25+
import java.net.URISyntaxException;
26+
import java.util.Date;
27+
import java.util.HashMap;
28+
import java.util.LinkedHashMap;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Set;
32+
33+
import static com.microsoft.azure.spring.cloud.config.Constants.FEATURE_FLAG_CONTENT_TYPE;
34+
import static com.microsoft.azure.spring.cloud.config.Constants.FEATURE_FLAG_PREFIX;
35+
import static com.microsoft.azure.spring.cloud.config.Constants.FEATURE_MANAGEMENT_KEY;
36+
import static com.microsoft.azure.spring.cloud.config.Constants.KEY_VAULT_CONTENT_TYPE;
3837

3938
public class AppConfigurationPropertySource extends EnumerablePropertySource<ConfigurationClient> {
4039
private static final Logger LOGGER = LoggerFactory.getLogger(AppConfigurationPropertySource.class);
@@ -163,23 +162,24 @@ private String getKeyVaultEntry(String value) {
163162

164163
// Check if we already have a client for this key vault, if not we will make
165164
// one
166-
if (!keyVaultClients.containsKey(uri.getHost())) {
167-
KeyVaultClient client = new KeyVaultClient(appConfigurationProperties, uri, keyVaultCredentialProvider,
168-
keyVaultClientProvider);
169-
keyVaultClients.put(uri.getHost(), client);
170-
}
171-
KeyVaultSecret secret = keyVaultClients.get(uri.getHost()).getSecret(uri, appProperties.getMaxRetryTime());
165+
KeyVaultSecret secret = getKeyVaultClient(uri, uri.getHost())
166+
.getSecret(uri, appProperties.getMaxRetryTime());
172167
if (secret == null) {
173168
throw new IOException("No Key Vault Secret found for Reference.");
174169
}
175170
secretValue = secret.getValue();
176171
} catch (RuntimeException | IOException e) {
177-
LOGGER.error("Error Retreiving Key Vault Entry");
172+
LOGGER.error("Error Retrieving Key Vault Entry");
178173
ReflectionUtils.rethrowRuntimeException(e);
179174
}
180175
return secretValue;
181176
}
182177

178+
KeyVaultClient getKeyVaultClient(URI uri, String uriHost) {
179+
return keyVaultClients.computeIfAbsent(uriHost, ignored ->
180+
new KeyVaultClient(appConfigurationProperties, uri, keyVaultCredentialProvider, keyVaultClientProvider));
181+
}
182+
183183
/**
184184
* Initializes Feature Management configurations. Only one
185185
* {@code AppConfigurationPropertySource} can call this, and it needs to be done after
@@ -243,7 +243,7 @@ private Object createFeature(ConfigurationSetting item) throws IOException {
243243
return feature;
244244

245245
} catch (IOException e) {
246-
throw new IOException("Unabled to parse Feature Management values from Azure.", e);
246+
throw new IOException("Unable to parse Feature Management values from Azure.", e);
247247
}
248248

249249
} else {

sdk/appconfiguration/spring-cloud-azure-appconfiguration-config/src/main/java/com/microsoft/azure/spring/cloud/config/stores/KeyVaultClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ KeyVaultClient build() {
5757
String fullUri = "https://" + uri.getHost();
5858

5959
if (tokenCredential != null && msiProps != null) {
60-
throw new IllegalArgumentException("More than 1 Conncetion method was set for connecting to Key Vault.");
60+
throw new IllegalArgumentException("More than 1 Connection method was set for connecting to Key Vault.");
6161
}
6262

6363
if (tokenCredential != null) {

sdk/appconfiguration/spring-cloud-azure-appconfiguration-config/src/test/java/com/microsoft/azure/spring/cloud/config/AppConfigurationBootstrapConfigurationTest.java

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,7 @@
22
// Licensed under the MIT License.
33
package com.microsoft.azure.spring.cloud.config;
44

5-
import static com.microsoft.azure.spring.cloud.config.TestConstants.CONN_STRING_PROP;
6-
import static com.microsoft.azure.spring.cloud.config.TestConstants.FAIL_FAST_PROP;
7-
import static com.microsoft.azure.spring.cloud.config.TestConstants.STORE_ENDPOINT_PROP;
8-
import static com.microsoft.azure.spring.cloud.config.TestConstants.TEST_CONN_STRING;
9-
import static com.microsoft.azure.spring.cloud.config.TestConstants.TEST_STORE_NAME;
10-
import static com.microsoft.azure.spring.cloud.config.TestUtils.propPair;
11-
import static org.assertj.core.api.Assertions.assertThat;
12-
import static org.junit.Assert.fail;
13-
import static org.mockito.Mockito.when;
14-
import static org.powermock.api.mockito.PowerMockito.whenNew;
15-
16-
import java.io.InputStream;
17-
5+
import com.microsoft.azure.spring.cloud.config.stores.ClientStore;
186
import org.apache.http.HttpEntity;
197
import org.apache.http.ProtocolVersion;
208
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -23,12 +11,20 @@
2311
import org.junit.Test;
2412
import org.mockito.Mock;
2513
import org.mockito.MockitoAnnotations;
26-
import org.powermock.api.mockito.PowerMockito;
2714
import org.springframework.boot.autoconfigure.AutoConfigurations;
2815
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2916

30-
import com.fasterxml.jackson.databind.ObjectMapper;
31-
import com.microsoft.azure.spring.cloud.config.stores.ClientStore;
17+
import java.io.InputStream;
18+
19+
import static com.microsoft.azure.spring.cloud.config.TestConstants.CONN_STRING_PROP;
20+
import static com.microsoft.azure.spring.cloud.config.TestConstants.FAIL_FAST_PROP;
21+
import static com.microsoft.azure.spring.cloud.config.TestConstants.STORE_ENDPOINT_PROP;
22+
import static com.microsoft.azure.spring.cloud.config.TestConstants.TEST_CONN_STRING;
23+
import static com.microsoft.azure.spring.cloud.config.TestConstants.TEST_STORE_NAME;
24+
import static com.microsoft.azure.spring.cloud.config.TestUtils.propPair;
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.junit.Assert.fail;
27+
import static org.mockito.Mockito.when;
3228

3329
public class AppConfigurationBootstrapConfigurationTest {
3430
private static final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
@@ -44,17 +40,10 @@ public class AppConfigurationBootstrapConfigurationTest {
4440
@Mock
4541
InputStream mockInputStream;
4642

47-
@Mock
48-
ObjectMapper mockObjectMapper;
49-
50-
@Mock
51-
ClientStore clientStoreMock;
52-
5343
@Before
5444
public void setup() {
55-
MockitoAnnotations.initMocks(this);
45+
MockitoAnnotations.openMocks(this);
5646
try {
57-
PowerMockito.whenNew(ObjectMapper.class).withAnyArguments().thenReturn(mockObjectMapper);
5847
when(mockClosableHttpResponse.getStatusLine())
5948
.thenReturn(new BasicStatusLine(new ProtocolVersion("", 0, 0), 200, ""));
6049
when(mockClosableHttpResponse.getEntity()).thenReturn(mockHttpEntity);
@@ -65,36 +54,30 @@ public void setup() {
6554
}
6655

6756
@Test
68-
public void iniConnectionStringSystemAssigned() throws Exception {
69-
whenNew(ClientStore.class).withAnyArguments().thenReturn(clientStoreMock);
57+
public void iniConnectionStringSystemAssigned() {
7058
contextRunner.withPropertyValues(propPair(FAIL_FAST_PROP, "false"))
7159
.run(context -> assertThat(context).hasSingleBean(AppConfigurationPropertySourceLocator.class));
7260
}
7361

7462
@Test
75-
public void iniConnectionStringUserAssigned() throws Exception {
76-
whenNew(ClientStore.class).withAnyArguments().thenReturn(clientStoreMock);
63+
public void iniConnectionStringUserAssigned() {
7764
contextRunner
7865
.withPropertyValues(propPair(FAIL_FAST_PROP, "false"),
7966
propPair("spring.cloud.azure.appconfiguration.managed-identity.client-id", "client-id"))
8067
.run(context -> assertThat(context).hasSingleBean(AppConfigurationPropertySourceLocator.class));
8168
}
8269

8370
@Test
84-
public void propertySourceLocatorBeanCreated() throws Exception {
85-
whenNew(ClientStore.class).withAnyArguments().thenReturn(clientStoreMock);
71+
public void propertySourceLocatorBeanCreated() {
8672
contextRunner
8773
.withPropertyValues(propPair(CONN_STRING_PROP, TEST_CONN_STRING), propPair(FAIL_FAST_PROP, "false"))
8874
.run(context -> assertThat(context).hasSingleBean(AppConfigurationPropertySourceLocator.class));
8975
}
9076

9177
@Test
92-
public void clientsBeanCreated() throws Exception {
93-
whenNew(ClientStore.class).withAnyArguments().thenReturn(clientStoreMock);
78+
public void clientsBeanCreated() {
9479
contextRunner
9580
.withPropertyValues(propPair(CONN_STRING_PROP, TEST_CONN_STRING), propPair(FAIL_FAST_PROP, "false"))
96-
.run(context -> {
97-
assertThat(context).hasSingleBean(ClientStore.class);
98-
});
81+
.run(context -> assertThat(context).hasSingleBean(ClientStore.class));
9982
}
10083
}

sdk/appconfiguration/spring-cloud-azure-appconfiguration-config/src/test/java/com/microsoft/azure/spring/cloud/config/AppConfigurationPropertySourceKeyVaultTest.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.microsoft.azure.spring.cloud.config.TestUtils.createItem;
2222
import static org.assertj.core.api.Assertions.assertThat;
2323
import static org.junit.Assert.fail;
24+
import static org.mockito.ArgumentMatchers.any;
2425
import static org.mockito.BDDMockito.given;
2526
import static org.mockito.Mockito.when;
2627

@@ -35,13 +36,9 @@
3536
import org.junit.Rule;
3637
import org.junit.Test;
3738
import org.junit.rules.ExpectedException;
38-
import org.junit.runner.RunWith;
3939
import org.mockito.Mock;
4040
import org.mockito.Mockito;
4141
import org.mockito.MockitoAnnotations;
42-
import org.powermock.api.mockito.PowerMockito;
43-
import org.powermock.core.classloader.annotations.PrepareForTest;
44-
import org.powermock.modules.junit4.PowerMockRunner;
4542

4643
import com.azure.core.http.rest.PagedFlux;
4744
import com.azure.core.http.rest.PagedResponse;
@@ -55,8 +52,6 @@
5552
import reactor.core.publisher.Flux;
5653
import reactor.core.publisher.Mono;
5754

58-
@RunWith(PowerMockRunner.class)
59-
@PrepareForTest({ AppConfigurationPropertySource.class })
6055
public class AppConfigurationPropertySourceKeyVaultTest {
6156
private static final String EMPTY_CONTENT_TYPE = "";
6257

@@ -121,33 +116,57 @@ public static void init() {
121116

122117
@Before
123118
public void setup() {
124-
MockitoAnnotations.initMocks(this);
119+
MockitoAnnotations.openMocks(this);
125120
appConfigurationProperties = new AppConfigurationProperties();
126121
appProperties = new AppConfigurationProviderProperties();
127122
appProperties.setMaxRetryTime(0);
128123
ConfigStore testStore = new ConfigStore();
129124
testStore.setEndpoint(TEST_STORE_NAME);
130125
ArrayList<String> contexts = new ArrayList<String>();
131126
contexts.add("/application/*");
132-
propertySource = new AppConfigurationPropertySource(TEST_CONTEXT, testStore, "\0",
133-
appConfigurationProperties, clientStoreMock, appProperties, tokenCredentialProvider, null);
134-
135-
testItems = new ArrayList<ConfigurationSetting>();
127+
propertySource = new KeyVaultMockingAppConfigurationPropertySource(TEST_CONTEXT, testStore, "\0",
128+
appConfigurationProperties, clientStoreMock, appProperties, tokenCredentialProvider, null);
129+
testItems = new ArrayList<>();
136130
testItems.add(item1);
137131
testItems.add(item2);
138132
testItems.add(item3);
139133
}
140134

135+
static class KeyVaultMockingAppConfigurationPropertySource extends AppConfigurationPropertySource {
136+
private KeyVaultClient keyVaultClient;
137+
138+
KeyVaultMockingAppConfigurationPropertySource(String context, ConfigStore configStore, String label,
139+
AppConfigurationProperties appConfigurationProperties, ClientStore clients,
140+
AppConfigurationProviderProperties appProperties, KeyVaultCredentialProvider keyVaultCredentialProvider,
141+
SecretClientBuilderSetup keyVaultClientProvider) {
142+
super(context, configStore, label, appConfigurationProperties, clients, appProperties,
143+
keyVaultCredentialProvider, keyVaultClientProvider);
144+
}
145+
146+
private void setKeyVaultClient(KeyVaultClient keyVaultClient) {
147+
this.keyVaultClient = keyVaultClient;
148+
}
149+
150+
@Override
151+
KeyVaultClient getKeyVaultClient(URI uri, String uriHost) {
152+
if (keyVaultClient == null) {
153+
return super.getKeyVaultClient(uri, uriHost);
154+
} else {
155+
return keyVaultClient;
156+
}
157+
}
158+
}
159+
141160
@Test
142161
public void testKeyVaultTest() throws Exception {
143162
testItems.add(keyVaultItem);
144-
when(clientStoreMock.listSettings(Mockito.any(), Mockito.anyString())).thenReturn(testItems)
145-
.thenReturn(new ArrayList<ConfigurationSetting>());
163+
when(clientStoreMock.listSettings(any(), Mockito.anyString())).thenReturn(testItems)
164+
.thenReturn(new ArrayList<>());
146165
KeyVaultClient client = Mockito.mock(KeyVaultClient.class);
147-
PowerMockito.whenNew(KeyVaultClient.class).withAnyArguments().thenReturn(client);
166+
((KeyVaultMockingAppConfigurationPropertySource) propertySource).setKeyVaultClient(client);
148167

149168
KeyVaultSecret secret = new KeyVaultSecret("mySecret", "mySecretValue");
150-
given(client.getSecret(Mockito.any(URI.class), Mockito.anyInt())).willReturn(secret);
169+
given(client.getSecret(any(URI.class), Mockito.anyInt())).willReturn(secret);
151170

152171
FeatureSet featureSet = new FeatureSet();
153172

0 commit comments

Comments
 (0)