UpdateConnection { private String checkDefinition(Connection connection){ String errorInfo = null; Map<ConnectionType, ConnectionDefinition> connectionDefinitionMap = getDefinitions.getConnectionDefinition(); if (connectionDefinitionMap.isEmpty()){ return "This type of connection has not been defined."; } else { if (connectionDefinitionMap.containsKey(connection.getConnectionType())){ List<Property> connectionProperties = connection.getProperty(); List<PropertyDefinition> propertyDefinitions = connectionDefinitionMap.get(connection.getConnectionType()).getPropertyDefinition(); if (connectionProperties != null && propertyDefinitions ==null) { errorInfo = "There are no properties for this type of connection."; } else if (connectionProperties != null && propertyDefinitions != null) { errorInfo = checkProperty(connection.getProperty(), propertyDefinitions); } } else { return "This type of connection has not been defined."; } } return errorInfo; } UpdateConnection(DataBroker dataBroker, TenantManage tenantManage); String ConnectionHandling(UserId userId, Connection connection);  }
@Test public void testcheckDefinition() throws Exception { Class<UpdateConnection> class3 = UpdateConnection.class; Method method = class3.getDeclaredMethod("checkDefinition",new Class[]{Connection.class}); method.setAccessible(true); CheckedFuture connectiondefinitionFuture = mock(CheckedFuture.class); ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(connectiondefinitionFuture); List<ConnectionDefinition> connectionDefinitionList = new ArrayList<ConnectionDefinition>(); List<ConnectionDefinition> connectionDefinitionListnull = new ArrayList<ConnectionDefinition>(); ConnectionDefinition connectionDefinition = mock(ConnectionDefinition.class); connectionDefinitionList.add(connectionDefinition); when(connectionDefinition.getConnectionType()).thenReturn(connectionType); Class<UpdateConnection> class1 = UpdateConnection.class; Class<GetDefinitions> class2 = GetDefinitions.class; Field field = class1.getDeclaredField("getDefinitions"); field.setAccessible(true); Field field1 = class2.getDeclaredField("connectionDefinitionsList"); field1.setAccessible(true); field1.set(field.get(updateConnection), connectionDefinitionList); when(connection.getConnectionType()).thenReturn(connectionType); when(connection.getProperty()).thenReturn(propertyList); when(connectionDefinition.getPropertyDefinition()).thenReturn(null); Assert.assertEquals(method.invoke(updateConnection, connection), "There are no properties for this type of connection."); field1.set(field.get(updateConnection), connectionDefinitionList); when(connection.getConnectionType()).thenReturn(connectionType); when(connection.getProperty()).thenReturn(propertyList); when(connectionDefinition.getPropertyDefinition()).thenReturn(propertyDefinitionList); when(property.getPropertyName()).thenReturn(propertyName); when(propertyDefinition.getPropertyName()).thenReturn(propertyName2); when(property.getPropertyName().toString()).thenReturn(new String("propertyname")); Assert.assertEquals(method.invoke(updateConnection, connection), "This type of propertypropertyname has not been defined."); when(connection.getConnectionType()).thenReturn(connectionType2); Assert.assertEquals(method.invoke(updateConnection, connection),"This type of connection has not been defined."); field1.set(field.get(updateConnection), connectionDefinitionListnull); Assert.assertEquals(method.invoke(updateConnection, connection), "This type of connection has not been defined."); }