Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Reduce random data in server tests #6602

Merged
merged 6 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/test/java/de/tum/in/www1/artemis/LtiIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.time.temporal.ChronoUnit;
import java.util.Set;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -139,8 +138,8 @@
jiraRequestMockProvider.enableMockingOfRequests();
}

private String generateRandomEmail() {
return RandomStringUtils.random(12, true, true) + "@email.com";
private String generateEmail(String username) {
return username + "@email.com";
}

private String replaceEmail(String requestBody, String email) {
Expand All @@ -163,7 +162,7 @@
@ValueSource(strings = { EDX_REQUEST_BODY, MOODLE_REQUEST_BODY })
@WithAnonymousUser
void launchAsAnonymousUser_noOnlineCourseConfigurationException(String requestBody) throws Exception {
requestBody = replaceEmail(requestBody, generateRandomEmail());
requestBody = replaceEmail(requestBody, generateEmail("launchAsAnonymousUser_noOnlineCourseConfigurationException"));

Check warning on line 165 in src/test/java/de/tum/in/www1/artemis/LtiIntegrationTest.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/java/de/tum/in/www1/artemis/LtiIntegrationTest.java#L165

Avoid reassigning parameters such as 'requestBody'

course.setOnlineCourseConfiguration(null);
courseRepository.save(course);
Expand All @@ -176,7 +175,7 @@
@ValueSource(strings = { EDX_REQUEST_BODY }) // To be readded when LtiUserId is removed, MOODLE_REQUEST_BODY })
@WithAnonymousUser
void launchAsAnonymousUser_WithoutExistingEmail(String requestBody) throws Exception {
String email = generateRandomEmail();
String email = generateEmail("launchAsAnonymousUser_WithoutExistingEmail");
requestBody = replaceEmail(requestBody, email);
addJiraMocks(email, null);

Expand All @@ -193,7 +192,7 @@
@ValueSource(strings = { EDX_REQUEST_BODY, MOODLE_REQUEST_BODY })
@WithAnonymousUser
void launchAsAnonymousUser_WithExistingEmail(String requestBody) throws Exception {
String email = generateRandomEmail();
String email = generateEmail("launchAsAnonymousUser_WithExistingEmail");
requestBody = replaceEmail(requestBody, email);
addJiraMocks(email, TEST_PREFIX + "student1");

Expand All @@ -210,7 +209,7 @@
@ValueSource(strings = { EDX_REQUEST_BODY }) // To be readded when LtiUserId is removed, MOODLE_REQUEST_BODY })
@WithAnonymousUser
void launchAsAnonymousUser_RequireExistingUser(String requestBody) throws Exception {
String email = generateRandomEmail();
String email = generateEmail("launchAsAnonymousUser_RequireExistingUser");
requestBody = replaceEmail(requestBody, email);

course = courseRepository.findByIdWithEagerOnlineCourseConfigurationElseThrow(course.getId());
Expand All @@ -228,7 +227,7 @@
@ValueSource(strings = { EDX_REQUEST_BODY, MOODLE_REQUEST_BODY })
@WithAnonymousUser
void launchAsAnonymousUser_checkExceptions(String requestBody) throws Exception {
requestBody = replaceEmail(requestBody, generateRandomEmail());
requestBody = replaceEmail(requestBody, generateEmail("launchAsAnonymousUser_checkExceptions"));

Check warning on line 230 in src/test/java/de/tum/in/www1/artemis/LtiIntegrationTest.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/java/de/tum/in/www1/artemis/LtiIntegrationTest.java#L230

Avoid reassigning parameters such as 'requestBody'

request.postWithoutLocation("/api/lti/launch/" + programmingExercise.getId() + 1, requestBody.getBytes(), HttpStatus.NOT_FOUND, new HttpHeaders(),
MediaType.APPLICATION_FORM_URLENCODED_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void testAddUserToOrganization() throws Exception {

Organization organization = database.createOrganization();
organization = organizationRepo.save(organization);
User student = database.createAndSaveUser(UUID.randomUUID().toString().replace("-", ""));
User student = database.createAndSaveUser(TEST_PREFIX + "testAddUserToOrganization");

request.postWithoutLocation("/api/admin/organizations/" + organization.getId() + "/users/" + student.getLogin(), null, HttpStatus.OK, null);
Organization updatedOrganization = request.get("/api/admin/organizations/" + organization.getId() + "/full", HttpStatus.OK, Organization.class);
Expand All @@ -198,11 +198,11 @@ void testAddUserToOrganization() throws Exception {
*/
@Test
@WithMockUser(username = "admin", roles = "ADMIN")
void testRemoveUserToOrganization() throws Exception {
void testRemoveUserFromOrganization() throws Exception {
jiraRequestMockProvider.enableMockingOfRequests();

Organization organization = database.createOrganization();
User student = database.createAndSaveUser(UUID.randomUUID().toString().replace("-", ""));
User student = database.createAndSaveUser(TEST_PREFIX + "testRemoveUser_");

organization.getUsers().add(student);
organization = organizationRepo.save(organization);
Expand Down Expand Up @@ -268,7 +268,7 @@ void testUpdateOrganization_idInBodyNull() throws Exception {
}

/**
* Test updating an existing organization when the Id in the path doesn't match the one in the Body
* Test updating an existing organization when the id in the path doesn't match the one in the Body
*/
@Test
@WithMockUser(username = "admin", roles = "ADMIN")
Expand All @@ -279,9 +279,9 @@ void testUpdateOrganization_IdInPathWrong() throws Exception {
organizationRepo.save(organization);
String initialName = organization.getName();
organization.setName("UpdatedName");
long randomId = 1337420;
long wrongId = 1337420;

Organization updatedOrganization = request.putWithResponseBody("/api/admin/organizations/" + randomId, organization, Organization.class, HttpStatus.BAD_REQUEST);
Organization updatedOrganization = request.putWithResponseBody("/api/admin/organizations/" + wrongId, organization, Organization.class, HttpStatus.BAD_REQUEST);
organization.setName(initialName);
assertThat(updatedOrganization).isNull();
}
Expand Down Expand Up @@ -332,7 +332,7 @@ void testGetNumberOfUsersAndCoursesOfAllOrganizations() throws Exception {
organization = organizationRepo.save(organization);

courseRepo.addOrganizationToCourse(course1.getId(), organization);
User student = database.createAndSaveUser(UUID.randomUUID().toString().replace("-", ""));
User student = database.createAndSaveUser(TEST_PREFIX + "testGetNumberOfUsersOfAll_");

userRepo.addOrganizationToUser(student.getId(), organization);

Expand All @@ -357,7 +357,7 @@ void testGetNumberOfUsersAndCoursesOfOrganization() throws Exception {
organization = organizationRepo.save(organization);

courseRepo.addOrganizationToCourse(course1.getId(), organization);
User student = database.createAndSaveUser(UUID.randomUUID().toString().replace("-", ""));
User student = database.createAndSaveUser(TEST_PREFIX + "testGetNumberOfUsers_");

userRepo.addOrganizationToUser(student.getId(), organization);

Expand All @@ -382,7 +382,7 @@ void testGetOrganizationById() throws Exception {
course1 = courseRepo.save(course1);
courseRepo.addOrganizationToCourse(course1.getId(), organization);

User student = database.createAndSaveUser(UUID.randomUUID().toString().replace("-", ""));
User student = database.createAndSaveUser(TEST_PREFIX + "testGetOrganizationById");

userRepo.addOrganizationToUser(student.getId(), organization);
// invoked remove to make sure it works correctly
Expand Down Expand Up @@ -430,7 +430,7 @@ void testGetAllOrganizationByUser() throws Exception {

Organization organization = database.createOrganization();
organization = organizationRepo.save(organization);
User student = database.createAndSaveUser(UUID.randomUUID().toString().replace("-", ""));
User student = database.createAndSaveUser(TEST_PREFIX + "testGetAllOrganizationByUser");

userRepo.addOrganizationToUser(student.getId(), organization);

Expand All @@ -448,7 +448,7 @@ void testOrganizationIndexing() throws Exception {
jiraRequestMockProvider.enableMockingOfRequests();

Organization organization = database.createOrganization();
User student = database.createAndSaveUser(UUID.randomUUID().toString().replace("-", ""));
User student = database.createAndSaveUser(TEST_PREFIX + "testOrganizationIndexing");

organization.getUsers().add(student);
organization = organizationRepo.save(organization);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ private Exercise createTextExerciseForTeam() {

private Team createTeamForExercise(User student, Exercise exercise) {
var team = new Team();
team.setShortName("t" + UUID.randomUUID().toString().substring(0, 3));
team.setShortName(TEST_PREFIX + "createTeamForExercise");
team.addStudents(student);
team.setExercise(exercise);
return teamRepository.save(team);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -664,7 +663,7 @@ void testGetAllExercisesOnPageAsEditorSuccess() throws Exception {
final Course course = database.addEmptyCourse();
final var now = ZonedDateTime.now();
FileUploadExercise exercise = ModelFactory.generateFileUploadExercise(now.minusDays(1), now.minusHours(2), now.minusHours(1), "pdf", course);
String title = "LoremIpsum" + UUID.randomUUID();
String title = TEST_PREFIX + "testGetAllExercisesOnPageAsEditorSuccess";
exercise.setTitle(title);
exercise = fileUploadExerciseRepository.save(exercise);
final var searchTerm = database.configureSearch(exercise.getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private ProgrammingExerciseSolutionEntry addSolutionEntryToTestCase(ProgrammingE
var solutionEntry = new ProgrammingExerciseSolutionEntry();
solutionEntry.setTestCase(testCase);
solutionEntry.setLine(1);
solutionEntry.setCode(UUID.randomUUID().toString());
solutionEntry.setCode("code");
return solutionEntryRepository.save(solutionEntry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -171,7 +170,7 @@ void setupTestScenario() {
private void createLearningGoal() {
Course course = courseRepository.findWithEagerLearningGoalsById(idOfCourse).get();
LearningGoal learningGoal = new LearningGoal();
learningGoal.setTitle("LearningGoal" + new Random().nextInt());
learningGoal.setTitle("LearningGoal" + idOfCourse);
learningGoal.setDescription("This is an example learning goal");
learningGoal.setTaxonomy(LearningGoalTaxonomy.UNDERSTAND);
learningGoal.setCourse(course);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void initTestCase() throws Exception {
List<Course> courses = this.database.createCoursesWithExercisesAndLectures(TEST_PREFIX, true, true, numberOfTutors);
this.course1 = this.courseRepository.findByIdWithExercisesAndLecturesElseThrow(courses.get(0).getId());
var lecture = this.course1.getLectures().stream().findFirst().get();
lecture.setTitle("Lecture " + new Random().nextInt()); // needed for search by title
lecture.setTitle("Lecture " + lecture.getId()); // needed for search by title
this.lecture1 = lectureRepository.save(lecture);
this.textExercise = textExerciseRepository.findByCourseIdWithCategories(course1.getId()).stream().findFirst().get();
// Add users that are not in the course
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Set;

Expand Down Expand Up @@ -170,10 +169,6 @@ Course setCourseInformationSharingConfiguration(CourseInformationSharingConfigur
return persistedCourse;
}

ChannelDTO createChannel(boolean isPublicChannel) throws Exception {
return createChannel(isPublicChannel, RandomConversationNameGenerator.generateRandomConversationName());
}

ChannelDTO createChannel(boolean isPublicChannel, String name) throws Exception {
var channelDTO = new ChannelDTO();
channelDTO.setName(name);
Expand Down Expand Up @@ -278,23 +273,4 @@ void resetWebsocketMock() {
reset(this.messagingTemplate);
}

public static class RandomConversationNameGenerator {

private static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";

private static final String NUMBERS = "0123456789";

private static final String ALL_CHARS = LOWERCASE_LETTERS + NUMBERS;

private static final SecureRandom RANDOM = new SecureRandom();

public static String generateRandomConversationName() {
var stringBuilder = new StringBuilder();
for (int i = 0; i < 10; i++) {
stringBuilder.append(ALL_CHARS.charAt(RANDOM.nextInt(ALL_CHARS.length())));
}
return stringBuilder.toString();
}
}

}
Loading
Loading