@Override
	public void exportData(JsonWriter writer) throws IOException {

		// version tag at the root
		writer.name(THIS_VERSION);

		writer.beginObject();

		// clients list
		writer.name(CLIENTS);
		writer.beginArray();
		writeClients(writer);
		writer.endArray();

		writer.name(GRANTS);
		writer.beginArray();
		writeGrants(writer);
		writer.endArray();

		writer.name(WHITELISTEDSITES);
		writer.beginArray();
		writeWhitelistedSites(writer);
		writer.endArray();

		writer.name(BLACKLISTEDSITES);
		writer.beginArray();
		writeBlacklistedSites(writer);
		writer.endArray();

		writer.name(AUTHENTICATIONHOLDERS);
		writer.beginArray();
		writeAuthenticationHolders(writer);
		writer.endArray();

		writer.name(ACCESSTOKENS);
		writer.beginArray();
		writeAccessTokens(writer);
		writer.endArray();

		writer.name(REFRESHTOKENS);
		writer.beginArray();
		writeRefreshTokens(writer);
		writer.endArray();

		writer.name(SYSTEMSCOPES);
		writer.beginArray();
		writeSystemScopes(writer);
		writer.endArray();

		for (MITREidDataServiceExtension extension : extensions) {
			if (extension.supportsVersion(THIS_VERSION)) {
				extension.exportExtensionData(writer);
				break;
			}
		}

		writer.endObject(); // end mitreid-connect-1.3
	}
@Test
	public void testExportAuthenticationHolders() throws IOException {
		OAuth2Request req1 = new OAuth2Request(new HashMap<String, String>(), "client1", new ArrayList<GrantedAuthority>(),
				true, new HashSet<String>(), new HashSet<String>(), "http://foo.com",
				new HashSet<String>(), null);
		Authentication mockAuth1 = new UsernamePasswordAuthenticationToken("user1", "pass1", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
		OAuth2Authentication auth1 = new OAuth2Authentication(req1, mockAuth1);

		AuthenticationHolderEntity holder1 = new AuthenticationHolderEntity();
		holder1.setId(1L);
		holder1.setAuthentication(auth1);

		OAuth2Request req2 = new OAuth2Request(new HashMap<String, String>(), "client2", new ArrayList<GrantedAuthority>(),
				true, new HashSet<String>(), new HashSet<String>(), "http://bar.com",
				new HashSet<String>(), null);
		OAuth2Authentication auth2 = new OAuth2Authentication(req2, null);

		AuthenticationHolderEntity holder2 = new AuthenticationHolderEntity();
		holder2.setId(2L);
		holder2.setAuthentication(auth2);

		List<AuthenticationHolderEntity> allAuthHolders = ImmutableList.of(holder1, holder2);

		when(clientRepository.getAllClients()).thenReturn(new HashSet<ClientDetailsEntity>());
		when(approvedSiteRepository.getAll()).thenReturn(new HashSet<ApprovedSite>());
		when(wlSiteRepository.getAll()).thenReturn(new HashSet<WhitelistedSite>());
		when(blSiteRepository.getAll()).thenReturn(new HashSet<BlacklistedSite>());
		when(authHolderRepository.getAll()).thenReturn(allAuthHolders);
		when(tokenRepository.getAllAccessTokens()).thenReturn(new HashSet<OAuth2AccessTokenEntity>());
		when(tokenRepository.getAllRefreshTokens()).thenReturn(new HashSet<OAuth2RefreshTokenEntity>());
		when(sysScopeRepository.getAll()).thenReturn(new HashSet<SystemScope>());

		// do the data export
		StringWriter stringWriter = new StringWriter();
		JsonWriter writer = new JsonWriter(stringWriter);
		writer.beginObject();
		dataService.exportData(writer);
		writer.endObject();
		writer.close();

		// parse the output as a JSON object for testing
		JsonElement elem = new JsonParser().parse(stringWriter.toString());
		JsonObject root = elem.getAsJsonObject();

		// make sure the root is there
		assertThat(root.has(MITREidDataService.MITREID_CONNECT_1_3), is(true));

		JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_3).getAsJsonObject();

		// make sure all the root elements are there
		assertThat(config.has(MITREidDataService.CLIENTS), is(true));
		assertThat(config.has(MITREidDataService.GRANTS), is(true));
		assertThat(config.has(MITREidDataService.WHITELISTEDSITES), is(true));
		assertThat(config.has(MITREidDataService.BLACKLISTEDSITES), is(true));
		assertThat(config.has(MITREidDataService.REFRESHTOKENS), is(true));
		assertThat(config.has(MITREidDataService.ACCESSTOKENS), is(true));
		assertThat(config.has(MITREidDataService.SYSTEMSCOPES), is(true));
		assertThat(config.has(MITREidDataService.AUTHENTICATIONHOLDERS), is(true));

		// make sure the root elements are all arrays
		assertThat(config.get(MITREidDataService.CLIENTS).isJsonArray(), is(true));
		assertThat(config.get(MITREidDataService.GRANTS).isJsonArray(), is(true));
		assertThat(config.get(MITREidDataService.WHITELISTEDSITES).isJsonArray(), is(true));
		assertThat(config.get(MITREidDataService.BLACKLISTEDSITES).isJsonArray(), is(true));
		assertThat(config.get(MITREidDataService.REFRESHTOKENS).isJsonArray(), is(true));
		assertThat(config.get(MITREidDataService.ACCESSTOKENS).isJsonArray(), is(true));
		assertThat(config.get(MITREidDataService.SYSTEMSCOPES).isJsonArray(), is(true));
		assertThat(config.get(MITREidDataService.AUTHENTICATIONHOLDERS).isJsonArray(), is(true));


		// check our holder list (this test)
		JsonArray holders = config.get(MITREidDataService.AUTHENTICATIONHOLDERS).getAsJsonArray();

		assertThat(holders.size(), is(2));
		// check for both of our clients in turn
		Set<AuthenticationHolderEntity> checked = new HashSet<>();
		for (JsonElement e : holders) {
			assertThat(e.isJsonObject(), is(true));
			JsonObject holder = e.getAsJsonObject();

			AuthenticationHolderEntity compare = null;
			if (holder.get("id").getAsLong() == holder1.getId()) {
				compare = holder1;
			} else if (holder.get("id").getAsLong() == holder2.getId()) {
				compare = holder2;
			}

			if (compare == null) {
				fail("Could not find matching authentication holder id: " + holder.get("id").getAsString());
			} else {
				assertTrue(holder.get("clientId").getAsString().equals(compare.getClientId()));
				assertTrue(holder.get("approved").getAsBoolean() == compare.isApproved());
				assertTrue(holder.get("redirectUri").getAsString().equals(compare.getRedirectUri()));
				if (compare.getUserAuth() != null) {
					assertTrue(holder.get("savedUserAuthentication").isJsonObject());
					JsonObject savedAuth = holder.get("savedUserAuthentication").getAsJsonObject();
					assertTrue(savedAuth.get("name").getAsString().equals(compare.getUserAuth().getName()));
					assertTrue(savedAuth.get("authenticated").getAsBoolean() == compare.getUserAuth().isAuthenticated());
					assertTrue(savedAuth.get("sourceClass").getAsString().equals(compare.getUserAuth().getSourceClass()));
				}
				checked.add(compare);
			}
		}
		// make sure all of our clients were found
		assertThat(checked.containsAll(allAuthHolders), is(true));
	}