@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void loadResourceConfigurationWithType() throws Exception { // tell the method story as it happens: mock or create dependencies and configure // those dependencies to get the method under test to completion. ResourceContext mockResourceContext = mock(ResourceContext.class); ResourceType mockResourceType = mock(ResourceType.class); when(mockResourceContext.getResourceType()).thenReturn(mockResourceType); ConfigurationDefinition mockConfigurationDefinition = mock(ConfigurationDefinition.class); when(mockResourceType.getResourceConfigurationDefinition()) .thenReturn(mockConfigurationDefinition); ConfigurationTemplate mockConfigurationTemplate = mock(ConfigurationTemplate.class); when(mockConfigurationDefinition.getDefaultTemplate()).thenReturn(mockConfigurationTemplate); Configuration mockConfiguration = mock(Configuration.class); when(mockConfigurationTemplate.getConfiguration()).thenReturn(mockConfiguration); Property mockProperty = mock(Property.class); when(mockConfiguration.get(eq("__type"))).thenReturn(mockProperty); Map<String, PropertyDefinition> mockMap = (Map<String, PropertyDefinition>) mock(Map.class); when(mockConfigurationDefinition.getPropertyDefinitions()).thenReturn(mockMap); ConfigurationLoadDelegate mockConfigurationLoadDelegate = mock(ConfigurationLoadDelegate.class); PowerMockito.whenNew(ConfigurationLoadDelegate.class) .withParameterTypes(ConfigurationDefinition.class, ASConnection.class, Address.class) .withArguments( any(ConfigurationDefinition.class), any(ASConnection.class), any(Address.class)) .thenReturn(mockConfigurationLoadDelegate); when(mockConfigurationLoadDelegate.loadResourceConfiguration()).thenReturn(mockConfiguration); PropertySimple pathPropertySimple = new PropertySimple("path", "abc=def,xyz=test1"); when(mockConfiguration.get(eq("path"))).thenReturn(pathPropertySimple); when(mockResourceContext.getPluginConfiguration()).thenReturn(mockConfiguration); ASConnection mockASConnection = mock(ASConnection.class); PropertySimple typePropertySimple = new PropertySimple("__type", "xyz"); PowerMockito.whenNew(PropertySimple.class) .withParameterTypes(String.class, Object.class) .withArguments(eq("__type"), eq("xyz")) .thenReturn(typePropertySimple); // create object to test and inject required dependencies TemplatedComponent objectUnderTest = new TemplatedComponent(); objectUnderTest.context = mockResourceContext; objectUnderTest.testConnection = mockASConnection; // run code under test Configuration result = objectUnderTest.loadResourceConfiguration(); // verify the results (Assert and mock verification) Assert.assertEquals(result, mockConfiguration); verify(mockMap, times(1)).remove(eq("__type")); verify(mockConfiguration, times(1)).get(eq("__type")); verify(mockConfiguration, never()).get(eq("__name")); verify(mockConfiguration, times(1)).get(eq("path")); verify(mockConfiguration, times(1)).put(eq(typePropertySimple)); PowerMockito.verifyNew(PropertySimple.class).withArguments(eq("__type"), eq("xyz")); PowerMockito.verifyNew(ConfigurationLoadDelegate.class) .withArguments( any(ConfigurationDefinition.class), eq(mockASConnection), any(Address.class)); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void updateResourceConfigurationWithName() throws Exception { // tell the method story as it happens: mock or create dependencies and configure // those dependencies to get the method under test to completion. ResourceContext mockResourceContext = mock(ResourceContext.class); ResourceType mockResourceType = mock(ResourceType.class); when(mockResourceContext.getResourceType()).thenReturn(mockResourceType); ConfigurationDefinition mockConfigurationDefinition = mock(ConfigurationDefinition.class); when(mockResourceType.getResourceConfigurationDefinition()) .thenReturn(mockConfigurationDefinition); ConfigurationDefinition mockConfigurationDefinitionCopy = mock(ConfigurationDefinition.class); when(mockConfigurationDefinition.copy()).thenReturn(mockConfigurationDefinitionCopy); ConfigurationTemplate mockConfigurationTemplate = mock(ConfigurationTemplate.class); when(mockConfigurationDefinitionCopy.getDefaultTemplate()) .thenReturn(mockConfigurationTemplate); Configuration mockConfiguration = mock(Configuration.class); when(mockConfigurationTemplate.getConfiguration()).thenReturn(mockConfiguration); Property mockProperty = mock(Property.class); when(mockConfiguration.get(eq("__type"))).thenReturn(null); when(mockConfiguration.get(eq("__name"))).thenReturn(mockProperty); Map<String, PropertyDefinition> mockMap = (Map<String, PropertyDefinition>) mock(Map.class); when(mockConfigurationDefinitionCopy.getPropertyDefinitions()).thenReturn(mockMap); ConfigurationUpdateReport mockReport = mock(ConfigurationUpdateReport.class); when(mockReport.getConfiguration()).thenReturn(mockConfiguration); ConfigurationWriteDelegate mockConfigurationWriteDelegate = mock(ConfigurationWriteDelegate.class); PowerMockito.whenNew(ConfigurationWriteDelegate.class) .withParameterTypes(ConfigurationDefinition.class, ASConnection.class, Address.class) .withArguments( any(ConfigurationDefinition.class), any(ASConnection.class), any(Address.class)) .thenReturn(mockConfigurationWriteDelegate); ASConnection mockASConnection = mock(ASConnection.class); when(mockASConnection.execute(any(ReadResource.class))).thenReturn(new Result()); // create object to test and inject required dependencies TemplatedComponent objectUnderTest = new TemplatedComponent(); objectUnderTest.context = mockResourceContext; objectUnderTest.testConnection = mockASConnection; // run code under test objectUnderTest.updateResourceConfiguration(mockReport); // verify the results (Assert and mock verification) verify(mockMap, times(1)).remove(eq("__name")); verify(mockConfiguration, times(1)).get(eq("__type")); verify(mockConfiguration, times(1)).get(eq("__name")); verify(mockConfiguration, times(1)).remove(eq("__name")); PowerMockito.verifyNew(ConfigurationWriteDelegate.class) .withArguments( any(ConfigurationDefinition.class), eq(mockASConnection), any(Address.class)); }