### Question:
PermissionBasedFieldVisibility implements GraphqlFieldVisibility { @Override public GraphQLFieldDefinition getFieldDefinition(GraphQLFieldsContainer fieldsContainer, String fieldName) { Iterator<GraphQLFieldDefinition> graphQlFieldDefinitionIterator = fieldsContainer.getFieldDefinitions().iterator(); while (graphQlFieldDefinitionIterator.hasNext()) { GraphQLFieldDefinition graphQlFieldDefinition = graphQlFieldDefinitionIterator.next(); if (!dataSetNamesWithOutReadPermission.contains(graphQlFieldDefinition.getName()) && graphQlFieldDefinition.getName().equals(fieldName)) { return graphQlFieldDefinition; } } return null; } PermissionBasedFieldVisibility(UserPermissionCheck userPermissionCheck, DataSetRepository dataSetRepository); @Override List<GraphQLFieldDefinition> getFieldDefinitions(GraphQLFieldsContainer fieldsContainer); @Override GraphQLFieldDefinition getFieldDefinition(GraphQLFieldsContainer fieldsContainer, String fieldName);  }### Answer:
@Test public void getFieldDefinitionReturnsNullIfUserHasNoPermission() throws Exception { final DataSetRepository dataSetRepository = mock(DataSetRepository.class); DataSet dataSet = createDataSetWithUserPermissions( "user__dataSetUserHasAccessTo", Sets.newHashSet(Permission.READ) ); DataSet dataSet2 = createDataSetWithUserPermissions( "user__dataSetUserDoesNotHasAccessTo", Sets.newHashSet() ); Collection<DataSet> dataSetCollection = Sets.newHashSet(dataSet, dataSet2); given(dataSetRepository.getDataSets()).willReturn(dataSetCollection); final PermissionBasedFieldVisibility permissionBasedFieldVisibility = new PermissionBasedFieldVisibility(userPermissionCheck, dataSetRepository); final GraphQLFieldsContainer graphQlFieldsContainer = createGraphQlFieldsContainer( "user__dataSetUserHasAccessTo", "user__dataSetUserDoesNotHasAccessTo" ); GraphQLFieldDefinition retrievedGraphQlFieldDefinition = permissionBasedFieldVisibility .getFieldDefinition(graphQlFieldsContainer,"user__dataSetUserDoesNotHasAccessTo"); assertThat(retrievedGraphQlFieldDefinition,is(nullValue())); }
@Test public void getFieldDefinitionReturnsFieldDefinitionIfUserHasPermission() throws Exception { final DataSetRepository dataSetRepository = mock(DataSetRepository.class); DataSet dataSet = createDataSetWithUserPermissions( "user__dataSetUserHasAccessTo", Sets.newHashSet(Permission.READ) ); DataSet dataSet2 = createDataSetWithUserPermissions( "user__dataSetUserDoesNotHasAccessTo", Sets.newHashSet() ); Collection<DataSet> dataSetCollection = Sets.newHashSet(dataSet, dataSet2); given(dataSetRepository.getDataSets()).willReturn(dataSetCollection); final PermissionBasedFieldVisibility permissionBasedFieldVisibility = new PermissionBasedFieldVisibility(userPermissionCheck, dataSetRepository); final GraphQLFieldsContainer graphQlFieldsContainer = createGraphQlFieldsContainer( "user__dataSetUserHasAccessTo", "user__dataSetUserDoesNotHasAccessTo" ); GraphQLFieldDefinition retrievedGraphQlFieldDefinition = permissionBasedFieldVisibility .getFieldDefinition(graphQlFieldsContainer,"user__dataSetUserHasAccessTo"); assertThat(retrievedGraphQlFieldDefinition,hasProperty("name", is("user__dataSetUserHasAccessTo"))); }
@Test public void getFieldDefinitionReturnsFieldDefinitionIfNotDataSetField() throws Exception { final DataSetRepository dataSetRepository = mock(DataSetRepository.class); DataSet dataSet = createDataSetWithUserPermissions( "user__dataSetUserHasAccessTo", Sets.newHashSet(Permission.READ) ); DataSet dataSet2 = createDataSetWithUserPermissions( "user__dataSetUserDoesNotHasAccessTo", Sets.newHashSet() ); Collection<DataSet> dataSetCollection = Sets.newHashSet(dataSet, dataSet2); given(dataSetRepository.getDataSets()).willReturn(dataSetCollection); final PermissionBasedFieldVisibility permissionBasedFieldVisibility = new PermissionBasedFieldVisibility(userPermissionCheck, dataSetRepository); final GraphQLFieldsContainer graphQlFieldsContainer = createGraphQlFieldsContainer( "user__dataSetUserHasAccessTo", "user__dataSetUserDoesNotHasAccessTo", "nonDataSetField" ); GraphQLFieldDefinition retrievedGraphQlFieldDefinition = permissionBasedFieldVisibility .getFieldDefinition(graphQlFieldsContainer,new String("nonDataSetField")); assertThat(retrievedGraphQlFieldDefinition,hasProperty("name", is("nonDataSetField"))); }