Skip to content

Commit

Permalink
Merge 5cab004 into f350139
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed Apr 25, 2018
2 parents f350139 + 5cab004 commit e1f76f7
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</parent>

<properties>

<start-class>edu.tamu.app.ProjectApplication</start-class>
</properties>

<packaging>war</packaging>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/edu/tamu/app/ProjectInitialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import edu.tamu.app.service.registry.ManagementBeanRegistry;

@Component
@Profile("!test")
public class ProjectInitialization implements CommandLineRunner {

@Autowired
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ security.basic.enabled: false

spring.template.cache: false

spring.profiles: production
spring.profiles.active: production

spring.datasource.username: spring
spring.datasource.password: spring

Expand Down
33 changes: 33 additions & 0 deletions src/test/java/edu/tamu/app/auth/model/AppUserDetailsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package edu.tamu.app.auth.model;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import edu.tamu.app.enums.Role;
import edu.tamu.app.model.User;
import edu.tamu.weaver.auth.model.Credentials;

@RunWith(SpringRunner.class)
public class AppUserDetailsTest {

private static final Credentials TEST_CREDENTIALS = new Credentials();
static {
TEST_CREDENTIALS.setUin("123456789");
TEST_CREDENTIALS.setEmail("aggieJack@tamu.edu");
TEST_CREDENTIALS.setFirstName("Aggie");
TEST_CREDENTIALS.setLastName("Jack");
TEST_CREDENTIALS.setRole("ROLE_USER");
}

private User testUser = new User(TEST_CREDENTIALS.getUin(), TEST_CREDENTIALS.getEmail(), TEST_CREDENTIALS.getFirstName(), TEST_CREDENTIALS.getLastName(), Role.valueOf(TEST_CREDENTIALS.getRole()));

@Test
public void testConstructor() {
AppUserDetails appUser = new AppUserDetails(testUser);
assertEquals("The parent constructor was not called correctly", testUser.getId(), appUser.getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package edu.tamu.app.auth.service;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.util.ReflectionTestUtils.setField;

import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import edu.tamu.app.enums.Role;
import edu.tamu.app.model.User;
import edu.tamu.app.model.repo.UserRepo;
import edu.tamu.weaver.auth.model.Credentials;

@RunWith(SpringRunner.class)
public class AppUserCredentialsServiceTest {

private static final Credentials TEST_CREDENTIALS_1 = new Credentials();
static {
TEST_CREDENTIALS_1.setUin("123456789");
TEST_CREDENTIALS_1.setEmail("aggieJack@tamu.edu");
TEST_CREDENTIALS_1.setFirstName("Aggie");
TEST_CREDENTIALS_1.setLastName("Jack");
TEST_CREDENTIALS_1.setRole("ROLE_USER");
}

private static final Credentials TEST_CREDENTIALS_2 = new Credentials();
static {
TEST_CREDENTIALS_2.setUin("987654321");
TEST_CREDENTIALS_2.setEmail("aggieJack@tamu.edu");
TEST_CREDENTIALS_2.setFirstName("Aggie");
TEST_CREDENTIALS_2.setLastName("Jack");
TEST_CREDENTIALS_2.setRole("ROLE_USER");
}

private static final Credentials TEST_NULL_CREDENTIALS = new Credentials();
static {
TEST_NULL_CREDENTIALS.setUin("987654321");
TEST_NULL_CREDENTIALS.setEmail("aggieJack@tamu.edu");
TEST_NULL_CREDENTIALS.setFirstName("Aggie");
TEST_NULL_CREDENTIALS.setLastName("Jack");
}

private static final Credentials TEST_CHANGED_CREDENTIALS = new Credentials();
static {
TEST_CHANGED_CREDENTIALS.setUin("111111111");
TEST_CHANGED_CREDENTIALS.setEmail("jsmithk@tamu.edu");
TEST_CHANGED_CREDENTIALS.setFirstName("John");
TEST_CHANGED_CREDENTIALS.setLastName("Smith");
TEST_CHANGED_CREDENTIALS.setRole("ROLE_ADMIN");
}

private User testUser1 = new User(TEST_CREDENTIALS_1.getUin(), TEST_CREDENTIALS_1.getEmail(), TEST_CREDENTIALS_1.getFirstName(), TEST_CREDENTIALS_1.getLastName(), Role.valueOf(TEST_CREDENTIALS_1.getRole()));
private User testUser2 = new User(TEST_CREDENTIALS_2.getUin(), TEST_CREDENTIALS_2.getEmail(), TEST_CREDENTIALS_2.getFirstName(), TEST_CREDENTIALS_2.getLastName(), Role.valueOf(TEST_CREDENTIALS_2.getRole()));

private static final String[] testAdmins = { "123456789", "987654321" };

private Optional<User> optionalUser1 = Optional.of(testUser1);

@Mock
private UserRepo userRepo;

@InjectMocks
private AppUserCredentialsService credentialsService;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(userRepo.findByUsername(TEST_CREDENTIALS_1.getUin())).thenReturn(optionalUser1);
when(userRepo.findByUsername(TEST_CREDENTIALS_2.getUin())).thenReturn(Optional.empty());
when(userRepo.findByUsername(TEST_CHANGED_CREDENTIALS.getUin())).thenReturn(optionalUser1);
when(userRepo.create(any(String.class), any(String.class), any(String.class), any(String.class), any(Role.class))).thenReturn(testUser2);
when(userRepo.save(any(User.class))).thenReturn(testUser1);
}

@Test
public void testUpdateUserByCredentials() {
setField(credentialsService, "admins", testAdmins);
User foundUser = credentialsService.updateUserByCredentials(TEST_CREDENTIALS_1);
assertEquals("Unable to find user", testUser1, foundUser);
User unfoundUser = credentialsService.updateUserByCredentials(TEST_CREDENTIALS_2);
assertEquals("Unable to find user", testUser2, unfoundUser);
}

@Test
public void testGetAnonymousRole() {
String anonRole = credentialsService.getAnonymousRole();
assertEquals("Anonymous Role not set correctly", Role.ROLE_ANONYMOUS.toString(), anonRole);
}

@Test
public void testNullRole() {
setField(credentialsService, "admins", testAdmins);
User nullUser = credentialsService.updateUserByCredentials(TEST_NULL_CREDENTIALS);
assertEquals("Null Role not updated", TEST_CREDENTIALS_1.getRole(), nullUser.getRole().toString());
}

@Test
public void testChangedUser() {
User changedUser = credentialsService.updateUserByCredentials(TEST_CHANGED_CREDENTIALS);
assertEquals("is present", changedUser, optionalUser1.get());
assertEquals("Username was not updated", TEST_CHANGED_CREDENTIALS.getUin(), changedUser.getUsername());
assertEquals("Email was not updated", TEST_CHANGED_CREDENTIALS.getEmail(), changedUser.getEmail());
assertEquals("First name was not updated", TEST_CHANGED_CREDENTIALS.getFirstName(), changedUser.getFirstName());
assertEquals("Last name was not updated", TEST_CHANGED_CREDENTIALS.getLastName(), changedUser.getLastName());
assertEquals("Role was not updated", TEST_CHANGED_CREDENTIALS.getRole(), changedUser.getRole().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import edu.tamu.app.utility.JsonNodeUtility;
import edu.tamu.weaver.response.ApiResponse;

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
public class ProjectControllerTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import edu.tamu.app.model.repo.VersionManagementSoftwareRepo;
import edu.tamu.weaver.response.ApiResponse;

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
public class VersionManagementSoftwareControllerTest {

Expand Down
1 change: 0 additions & 1 deletion src/test/java/edu/tamu/app/model/ProjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import edu.tamu.app.model.repo.ProjectRepo;
import edu.tamu.app.model.repo.VersionManagementSoftwareRepo;

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ProjectApplication.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
public class ProjectTest {
Expand Down
1 change: 0 additions & 1 deletion src/test/java/edu/tamu/app/model/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import edu.tamu.app.model.repo.UserRepo;
import edu.tamu.weaver.auth.model.Credentials;

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ProjectApplication.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
public class UserTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import edu.tamu.app.ProjectApplication;
import edu.tamu.app.model.request.FeatureRequest;

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ProjectApplication.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
public class TemplateServiceTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import edu.tamu.app.ProjectApplication;
import edu.tamu.app.ProjectInitialization;
import edu.tamu.app.enums.ServiceType;
import edu.tamu.app.model.VersionManagementSoftware;
import edu.tamu.app.model.repo.ProjectRepo;
Expand All @@ -32,7 +34,6 @@
import edu.tamu.app.service.versioning.VersionOneService;
import edu.tamu.app.utility.JsonNodeUtility;

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ProjectApplication.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
public class VersionOneServiceTest {
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ security.basic.enabled: false

spring.template.cache: false

spring.profiles: test
spring.profiles.active:test

spring.datasource.username: spring
spring.datasource.password: spring

Expand Down

0 comments on commit e1f76f7

Please sign in to comment.