From 47f5774f395e6ab3dbaf92877fdde77b13a9eb43 Mon Sep 17 00:00:00 2001 From: "Rodriguez,Hector (IT EDP)" Date: Tue, 26 Sep 2023 12:03:30 +0200 Subject: [PATCH] Fixed spotlessJavaCheck errors --- .../config/AuthSecurityTestConfig.java | 70 +++--- .../CrowdProjectIdentityMgmtAdapterTest.java | 222 +++++++++--------- .../openshift/OpenshiftClientTest.java | 46 ++-- 3 files changed, 166 insertions(+), 172 deletions(-) diff --git a/src/test/java/org/opendevstack/provision/config/AuthSecurityTestConfig.java b/src/test/java/org/opendevstack/provision/config/AuthSecurityTestConfig.java index 85d5c9a0..ca02abd3 100644 --- a/src/test/java/org/opendevstack/provision/config/AuthSecurityTestConfig.java +++ b/src/test/java/org/opendevstack/provision/config/AuthSecurityTestConfig.java @@ -13,6 +13,10 @@ */ package org.opendevstack.provision.config; +import static java.util.Map.entry; +import static org.mockito.Mockito.mock; + +import java.util.Map; import org.opendevstack.provision.adapter.IODSAuthnzAdapter; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; @@ -21,43 +25,37 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; -import java.util.Map; - -import static java.util.Map.entry; -import static org.mockito.Mockito.mock; - @Configuration public class AuthSecurityTestConfig { - public static final String TEST_USER_USERNAME = "user"; - public static final String TEST_ADMIN_USERNAME = "admin"; - public static final String TEST_ADMIN_EMAIL = "admin@example.com"; - public static final String TEST_NOT_PERMISSIONED_USER_USERNAME = "not-permissioned-user"; - public static final String TEST_VALID_CREDENTIAL = "validsecret"; - - @Value("${idmanager.group.opendevstack-users}") - protected String roleUser; - - @Value("${idmanager.group.opendevstack-administrators}") - protected String roleAdmin; - - @Bean - public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } - - @Bean(name = "testUsersAndRoles") - public Map testUsersAndRoles() { - return Map.ofEntries( - entry(TEST_USER_USERNAME, roleUser), - entry(TEST_ADMIN_USERNAME, roleAdmin), - entry(TEST_NOT_PERMISSIONED_USER_USERNAME, "not-opendevstack-user")); - } - - @Bean - @Primary - public IODSAuthnzAdapter iodsAuthnzAdapter() { - return mock(IODSAuthnzAdapter.class); - } - + public static final String TEST_USER_USERNAME = "user"; + public static final String TEST_ADMIN_USERNAME = "admin"; + public static final String TEST_ADMIN_EMAIL = "admin@example.com"; + public static final String TEST_NOT_PERMISSIONED_USER_USERNAME = "not-permissioned-user"; + public static final String TEST_VALID_CREDENTIAL = "validsecret"; + + @Value("${idmanager.group.opendevstack-users}") + protected String roleUser; + + @Value("${idmanager.group.opendevstack-administrators}") + protected String roleAdmin; + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Bean(name = "testUsersAndRoles") + public Map testUsersAndRoles() { + return Map.ofEntries( + entry(TEST_USER_USERNAME, roleUser), + entry(TEST_ADMIN_USERNAME, roleAdmin), + entry(TEST_NOT_PERMISSIONED_USER_USERNAME, "not-opendevstack-user")); + } + + @Bean + @Primary + public IODSAuthnzAdapter iodsAuthnzAdapter() { + return mock(IODSAuthnzAdapter.class); + } } diff --git a/src/test/java/org/opendevstack/provision/services/CrowdProjectIdentityMgmtAdapterTest.java b/src/test/java/org/opendevstack/provision/services/CrowdProjectIdentityMgmtAdapterTest.java index 8b99e008..066fa1f3 100644 --- a/src/test/java/org/opendevstack/provision/services/CrowdProjectIdentityMgmtAdapterTest.java +++ b/src/test/java/org/opendevstack/provision/services/CrowdProjectIdentityMgmtAdapterTest.java @@ -14,6 +14,10 @@ package org.opendevstack.provision.services; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -23,121 +27,115 @@ import org.opendevstack.provision.adapter.exception.IdMgmtException; import org.opendevstack.provision.model.OpenProjectData; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) public class CrowdProjectIdentityMgmtAdapterTest { - @InjectMocks - private CrowdProjectIdentityMgmtAdapter idMgr; - - @Mock - private IODSAuthnzAdapter manager; - - @Test - public void testGroupExists() { - String group = "xxx"; - - when(manager.existsGroupWithName(eq(group))).thenReturn(true); - assertTrue(idMgr.groupExists(group)); - - when(manager.existsGroupWithName(eq(group))).thenReturn(false); - assertFalse(idMgr.groupExists(group)); + @InjectMocks private CrowdProjectIdentityMgmtAdapter idMgr; + + @Mock private IODSAuthnzAdapter manager; + + @Test + public void testGroupExists() { + String group = "xxx"; + + when(manager.existsGroupWithName(eq(group))).thenReturn(true); + assertTrue(idMgr.groupExists(group)); + + when(manager.existsGroupWithName(eq(group))).thenReturn(false); + assertFalse(idMgr.groupExists(group)); + } + + @Test + public void testUserExists() { + String principal = "user"; + + when(manager.existPrincipalWithName(principal)).thenReturn(true); + assertTrue(idMgr.userExists(principal)); + + when(manager.existPrincipalWithName(principal)).thenReturn(false); + assertFalse(idMgr.userExists(principal)); + } + + @Test + public void testCreateGroup() throws Exception { + String group = "xxx"; + + when(manager.addGroup(group)).thenReturn(group); + String groupInternal = idMgr.createGroupInternal(group); + assertEquals(group, groupInternal); + + assertEquals(group, idMgr.createAdminGroup(group)); + assertEquals(group, idMgr.createUserGroup(group)); + assertEquals(group, idMgr.createReadonlyGroup(group)); + } + + @Test + public void testCreateNullGroup() { + assertThrows(IdMgmtException.class, () -> idMgr.createGroupInternal(null)); + } + + @Test + public void testCreateGroupSOAPErr() { + assertThrows( + IdMgmtException.class, + () -> { + String group = "xxx"; + when(manager.addGroup(group)).thenThrow(IdMgmtException.class); + idMgr.createGroupInternal(group); + }); + } + + @Test + public void testValidateProject() throws Exception { + String principal = mockPrincipalExists("user", true); + String group = mockGroupExists("xxx", true); + + OpenProjectData data = new OpenProjectData(); + data.setProjectAdminGroup(group); + data.setProjectUserGroup(group); + data.setProjectReadonlyGroup(group); + data.setProjectAdminUser(principal); + + idMgr.validateIdSettingsOfProject(data); + + data.setProjectUserGroup("doesNotExistUG"); + data.setProjectAdminGroup("doesNotExistAD"); + data.setProjectReadonlyGroup("doesNotExistRO"); + + mockGroupExists(data.getProjectUserGroup(), false); + mockGroupExists(data.getProjectAdminGroup(), false); + mockGroupExists(data.getProjectReadonlyGroup(), false); + + Exception testE = null; + try { + idMgr.validateIdSettingsOfProject(data); + } catch (IdMgmtException idEx) { + testE = idEx; } - - @Test - public void testUserExists() { - String principal = "user"; - - when(manager.existPrincipalWithName(principal)).thenReturn(true); - assertTrue(idMgr.userExists(principal)); - - when(manager.existPrincipalWithName(principal)).thenReturn(false); - assertFalse(idMgr.userExists(principal)); - } - - @Test - public void testCreateGroup() throws Exception { - String group = "xxx"; - - when(manager.addGroup(group)).thenReturn(group); - String groupInternal = idMgr.createGroupInternal(group); - assertEquals(group, groupInternal); - - assertEquals(group, idMgr.createAdminGroup(group)); - assertEquals(group, idMgr.createUserGroup(group)); - assertEquals(group, idMgr.createReadonlyGroup(group)); - } - - @Test - public void testCreateNullGroup() { - assertThrows(IdMgmtException.class, () -> idMgr.createGroupInternal(null)); - } - - @Test - public void testCreateGroupSOAPErr() { - assertThrows( - IdMgmtException.class, - () -> { - String group = "xxx"; - when(manager.addGroup(group)).thenThrow(IdMgmtException.class); - idMgr.createGroupInternal(group); - }); - } - - @Test - public void testValidateProject() throws Exception { - String principal = mockPrincipalExists("user", true); - String group = mockGroupExists("xxx", true); - - OpenProjectData data = new OpenProjectData(); - data.setProjectAdminGroup(group); - data.setProjectUserGroup(group); - data.setProjectReadonlyGroup(group); - data.setProjectAdminUser(principal); - - idMgr.validateIdSettingsOfProject(data); - - data.setProjectUserGroup("doesNotExistUG"); - data.setProjectAdminGroup("doesNotExistAD"); - data.setProjectReadonlyGroup("doesNotExistRO"); - - mockGroupExists(data.getProjectUserGroup(), false); - mockGroupExists(data.getProjectAdminGroup(), false); - mockGroupExists(data.getProjectReadonlyGroup(), false); - - Exception testE = null; - try { - idMgr.validateIdSettingsOfProject(data); - } catch (IdMgmtException idEx) { - testE = idEx; - } - assertNotNull(testE); - assertTrue(testE.getMessage().contains(data.getProjectUserGroup())); - assertTrue(testE.getMessage().contains(data.getProjectAdminGroup())); - assertTrue(testE.getMessage().contains(data.getProjectReadonlyGroup())); - - mockPrincipalExists(principal, false); - testE = null; - try { - idMgr.validateIdSettingsOfProject(data); - } catch (IdMgmtException idEx) { - testE = idEx; - } - assertNotNull(testE); - assertTrue(testE.getMessage().contains(data.getProjectUserGroup())); - assertTrue(testE.getMessage().contains(data.getProjectAdminUser())); - } - - public String mockGroupExists(String groupName, boolean existsGroup) { - when(manager.existsGroupWithName(groupName)).thenReturn(existsGroup); - return groupName; - } - - public String mockPrincipalExists(String user, boolean exists) { - when(manager.existPrincipalWithName(user)).thenReturn(exists); - return user; + assertNotNull(testE); + assertTrue(testE.getMessage().contains(data.getProjectUserGroup())); + assertTrue(testE.getMessage().contains(data.getProjectAdminGroup())); + assertTrue(testE.getMessage().contains(data.getProjectReadonlyGroup())); + + mockPrincipalExists(principal, false); + testE = null; + try { + idMgr.validateIdSettingsOfProject(data); + } catch (IdMgmtException idEx) { + testE = idEx; } + assertNotNull(testE); + assertTrue(testE.getMessage().contains(data.getProjectUserGroup())); + assertTrue(testE.getMessage().contains(data.getProjectAdminUser())); + } + + public String mockGroupExists(String groupName, boolean existsGroup) { + when(manager.existsGroupWithName(groupName)).thenReturn(existsGroup); + return groupName; + } + + public String mockPrincipalExists(String user, boolean exists) { + when(manager.existPrincipalWithName(user)).thenReturn(exists); + return user; + } } diff --git a/src/test/java/org/opendevstack/provision/services/openshift/OpenshiftClientTest.java b/src/test/java/org/opendevstack/provision/services/openshift/OpenshiftClientTest.java index 4ed87935..d5a9595a 100644 --- a/src/test/java/org/opendevstack/provision/services/openshift/OpenshiftClientTest.java +++ b/src/test/java/org/opendevstack/provision/services/openshift/OpenshiftClientTest.java @@ -13,39 +13,37 @@ */ package org.opendevstack.provision.services.openshift; -import com.github.tomakehurst.wiremock.WireMockServer; -import org.junit.jupiter.api.Test; +import static com.github.tomakehurst.wiremock.client.WireMock.*; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static org.junit.Assert.assertEquals; +import com.github.tomakehurst.wiremock.WireMockServer; import java.io.IOException; import java.util.Objects; import java.util.Set; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; public class OpenshiftClientTest { - @Test - public void testOpenshiftClientReturnsProjectKeys() throws IOException { - - byte[] jsonContent = + @Test + public void testOpenshiftClientReturnsProjectKeys() throws IOException { - Objects.requireNonNull(getClass().getResourceAsStream("/openshift/openshift-projects.json")) - .readAllBytes(); - WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); - wireMockServer.start(); - wireMockServer.stubFor( - get("/apis/project.openshift.io/v1/projects") - .willReturn(aResponse().withBody(jsonContent))); - OpenshiftClient ocClient = new OpenshiftClient(wireMockServer.baseUrl()); + byte[] jsonContent = + Objects.requireNonNull(getClass().getResourceAsStream("/openshift/openshift-projects.json")) + .readAllBytes(); + WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); + wireMockServer.start(); + wireMockServer.stubFor( + get("/apis/project.openshift.io/v1/projects") + .willReturn(aResponse().withBody(jsonContent))); + OpenshiftClient ocClient = new OpenshiftClient(wireMockServer.baseUrl()); - Set projects = ocClient.projects(); + Set projects = ocClient.projects(); - assertEquals("testproject-dev", projects.iterator().next()); + assertEquals("testproject-dev", projects.iterator().next()); - wireMockServer.verify( - exactly(1), getRequestedFor(urlEqualTo("/apis/project.openshift.io/v1/projects"))); - wireMockServer.stop(); - } + wireMockServer.verify( + exactly(1), getRequestedFor(urlEqualTo("/apis/project.openshift.io/v1/projects"))); + wireMockServer.stop(); + } }