Skip to content

Commit

Permalink
Fixed spotlessJavaCheck errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hrcornejo committed Sep 26, 2023
1 parent 2a67204 commit 47f5774
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = "[email protected]";
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<String, String> 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 = "[email protected]";
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<String, String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> projects = ocClient.projects();
Set<String> 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();
}
}

0 comments on commit 47f5774

Please sign in to comment.