Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/742#discussion_r123496290
--- Diff:
locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/networking/creator/DefaultAzureArmNetworkCreatorTest.java
---
@@ -90,62 +97,44 @@ public void testPreExisting() {
verify(azureComputeApi).getSubnetApi(TEST_RESOURCE_GROUP,
TEST_NETWORK_NAME);
Map<String, Object> templateOptions =
configBag.get(TEMPLATE_OPTIONS);
- Map<String, Object> ipOptions = (Map<String,
Object>)templateOptions.get("ipOptions");
+ Map<?, ?> ipOptions = (Map<?, ?>)templateOptions.get("ipOptions");
assertEquals(ipOptions.get("subnet"), TEST_SUBNET_ID);
assertEquals(ipOptions.get("allocateNewPublicIp"), true);
}
@Test
- public void testVanilla() {
- //Setup config bag
- ConfigBag configBag = ConfigBag.newInstance();
- configBag.put(CLOUD_REGION_ID, TEST_LOCATION);
-
- //Setup mocks
-
when(computeService.getContext().unwrapApi(AzureComputeApi.class)).thenReturn(azureComputeApi);
- when(azureComputeApi.getSubnetApi(TEST_RESOURCE_GROUP,
TEST_NETWORK_NAME)).thenReturn(subnetApi);
-
when(subnetApi.get(TEST_SUBNET_NAME)).thenReturn(null).thenReturn(subnet);
//null first time, subnet next
- when(subnet.id()).thenReturn(TEST_SUBNET_ID);
-
-
when(azureComputeApi.getResourceGroupApi()).thenReturn(resourceGroupApi);
- when(resourceGroupApi.get(TEST_RESOURCE_GROUP)).thenReturn(null);
-
-
when(azureComputeApi.getVirtualNetworkApi(TEST_RESOURCE_GROUP)).thenReturn(virtualNetworkApi);
-
-
- //Test
-
DefaultAzureArmNetworkCreator.createDefaultNetworkAndAddToTemplateOptionsIfRequired(computeService,
configBag);
-
- //verify
- verify(subnetApi, times(2)).get(TEST_SUBNET_NAME);
- verify(subnet).id();
- verify(azureComputeApi,
times(2)).getSubnetApi(TEST_RESOURCE_GROUP, TEST_NETWORK_NAME);
-
- verify(azureComputeApi, times(2)).getResourceGroupApi();
- verify(resourceGroupApi).get(TEST_RESOURCE_GROUP);
- verify(azureComputeApi).getVirtualNetworkApi(TEST_RESOURCE_GROUP);
-
- Map<String, Object> templateOptions =
configBag.get(TEMPLATE_OPTIONS);
- Map<String, Object> ipOptions = (Map<String,
Object>)templateOptions.get("ipOptions");
- assertEquals(ipOptions.get("subnet"), TEST_SUBNET_ID);
- assertEquals(ipOptions.get("allocateNewPublicIp"), true);
+ public void testVanillaWhereNoResourceGroup() throws Exception {
+ runVanilla(ImmutableMap.of(), false);
}
-
+
@Test
- public void testVanillaWhereTemplateOptionsAlreadySpecified() {
+ public void testVanillaWhereExistingResourceGroup() throws Exception {
+ runVanilla(ImmutableMap.of(), true);
+ }
+
+ @Test
+ public void testVanillaWhereTemplateOptionsAlreadySpecified() throws
Exception {
+ ImmutableMap<?, ?> additionalConfig =
ImmutableMap.of(TEMPLATE_OPTIONS, ImmutableMap.of("unrelated-key",
"unrelated-value"));
+ ConfigBag result = runVanilla(additionalConfig, false);
+ Map<String, ?> templateOptions = result.get(TEMPLATE_OPTIONS);
+ assertEquals(templateOptions.get("unrelated-key"),
"unrelated-value");
+ }
+
+ protected ConfigBag runVanilla(Map<?, ?> additionalConfig, boolean
resoureGroupExists) throws Exception {
--- End diff --
When I first wrote the `runVanilla` method, it was exactly the same length
as your existing test method - everything was duplicated in the two methods,
with the only difference being one line at the start (to set up config) and one
line at the end (to assert the config was still there). The tests are logically
identical: the behaviour should be the same if you pass extra templateOptions
or not. Therefore having a `runVanilla(Map<?,?> additoinalConfig)` makes sense
I think.
The more controversial thing is then when adding `boolean
resourceGroupExists` (which was previously untested). I could have duplicated
the method - no strong feelings.
I think we can make our tests less brittle by being more relaxed about what
we assert. For example `verify(subnetApi, times(2)).get(TEST_SUBNET_NAME)` - do
we really care that it's exactly twice, or do we just care that it's
`atLeastOnce()`?
I'll split it into two separate test methods, with duplication, as you
suggest.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---