From 25512b4818818d1bb036d02acff5d8b046292325 Mon Sep 17 00:00:00 2001 From: Joshua Casey Date: Fri, 22 Mar 2019 16:56:11 -0500 Subject: [PATCH] Test Refactor (ClientAdminBootstrapTests) - Apply IntelliJ sanitizations and reformat [#164842699] --- .../uaa/client/ClientAdminBootstrapTests.java | 119 ++++++------------ 1 file changed, 39 insertions(+), 80 deletions(-) diff --git a/server/src/test/java/org/cloudfoundry/identity/uaa/client/ClientAdminBootstrapTests.java b/server/src/test/java/org/cloudfoundry/identity/uaa/client/ClientAdminBootstrapTests.java index ab630b71843..0109e932d25 100644 --- a/server/src/test/java/org/cloudfoundry/identity/uaa/client/ClientAdminBootstrapTests.java +++ b/server/src/test/java/org/cloudfoundry/identity/uaa/client/ClientAdminBootstrapTests.java @@ -1,16 +1,3 @@ -/******************************************************************************* - * Cloud Foundry - * Copyright (c) [2009-2016] Pivotal Software, Inc. All Rights Reserved. - * - * This product is licensed to you under the Apache License, Version 2.0 (the "License"). - * You may not use this product except in compliance with the License. - * - * This product includes a number of subcomponents with - * separate copyright notices and license terms. Your use of these - * subcomponents is subject to the terms and conditions of the - * subcomponent's license, as noted in the LICENSE file. - *******************************************************************************/ - package org.cloudfoundry.identity.uaa.client; import org.cloudfoundry.identity.uaa.audit.event.EntityDeletedEvent; @@ -38,39 +25,16 @@ import org.springframework.util.StringUtils; import org.yaml.snakeyaml.Yaml; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; +import java.util.*; import static java.util.Collections.singletonList; -import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_AUTHORIZATION_CODE; -import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_IMPLICIT; -import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_JWT_BEARER; -import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_REFRESH_TOKEN; -import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_SAML2_BEARER; -import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_USER_TOKEN; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.*; +import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.*; import static org.mockito.Mockito.same; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; public class ClientAdminBootstrapTests extends JdbcTestBase { @@ -100,14 +64,13 @@ public void testSimpleAddClient() throws Exception { testSimpleAddClient("foo"); } - public ClientDetails testSimpleAddClient(String clientId) throws Exception { + private void testSimpleAddClient(String clientId) throws Exception { Map map = createClientMap(clientId); ClientDetails created = doSimpleTest(map); assertSet((String) map.get("redirect-uri"), null, created.getRegisteredRedirectUri(), String.class); - return created; } - public Map createClientMap(String clientId) { + private Map createClientMap(String clientId) { Map map = new HashMap<>(); map.put("id", clientId); map.put("secret", "bar"); @@ -120,11 +83,11 @@ public Map createClientMap(String clientId) { @Test public void client_slated_for_deletion_does_not_get_inserted() throws Exception { - String autoApproveId = "autoapprove-"+new RandomValueStringGenerator().generate().toLowerCase(); + String autoApproveId = "autoapprove-" + new RandomValueStringGenerator().generate().toLowerCase(); testSimpleAddClient(autoApproveId); reset(clientRegistrationService); bootstrap = spy(bootstrap); - String clientId = "client-"+new RandomValueStringGenerator().generate().toLowerCase(); + String clientId = "client-" + new RandomValueStringGenerator().generate().toLowerCase(); Map> clients = Collections.singletonMap(clientId, createClientMap(clientId)); bootstrap.setClients(clients); bootstrap.setAutoApproveClients(singletonList(autoApproveId)); @@ -138,38 +101,34 @@ public void client_slated_for_deletion_does_not_get_inserted() throws Exception @Test public void test_delete_from_yaml_existing_client() throws Exception { bootstrap = spy(bootstrap); - String clientId = "client-"+new RandomValueStringGenerator().generate().toLowerCase(); + String clientId = "client-" + new RandomValueStringGenerator().generate().toLowerCase(); testSimpleAddClient(clientId); verify(bootstrap, never()).publish(any()); - bootstrap.setClientsToDelete(Arrays.asList(clientId)); + bootstrap.setClientsToDelete(Collections.singletonList(clientId)); bootstrap.onApplicationEvent(new ContextRefreshedEvent(mock(ApplicationContext.class))); ArgumentCaptor captor = ArgumentCaptor.forClass(EntityDeletedEvent.class); verify(bootstrap, times(1)).publish(captor.capture()); assertNotNull(captor.getValue()); verify(publisher, times(1)).publishEvent(same(captor.getValue())); assertEquals(clientId, captor.getValue().getObjectId()); - assertEquals(clientId, ((ClientDetails)captor.getValue().getDeleted()).getClientId()); + assertEquals(clientId, ((ClientDetails) captor.getValue().getDeleted()).getClientId()); assertSame(SystemAuthentication.SYSTEM_AUTHENTICATION, captor.getValue().getAuthentication()); assertNotNull(captor.getValue().getAuditEvent()); } @Test - public void test_delete_from_yaml_non_existing_client() throws Exception { + public void test_delete_from_yaml_non_existing_client() { ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class); bootstrap = spy(bootstrap); bootstrap.setApplicationEventPublisher(publisher); - String clientId = "client-"+new RandomValueStringGenerator().generate().toLowerCase(); + String clientId = "client-" + new RandomValueStringGenerator().generate().toLowerCase(); verify(bootstrap, never()).publish(any()); - bootstrap.setClientsToDelete(Arrays.asList(clientId)); + bootstrap.setClientsToDelete(Collections.singletonList(clientId)); bootstrap.onApplicationEvent(new ContextRefreshedEvent(mock(ApplicationContext.class))); verify(bootstrap, never()).publish(any()); verify(publisher, never()).publishEvent(any()); } - public Integer countClients(String clientId) { - return jdbcTemplate.queryForObject("SELECT count(*) FROM oauth_client_details WHERE client_id = ? AND identity_zone_id = ?", Integer.class, clientId, IdentityZoneHolder.get().getId()); - } - @Test(expected = InvalidClientDetailsException.class) public void no_registered_redirect_url_for_auth_code() throws Exception { Map map = new HashMap<>(); @@ -228,7 +187,7 @@ public void clientMetadata_getsBootstrapped() throws Exception { map.put("app-launch-url", "http://takemetothispage.com"); map.put("app-icon", "bAsE64encODEd/iMAgE="); map.put("redirect-uri", "http://localhost/callback"); - map.put("authorized-grant-types","client_credentials"); + map.put("authorized-grant-types", "client_credentials"); bootstrap.setClients(Collections.singletonMap((String) map.get("id"), map)); bootstrap.afterPropertiesSet(); @@ -309,13 +268,13 @@ public void testOverrideClient() throws Exception { when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata()); doThrow(new ClientAlreadyExistsException("Planned")) - .when(clientRegistrationService).addClientDetails(any(ClientDetails.class), anyString()); + .when(clientRegistrationService).addClientDetails(any(ClientDetails.class), anyString()); bootstrap.afterPropertiesSet(); verify(clientRegistrationService, times(1)).addClientDetails(any(ClientDetails.class), anyString()); ArgumentCaptor captor = ArgumentCaptor.forClass(ClientDetails.class); verify(clientRegistrationService, times(1)).updateClientDetails(captor.capture(), anyString()); verify(clientRegistrationService, times(1)).updateClientSecret("foo", "bar", IdentityZoneHolder.get().getId()); - assertEquals(new HashSet(Arrays.asList("client_credentials")), captor.getValue().getAuthorizedGrantTypes()); + assertEquals(new HashSet(Collections.singletonList("client_credentials")), captor.getValue().getAuthorizedGrantTypes()); } @Test @@ -337,13 +296,13 @@ public void testOverrideClientWithEmptySecret() throws Exception { when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata()); doThrow(new ClientAlreadyExistsException("Planned")) - .when(clientRegistrationService).addClientDetails(any(ClientDetails.class), anyString()); + .when(clientRegistrationService).addClientDetails(any(ClientDetails.class), anyString()); bootstrap.afterPropertiesSet(); verify(clientRegistrationService, times(1)).addClientDetails(any(ClientDetails.class), anyString()); ArgumentCaptor captor = ArgumentCaptor.forClass(ClientDetails.class); verify(clientRegistrationService, times(1)).updateClientDetails(captor.capture(), anyString()); verify(clientRegistrationService, times(1)).updateClientSecret("foo", "", IdentityZoneHolder.get().getId()); - assertEquals(new HashSet(Arrays.asList("client_credentials")), captor.getValue().getAuthorizedGrantTypes()); + assertEquals(new HashSet(Collections.singletonList("client_credentials")), captor.getValue().getAuthorizedGrantTypes()); } @Test @@ -358,14 +317,14 @@ public void testOverrideClientByDefault() throws Exception { Map map = new HashMap<>(); map.put("secret", "bar"); map.put("redirect-uri", "http://localhost/callback"); - map.put("authorized-grant-types","client_credentials"); + map.put("authorized-grant-types", "client_credentials"); bootstrap.setClients(Collections.singletonMap("foo", map)); when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata()); doThrow(new ClientAlreadyExistsException("Planned")).when(clientRegistrationService) - .addClientDetails( - any(ClientDetails.class), - anyString() - ); + .addClientDetails( + any(ClientDetails.class), + anyString() + ); bootstrap.afterPropertiesSet(); verify(clientRegistrationService, times(1)).addClientDetails(any(ClientDetails.class), anyString()); verify(clientRegistrationService, times(1)).updateClientDetails(any(ClientDetails.class), anyString()); @@ -380,12 +339,12 @@ public void testOverrideClientWithYaml() throws Exception { @SuppressWarnings("rawtypes") Map fooBeforeClient = new Yaml().loadAs("id: foo\noverride: true\nsecret: somevalue\n" - + "access-token-validity: 100\nredirect-uri: http://localhost/callback\n" - + "authorized-grant-types: client_credentials", Map.class); + + "access-token-validity: 100\nredirect-uri: http://localhost/callback\n" + + "authorized-grant-types: client_credentials", Map.class); @SuppressWarnings("rawtypes") Map barBeforeClient = new Yaml().loadAs("id: bar\noverride: true\nsecret: somevalue\n" - + "access-token-validity: 100\nredirect-uri: http://localhost/callback\n" - + "authorized-grant-types: client_credentials", Map.class); + + "access-token-validity: 100\nredirect-uri: http://localhost/callback\n" + + "authorized-grant-types: client_credentials", Map.class); @SuppressWarnings("rawtypes") Map clients = new HashMap(); clients.put("foo", fooBeforeClient); @@ -394,9 +353,9 @@ public void testOverrideClientWithYaml() throws Exception { bootstrap.afterPropertiesSet(); Map fooUpdateClient = new HashMap(fooBeforeClient); - fooUpdateClient.put("secret","bar"); + fooUpdateClient.put("secret", "bar"); Map barUpdateClient = new HashMap(fooBeforeClient); - barUpdateClient.put("secret","bar"); + barUpdateClient.put("secret", "bar"); clients = new HashMap(); clients.put("foo", fooUpdateClient); clients.put("bar", barUpdateClient); @@ -405,7 +364,7 @@ public void testOverrideClientWithYaml() throws Exception { reset(clientRegistrationService); when(clientMetadataProvisioning.update(any(ClientMetadata.class), anyString())).thenReturn(new ClientMetadata()); doThrow(new ClientAlreadyExistsException("Planned")).when(clientRegistrationService).addClientDetails( - any(ClientDetails.class), anyString()); + any(ClientDetails.class), anyString()); bootstrap.afterPropertiesSet(); verify(clientRegistrationService, times(2)).addClientDetails(any(ClientDetails.class), anyString()); verify(clientRegistrationService, times(2)).updateClientDetails(any(ClientDetails.class), anyString()); @@ -463,11 +422,11 @@ private ClientDetails doSimpleTest(Map map) throws Exception { ClientDetails created = clientRegistrationService.loadClientByClientId((String) map.get("id")); assertNotNull(created); assertSet((String) map.get("scope"), Collections.singleton("uaa.none"), created.getScope(), String.class); - assertSet((String) map.get("resource-ids"), new HashSet(Arrays.asList("none")), created.getResourceIds(), String.class); + assertSet((String) map.get("resource-ids"), new HashSet(Collections.singletonList("none")), created.getResourceIds(), String.class); String authTypes = (String) map.get("authorized-grant-types"); - if (authTypes!=null && authTypes.contains(GRANT_TYPE_AUTHORIZATION_CODE)) { - authTypes+=",refresh_token"; + if (authTypes != null && authTypes.contains(GRANT_TYPE_AUTHORIZATION_CODE)) { + authTypes += ",refresh_token"; } assertSet(authTypes, Collections.emptySet(), created.getAuthorizedGrantTypes(), String.class); @@ -485,9 +444,9 @@ private ClientDetails doSimpleTest(Map map) throws Exception { "refresh-token-validity")) { info.remove(key); } - for (Map.Entry entry : info.entrySet()) { - assertTrue("Client should contain additional information key:"+ entry.getKey(), created.getAdditionalInformation().containsKey(entry.getKey())); - if (entry.getValue()!=null) { + for (Map.Entry entry : info.entrySet()) { + assertTrue("Client should contain additional information key:" + entry.getKey(), created.getAdditionalInformation().containsKey(entry.getKey())); + if (entry.getValue() != null) { assertEquals(entry.getValue(), created.getAdditionalInformation().get(entry.getKey())); } } @@ -497,8 +456,8 @@ private ClientDetails doSimpleTest(Map map) throws Exception { } private void assertSet(String expectedValue, Collection defaultValueIfNull, Collection actualValue, Class type) { - Collection assertScopes = defaultValueIfNull; - if (expectedValue!=null) { + Collection assertScopes = defaultValueIfNull; + if (expectedValue != null) { if (String.class.equals(type)) { assertScopes = StringUtils.commaDelimitedListToSet(expectedValue); } else {