diff --git a/.github/workflows/android-build.yml b/.github/workflows/android-build.yml index fd5b11f6..c153f4dc 100644 --- a/.github/workflows/android-build.yml +++ b/.github/workflows/android-build.yml @@ -7,30 +7,70 @@ on: jobs: build: - runs-on: ubuntu-latest - steps: - name: Checkout Git repository - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: fetch-depth: 0 - name: Fetch Git tags run: git fetch --tags - name: set up JDK 11 - uses: actions/setup-java@v1 + uses: actions/setup-java@v3 with: - java-version: 11 - - name: Cache local Gradle repository - uses: actions/cache@v2 - with: - path: | - ~/.gradle/caches - ~/.gradle/wrapper - key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} - restore-keys: | - ${{ runner.os }}-gradle- + distribution: 'zulu' # See 'Supported distributions' for available options + java-version: '11' + - name: Gradle cache + uses: gradle/gradle-build-action@v2 - name: Grant execute permission for gradlew run: chmod +x gradlew - name: Build with Gradle run: ./gradlew build + + test: + runs-on: ubuntu-latest + strategy: + matrix: + api-level: [24, 30, 32] + target: [default, google_apis] + steps: + - name: checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Fetch Git tags + run: git fetch --tags + - name: set up JDK 11 + uses: actions/setup-java@v3 + with: + distribution: 'zulu' # See 'Supported distributions' for available options + java-version: '11' + - name: Gradle cache + uses: gradle/gradle-build-action@v2 + - name: AVD cache + uses: actions/cache@v3 + id: avd-cache + with: + path: | + ~/.android/avd/* + ~/.android/adb* + key: avd-${{ matrix.api-level }} + - name: create AVD and generate snapshot for caching + if: steps.avd-cache.outputs.cache-hit != 'true' + uses: reactivecircus/android-emulator-runner@v2 + with: + api-level: ${{ matrix.api-level }} + force-avd-creation: false + emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none + disable-animations: false + script: echo "Generated AVD snapshot for caching." + - name: Grant execute permission for gradlew + run: chmod +x gradlew + - name: run tests + uses: reactivecircus/android-emulator-runner@v2 + with: + api-level: ${{ matrix.api-level }} + force-avd-creation: false + emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none + disable-animations: true + script: ./gradlew connectedCheck diff --git a/.github/workflows/android-release.yml b/.github/workflows/android-release.yml deleted file mode 100644 index ff022caf..00000000 --- a/.github/workflows/android-release.yml +++ /dev/null @@ -1,79 +0,0 @@ -name: Android CI for releases - -on: - push: - # Pattern matched against refs/tags - tags: - - '*' # Push events to every tag - -jobs: - release: - - runs-on: ubuntu-latest - - steps: - - name: Checkout Git repository - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - name: Fetch Git tags - run: git fetch --tags - - name: set up JDK 1.8 - uses: actions/setup-java@v1 - with: - java-version: 1.8 - - name: Cache local Gradle repository - uses: actions/cache@v2 - with: - path: | - ~/.gradle/caches - ~/.gradle/wrapper - key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} - restore-keys: | - ${{ runner.os }}-gradle- - - name: Grant execute permission for gradlew - run: chmod +x gradlew - - name: Build with Gradle - run: ./gradlew build -Pbuild=prod - - name: Sign artifact - id: signArtifact - uses: r0adkll/sign-android-release@v1 - with: - releaseDirectory: app/build/outputs/apk/release/release - alias: ${{ secrets.ALIAS }} - signingKeyBase64: ${{ secrets.KEYSTORE }} - keyStorePassword: ${{ secrets.KEYSTORE_PASSWORD }} - keyPassword: ${{ secrets.KEY_PASSWORD }} - - name: Upload artifact - uses: actions/upload-artifact@v2 - with: - name: Release artifact - path: app/build/outputs/apk/release/release/SWADroid-*.apk - - name: Create Release - id: create_release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: Release ${{ github.ref }} - draft: false - prerelease: false - - name: Save name of Artifact - id: set-result-artifact - run: | - ARTIFACT_PATHNAME_APK=$(ls app/build/outputs/apk/release/release/*.apk | head -n 1) - ARTIFACT_NAME_APK=$(basename $ARTIFACT_PATHNAME_APK) - echo "ARTIFACT_NAME_APK is " ${ARTIFACT_NAME_APK} - echo "ARTIFACT_PATHNAME_APK=${ARTIFACT_PATHNAME_APK}" >> $GITHUB_ENV - echo "ARTIFACT_NAME_APK=${ARTIFACT_NAME_APK}" >> $GITHUB_ENV - - name: Upload Artifact Assets - id: upload-release-asset - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ${{ env.ARTIFACT_PATHNAME_APK }} - asset_name: ${{ env.ARTIFACT_NAME_APK }} - asset_content_type: application/zip diff --git a/.gitignore b/.gitignore index d68d3a56..3923a389 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,4 @@ lint/tmp/ # lint/reports/ /SWADroid/src/debug/ /SWADroid/release/ +/SWADroid/schemas/ diff --git a/SWADroid/build.gradle b/SWADroid/build.gradle index aad58ad9..99d6fe3b 100644 --- a/SWADroid/build.gradle +++ b/SWADroid/build.gradle @@ -39,30 +39,32 @@ if (build_param != "prod") { } android { - compileSdkVersion 31 - buildToolsVersion "31.0.0" + compileSdkVersion 32 + buildToolsVersion "32.0.0" compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } - lintOptions { - abortOnError false - } - defaultConfig { applicationId "es.ugr.swad.swadroid" versionName androidGitVersion.name() versionCode androidGitVersion.code() minSdkVersion 24 - targetSdkVersion 31 + targetSdkVersion 32 - testApplicationId "es.ugr.swad.swadroid.test" - testInstrumentationRunner "android.test.InstrumentationTestRunner" + testApplicationId "es.ugr.swad.swadroid.androidTest" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" buildConfigField "String", "SWAD_APP_KEY", getBuildConfigField("SWAD_APP_KEY") + + javaCompileOptions { + annotationProcessorOptions { + arguments += ["room.schemaLocation": "$projectDir/schemas".toString()] + } + } } buildTypes { @@ -72,6 +74,16 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + + sourceSets { + // Adds exported schema location as androidTest app assets. + androidTest.assets.srcDirs += files("$projectDir/schemas".toString()) + } + lint { + abortOnError false + } + namespace 'es.ugr.swad.swadroid' + } dependencies { @@ -86,4 +98,28 @@ dependencies { implementation 'com.google.android.material:material:1.6.0' implementation platform('com.google.firebase:firebase-bom:30.0.2') implementation 'com.google.firebase:firebase-analytics' + + //Lombok + def lombok_version = "1.18.24" + compileOnly "org.projectlombok:lombok:$lombok_version" + annotationProcessor "org.projectlombok:lombok:$lombok_version" + testCompileOnly "org.projectlombok:lombok:$lombok_version" + testAnnotationProcessor "org.projectlombok:lombok:$lombok_version" + + //Database dependencies + def room_version = "2.4.2" + implementation 'net.zetetic:android-database-sqlcipher:4.5.1@aar' + implementation "androidx.sqlite:sqlite:2.2.0" + implementation "androidx.room:room-runtime:$room_version" + annotationProcessor "androidx.room:room-compiler:$room_version" + // optional - Test helpers + testImplementation "androidx.room:room-testing:$room_version" + + //Tests + def test_version = "1.4.0" + testImplementation 'junit:junit:4.13.2' + androidTestImplementation "androidx.test:core:$test_version" + androidTestImplementation "androidx.test:rules:$test_version" + androidTestImplementation "androidx.test:runner:$test_version" + androidTestImplementation 'androidx.test.ext:junit:1.1.3' } diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/CourseDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/CourseDaoTest.java new file mode 100644 index 00000000..6ea6bf2c --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/CourseDaoTest.java @@ -0,0 +1,115 @@ +package es.ugr.swad.swadroid.dao; + +import static junit.framework.TestCase.assertEquals; + +import android.database.Cursor; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import es.ugr.swad.swadroid.model.Course; + +public class CourseDaoTest extends DaoFixtureTest { + + @Before + public void setUp() { + courseDao.insertCourses(COURSES); + } + + @Test + public void findAll() { + List all = courseDao.findAll(); + assertEquals(COURSES, all); + } + + @Test + public void findAllOrderedByShortNameAsc() { + List ordered = courseDao.findAllOrderedByShortNameAsc(); + List expected = Arrays.asList( + new Course(2, 1, "c1", "Course 1"), + new Course(1, 1, "c2", "Course 2"), + new Course(3, 1, "c3", "Course 3") + ); + + assertEquals(expected, ordered); + } + + @Test + public void findAllOrderedByShortNameAscCursor() { + Cursor cursor = courseDao.findAllOrderedByShortNameAscCursor(); + List ordered = new ArrayList<>(); + List expected = Arrays.asList( + new Course(1, 1, "c1", "Course 1"), + new Course(2, 1, "c2", "Course 2"), + new Course(3, 1, "c3", "Course 3") + ); + + int idColNum = cursor.getColumnIndexOrThrow("id"); + int userRoleColNum = cursor.getColumnIndexOrThrow("userRole"); + int shortNameColNum = cursor.getColumnIndexOrThrow("shortName"); + int fullNameColNum = cursor.getColumnIndex("fullName"); + + while (cursor.moveToNext()) { + long id = cursor.getLong(idColNum); + int userRole = cursor.getInt(userRoleColNum); + String shortName = cursor.getString(shortNameColNum); + String fullName = cursor.getString(fullNameColNum); + ordered.add(new Course(id, userRole, shortName, fullName)); + } + + assertEquals(expected, ordered); + } + + @Test + public void findCourseById() { + Course byId = courseDao.findCourseById(1); + assertEquals(COURSES.get(0), byId); + } + + @Test + public void insertCourses() { + List all = courseDao.findAll(); + assertEquals(COURSES, all); + } + + @Test + public void updateCourses() { + Course course = new Course(2, 1, "c1", "Course 1 Modified"); + courseDao.updateCourses(Collections.singletonList(course)); + + List modified = courseDao.findAll(); + List expected = Arrays.asList( + new Course(1, 1, "c2", "Course 2"), + course, + new Course(3, 1, "c3", "Course 3") + ); + + assertEquals(expected, modified); + } + + @Test + public void deleteCourses() { + List all = courseDao.findAll(); + List expected = COURSES.subList(1, 3); + assertEquals(COURSES, all); + + int numDeleted = courseDao.deleteCourses(Collections.singletonList(COURSES.get(0))); + assertEquals(1, numDeleted); + assertEquals(expected, courseDao.findAll()); + } + + @Test + public void deleteAll() { + List all = courseDao.findAll(); + assertEquals(COURSES, all); + + int numDeleted = courseDao.deleteAll(); + assertEquals(3, numDeleted); + assertEquals(Collections.EMPTY_LIST, courseDao.findAll()); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/DaoFixtureTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/DaoFixtureTest.java new file mode 100644 index 00000000..867f36ee --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/DaoFixtureTest.java @@ -0,0 +1,166 @@ +package es.ugr.swad.swadroid.dao; + +import android.content.Context; + +import androidx.room.Room; +import androidx.test.platform.app.InstrumentationRegistry; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; + +import java.util.Arrays; +import java.util.List; + +import es.ugr.swad.swadroid.database.AppDatabase; +import es.ugr.swad.swadroid.model.Course; +import es.ugr.swad.swadroid.model.Event; +import es.ugr.swad.swadroid.model.Group; +import es.ugr.swad.swadroid.model.GroupType; +import es.ugr.swad.swadroid.model.SWADNotification; +import es.ugr.swad.swadroid.model.TestAnswer; +import es.ugr.swad.swadroid.model.TestConfig; +import es.ugr.swad.swadroid.model.TestQuestion; +import es.ugr.swad.swadroid.model.TestTag; +import es.ugr.swad.swadroid.model.TestTagsQuestions; +import es.ugr.swad.swadroid.model.User; +import es.ugr.swad.swadroid.model.UserAttendance; + +public abstract class DaoFixtureTest { + protected static AppDatabase db; + + protected static CourseDao courseDao; + protected static EventDao eventDao; + protected static GroupTypeDao groupTypeDao; + protected static GroupDao groupDao; + protected static UserDao userDao; + protected static UserAttendanceDao userAttendanceDao; + protected static SWADNotificationDao swadNotificationDao; + protected static TestConfigDao testConfigDao; + protected static TestTagDao testTagDao; + protected static TestQuestionDao testQuestionDao; + protected static TestTagsQuestionsDao testTagsQuestionsDao; + protected static TestAnswerDao testAnswerDao; + protected static RawDao rawDao; + + private static final long BASE_TIME = System.currentTimeMillis() / 1000; + + protected final List COURSES = Arrays.asList( + new Course(1, 1, "c2", "Course 2"), + new Course(2, 1, "c1", "Course 1"), + new Course(3, 1, "c3", "Course 3") + ); + protected final List EVENTS = Arrays.asList( + new Event(1, 2, false, "", "", "", "", 0L, 0L, false, "Event 2", "Description 2", "", ""), + new Event(2, 1, false, "", "", "", "", 0L, 0L, false, "Event 1", "Description 1", "", ""), + new Event(3, 3, false, "", "", "", "", 0L, 0L, false, "Event 3", "Description 3", "", "") + ); + protected final List ATTENDANCES = Arrays.asList( + new UserAttendance(1, 2, false), + new UserAttendance(2, 1, false), + new UserAttendance(3, 3, false) + ); + protected final List USERS = Arrays.asList( + new User(1, "wsKey", "userID2", "userNickname2", "userSurname1", "userSurname2", "userFirstname", "userPhoto", "20210101", 0), + new User(2, "wsKey", "userID1", "userNickname1", "userSurname1", "userSurname2", "userFirstname", "userPhoto", "20210101", 0), + new User(3, "wsKey", "userID3", "userNickname3", "userSurname1", "userSurname2", "userFirstname", "userPhoto", "20210101", 0) + ); + protected final List GROUP_TYPES = Arrays.asList( + new GroupType(1, "Type 2", 2, 0, 0, 0), + new GroupType(2, "Type 1", 1, 0, 0, 0), + new GroupType(3, "Type 3", 3, 0, 0, 0), + new GroupType(4, "Type 4", 1, 0, 0, 0) + ); + protected final List GROUPS = Arrays.asList( + new Group(1, "Group 2", 2, 2, 0, 0, 0, 0, 0), + new Group(2, "Group 1", 1, 1, 0, 0, 0, 0, 0), + new Group(3, "Group 3", 3, 3, 0, 0, 0, 0, 0) + ); + protected final List NOTIFICATIONS = Arrays.asList( + new SWADNotification(1, 2, "eventType", BASE_TIME, "userNickname", "userSurname1", "userSurname2", "userFirstName", "userPhoto", "location", "summary", 0, "content", true, false), + new SWADNotification(2, 1, "eventType", BASE_TIME - 604800, "userNickname", "userSurname1", "userSurname2", "userFirstName", "userPhoto", "location", "summary", 0, "content", false, false), + new SWADNotification(3, 3, "eventType", BASE_TIME - 86400, "userNickname", "userSurname1", "userSurname2", "userFirstName", "userPhoto", "location", "summary", 0, "content", false, false) + ); + protected final List TEST_CONFIGS = Arrays.asList( + new TestConfig(1, 1, 3, 5, "F1", BASE_TIME), + new TestConfig(2, 1, 3, 5, "F2", BASE_TIME + 1), + new TestConfig(3, 1, 3, 5, "F3", BASE_TIME + 2) + ); + protected final List TEST_TAGS = Arrays.asList( + new TestTag(2, "Tag2"), + new TestTag(1, "Tag1"), + new TestTag(3, "Tag3") + ); + protected final List TEST_QUESTIONS = Arrays.asList( + new TestQuestion(2, 1, "Stem2", TestAnswer.TYPE_TRUE_FALSE, false, "Feedback2"), + new TestQuestion(1, 1, "Stem1", TestAnswer.TYPE_UNIQUE_CHOICE, false, "Feedback1"), + new TestQuestion(3, 1, "Stem3", TestAnswer.TYPE_MULTIPLE_CHOICE, false, "Feedback3"), + new TestQuestion(5, 2, "Stem5", TestAnswer.TYPE_TRUE_FALSE, true, "Feedback5"), + new TestQuestion(4, 2, "Stem4", TestAnswer.TYPE_UNIQUE_CHOICE, true, "Feedback4"), + new TestQuestion(6, 2, "Stem6", TestAnswer.TYPE_MULTIPLE_CHOICE, true, "Feedback6"), + new TestQuestion(8, 3, "Stem8", TestAnswer.TYPE_TRUE_FALSE, false, "Feedback8"), + new TestQuestion(7, 3, "Stem7", TestAnswer.TYPE_UNIQUE_CHOICE, false, "Feedback7"), + new TestQuestion(9, 3, "Stem9", TestAnswer.TYPE_MULTIPLE_CHOICE, false, "Feedback9") + ); + protected final List TEST_TAGS_QUESTIONS = Arrays.asList( + new TestTagsQuestions(1, 2, 1), + new TestTagsQuestions(2, 2, 1), + new TestTagsQuestions(3, 2, 1), + new TestTagsQuestions(5, 1, 2), + new TestTagsQuestions(4, 1, 2), + new TestTagsQuestions(6, 1, 2), + new TestTagsQuestions(8, 3, 3), + new TestTagsQuestions(7, 3, 3), + new TestTagsQuestions(9, 3, 3) + ); + protected final List TEST_ANSWERS = Arrays.asList( + new TestAnswer(1, 1, 1, true, "Answer1", "Feedback1"), + new TestAnswer(2, 1, 2, false, "Answer2", "Feedback2"), + new TestAnswer(3, 2, 1, false, "Answer3", "Feedback3"), + new TestAnswer(4, 2, 2, true, "Answer4", "Feedback4"), + new TestAnswer(5, 3, 1, true, "Answer5", "Feedback5"), + new TestAnswer(6, 3, 2, false, "Answer6", "Feedback6"), + new TestAnswer(7, 4, 1, true, "Answer7", "Feedback7"), + new TestAnswer(8, 4, 2, false, "Answer8", "Feedback8"), + new TestAnswer(9, 5, 1, false, "Answer9", "Feedback9"), + new TestAnswer(10, 5, 2, true, "Answer10", "Feedback10"), + new TestAnswer(11, 6, 1, true, "Answer11", "Feedback11"), + new TestAnswer(12, 6, 2, false, "Answer12", "Feedback12"), + new TestAnswer(13, 7, 1, true, "Answer13", "Feedback13"), + new TestAnswer(14, 7, 2, false, "Answer14", "Feedback14"), + new TestAnswer(15, 8, 1, false, "Answer15", "Feedback15"), + new TestAnswer(16, 8, 2, true, "Answer16", "Feedback16"), + new TestAnswer(17, 9, 1, true, "Answer17", "Feedback17"), + new TestAnswer(18, 9, 2, true, "Answer18", "Feedback18") + ); + + @BeforeClass + public static void setUpClass() { + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + db = Room.inMemoryDatabaseBuilder(context, AppDatabase.class).build(); + + courseDao = db.getCourseDao(); + eventDao = db.getEventDao(); + groupTypeDao = db.getGroupTypeDao(); + groupDao = db.getGroupDao(); + userDao = db.getUserDao(); + userAttendanceDao = db.getUserAttendanceDao(); + swadNotificationDao = db.getSwadNotificationDao(); + testConfigDao = db.getTestConfigDao(); + testTagDao = db.getTestTagDao(); + testQuestionDao = db.getTestQuestionDao(); + testTagsQuestionsDao = db.getTestTagsQuestionsDao(); + testAnswerDao = db.getTestAnswerDao(); + rawDao = db.getRawDao(); + } + + @After + public void tearDown() { + db.clearAllTables(); + } + + @AfterClass + public static void tearDownClass() { + db.close(); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/EventDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/EventDaoTest.java new file mode 100644 index 00000000..9366a73b --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/EventDaoTest.java @@ -0,0 +1,132 @@ +package es.ugr.swad.swadroid.dao; + +import static org.junit.Assert.assertEquals; + +import android.database.Cursor; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import es.ugr.swad.swadroid.model.Event; + +public class EventDaoTest extends DaoFixtureTest { + + @Before + public void setUp() { + courseDao.insertCourses(COURSES); + eventDao.insertEvents(EVENTS); + } + + @Test + public void findAll() { + List all = eventDao.findAll(); + assertEquals(EVENTS, all); + } + + @Test + public void findAllByCourseCode() { + List events = eventDao.findAllByCourseCode(COURSES.get(0).getId()); + List expected = Collections.singletonList( + new Event(2, 1, false, "", "", "", "", 0L, 0L, false, "Event 1", "Description 1", "", "") + ); + + assertEquals(expected, events); + } + + @Test + public void findAllByCourseCodeCursor() { + Cursor cursor = eventDao.findAllByCourseCodeCursor(COURSES.get(0).getId()); + List ordered = new ArrayList<>(); + List expected = Collections.singletonList( + new Event(2, 1, false, "", "", "", "", 0L, 0L, false, "Event 1", "Description 1", "", "") + ); + + int idColNum = cursor.getColumnIndexOrThrow("id"); + int crsCodColNum = cursor.getColumnIndexOrThrow("crsCod"); + int hiddenColNum = cursor.getColumnIndexOrThrow("hidden"); + int userSurname1ColNum = cursor.getColumnIndexOrThrow("userSurname1"); + int userSurname2ColNum = cursor.getColumnIndex("userSurname2"); + int userFirstNameColNum = cursor.getColumnIndex("userFirstName"); + int userPhotoColNum = cursor.getColumnIndex("userPhoto"); + int startTimeColNum = cursor.getColumnIndex("startTime"); + int endTimeColNum = cursor.getColumnIndex("endTime"); + int commentsTeachersVisibleColNum = cursor.getColumnIndex("commentsTeachersVisible"); + int titleNameColNum = cursor.getColumnIndex("title"); + int textNameColNum = cursor.getColumnIndex("text"); + int groupsNameColNum = cursor.getColumnIndex("groups"); + int statusNameColNum = cursor.getColumnIndex("status"); + + while (cursor.moveToNext()) { + long id = cursor.getLong(idColNum); + long crsCod = cursor.getLong(crsCodColNum); + boolean hidden = cursor.getInt(hiddenColNum) > 0; + String userSurname1 = cursor.getString(userSurname1ColNum); + String userSurname2 = cursor.getString(userSurname2ColNum); + String userFirstName = cursor.getString(userFirstNameColNum); + String userPhoto = cursor.getString(userPhotoColNum); + long startTime = cursor.getLong(startTimeColNum); + long endTime = cursor.getLong(endTimeColNum); + boolean commentsTeachersVisible = cursor.getInt(commentsTeachersVisibleColNum) > 0; + String title = cursor.getString(titleNameColNum); + String text = cursor.getString(textNameColNum); + String groups = cursor.getString(groupsNameColNum); + String status = cursor.getString(statusNameColNum); + ordered.add(new Event(id, crsCod, hidden, userSurname1, userSurname2, userFirstName, userPhoto, startTime, endTime, commentsTeachersVisible, title, text, groups, status)); + } + + assertEquals(expected, ordered); + } + + @Test + public void findById() { + Event byId = eventDao.findById(1); + assertEquals(EVENTS.get(0), byId); + } + + @Test + public void insertEvents() { + List all = eventDao.findAll(); + assertEquals(EVENTS, all); + } + + @Test + public void updateEvents() { + Event event = new Event(2, 1, false, "", "", "", "", 0L, 0L, false, "Event 1 Modified", "Description 1 Modified", "", ""); + eventDao.updateEvents(Collections.singletonList(event)); + + List modified = eventDao.findAll(); + List expected = Arrays.asList( + new Event(1, 2, false, "", "", "", "", 0L, 0L, false, "Event 2", "Description 2", "", ""), + event, + new Event(3, 3, false, "", "", "", "", 0L, 0L, false, "Event 3", "Description 3", "", "") + ); + + assertEquals(expected, modified); + } + + @Test + public void deleteEvents() { + List all = eventDao.findAll(); + List expected = EVENTS.subList(1, 3); + assertEquals(EVENTS, all); + + int numDeleted = eventDao.deleteEvents(Collections.singletonList(EVENTS.get(0))); + assertEquals(1, numDeleted); + assertEquals(expected, eventDao.findAll()); + } + + @Test + public void deleteAll() { + List all = eventDao.findAll(); + assertEquals(EVENTS, all); + + int numDeleted = eventDao.deleteAll(); + assertEquals(3, numDeleted); + assertEquals(Collections.EMPTY_LIST, eventDao.findAll()); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/GroupDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/GroupDaoTest.java new file mode 100644 index 00000000..6abc79c1 --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/GroupDaoTest.java @@ -0,0 +1,117 @@ +package es.ugr.swad.swadroid.dao; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import es.ugr.swad.swadroid.model.Group; +import es.ugr.swad.swadroid.model.GroupsCourse; +import es.ugr.swad.swadroid.model.GroupsGroupType; + +public class GroupDaoTest extends DaoFixtureTest { + + @Before + public void setUp() { + courseDao.insertCourses(COURSES); + groupTypeDao.insertGroupTypes(GROUP_TYPES); + groupDao.insertGroups(GROUPS); + } + + @Test + public void findAll() { + List all = groupDao.findAll(); + assertEquals(GROUPS, all); + } + + @Test + public void findAllByTypeCode() { + List groups = groupDao.findAllByTypeCode(GROUP_TYPES.get(1).getId()); + List expected = Collections.singletonList( + new Group(1, "Group 2", 2, 2, 0, 0, 0, 0, 0) + ); + + assertEquals(expected, groups); + } + + @Test + public void findGroupsGroupTypeByGroupCode() { + GroupsGroupType groupsGroupTypeByGroupCode = groupDao.findGroupsGroupTypeByGroupCode(GROUPS.get(0).getId()); + GroupsGroupType expected = new GroupsGroupType(); + expected.setGroupType(GROUP_TYPES.get(1)); + expected.setGroups(Collections.singletonList( + new Group(1, "Group 2", 2, 2, 0, 0, 0, 0, 0) + )); + + assertEquals(expected, groupsGroupTypeByGroupCode); + } + + @Test + public void findGroupsCourseByCourseCode() { + GroupsCourse groupsCourseByCourseCode = groupDao.findGroupsCourseByCourseCode(COURSES.get(1).getId()); + GroupsCourse expected = new GroupsCourse(); + expected.setCourse(COURSES.get(1)); + expected.setGroups(Collections.singletonList( + new Group(1, "Group 2", 2, 2, 0, 0, 0, 0, 0) + )); + + assertEquals(expected, groupsCourseByCourseCode); + } + + @Test + public void insertGroups() { + List all = groupDao.findAll(); + assertEquals(GROUPS, all); + } + + @Test + public void updateGroups() { + Group group = new Group(2, "Group 1 Modified", 1, 1, 0, 0, 0, 0, 0); + groupDao.updateGroups(Collections.singletonList(group)); + + List modified = groupDao.findAll(); + List expected = Arrays.asList( + new Group(1, "Group 2", 2, 2, 0, 0, 0, 0, 0), + group, + new Group(3, "Group 3", 3, 3, 0, 0, 0, 0, 0) + ); + + assertEquals(expected, modified); + } + + @Test + public void deleteGroups() { + List all = groupDao.findAll(); + List expected = GROUPS.subList(1, 3); + assertEquals(GROUPS, all); + + int numDeleted = groupDao.deleteGroups(Collections.singletonList(GROUPS.get(0))); + assertEquals(1, numDeleted); + assertEquals(expected, groupDao.findAll()); + } + + @Test + public void deleteGroupsByIdNotIn() { + List all = groupDao.findAll(); + List expected = Collections.singletonList(GROUPS.get(0)); + assertEquals(GROUPS, all); + + int numDeleted = groupDao.deleteGroupsByIdNotIn(Collections.singletonList(GROUPS.get(0).getId())); + assertEquals(2, numDeleted); + assertEquals(expected, groupDao.findAll()); + } + + @Test + public void deleteAll() { + List all = groupDao.findAll(); + assertEquals(GROUPS, all); + + int numDeleted = groupDao.deleteAll(); + assertEquals(3, numDeleted); + assertEquals(Collections.EMPTY_LIST, groupDao.findAll()); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/GroupTypeDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/GroupTypeDaoTest.java new file mode 100644 index 00000000..2d0f67e3 --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/GroupTypeDaoTest.java @@ -0,0 +1,91 @@ +package es.ugr.swad.swadroid.dao; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import es.ugr.swad.swadroid.model.GroupType; + +public class GroupTypeDaoTest extends DaoFixtureTest { + + @Before + public void setUp() { + courseDao.insertCourses(COURSES); + groupTypeDao.insertGroupTypes(GROUP_TYPES); + } + + @Test + public void findAll() { + List all = groupTypeDao.findAll(); + assertEquals(GROUP_TYPES, all); + } + + @Test + public void findAllByCourseCode() { + List groupTypes = groupTypeDao.findAllByCourseCode(COURSES.get(1).getId()); + List expected = Collections.singletonList( + new GroupType(1, "Type 2", 2, 0, 0, 0) + ); + + assertEquals(expected, groupTypes); + } + + @Test + public void findAllByCourseCodeOrderByGroupTypeNameAsc() { + List groupTypes = groupTypeDao.findAllByCourseCodeOrderByGroupTypeNameAsc(COURSES.get(0).getId()); + List expected = Arrays.asList( + new GroupType(2, "Type 1", 1, 0, 0, 0), + new GroupType(4, "Type 4", 1, 0, 0, 0) + ); + + assertEquals(expected, groupTypes); + } + + @Test + public void insertGroupTypes() { + List all = groupTypeDao.findAll(); + assertEquals(GROUP_TYPES, all); + } + + @Test + public void updateGroupTypes() { + GroupType groupType = new GroupType(2, "Type 1 Modified", 1, 0, 0, 0); + groupTypeDao.updateGroupTypes(Collections.singletonList(groupType)); + + List modified = groupTypeDao.findAll(); + List expected = Arrays.asList( + new GroupType(1, "Type 2", 2, 0, 0, 0), + groupType, + new GroupType(3, "Type 3", 3, 0, 0, 0), + new GroupType(4, "Type 4", 1, 0, 0, 0) + ); + + assertEquals(expected, modified); + } + + @Test + public void deleteGroupTypes() { + List all = groupTypeDao.findAll(); + List expected = GROUP_TYPES.subList(1, 4); + assertEquals(GROUP_TYPES, all); + + int numDeleted = groupTypeDao.deleteGroupTypes(Collections.singletonList(GROUP_TYPES.get(0))); + assertEquals(1, numDeleted); + assertEquals(expected, groupTypeDao.findAll()); + } + + @Test + public void deleteAll() { + List all = groupTypeDao.findAll(); + assertEquals(GROUP_TYPES, all); + + int numDeleted = groupTypeDao.deleteAll(); + assertEquals(4, numDeleted); + assertEquals(Collections.EMPTY_LIST, groupTypeDao.findAll()); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/RawDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/RawDaoTest.java new file mode 100644 index 00000000..58143753 --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/RawDaoTest.java @@ -0,0 +1,15 @@ +package es.ugr.swad.swadroid.dao; + +import static junit.framework.TestCase.assertEquals; + +import androidx.sqlite.db.SimpleSQLiteQuery; + +import org.junit.Test; + +public class RawDaoTest extends DaoFixtureTest { + + @Test + public void rawQuery() { + assertEquals(0, rawDao.rawQuery(new SimpleSQLiteQuery("VACUUM"))); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/SWADNotificationDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/SWADNotificationDaoTest.java new file mode 100644 index 00000000..167209d5 --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/SWADNotificationDaoTest.java @@ -0,0 +1,144 @@ +package es.ugr.swad.swadroid.dao; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import es.ugr.swad.swadroid.model.SWADNotification; + +public class SWADNotificationDaoTest extends DaoFixtureTest { + + @Before + public void setUp() { + swadNotificationDao.insertSWADNotifications(NOTIFICATIONS); + } + + @Test + public void findAll() { + List all = swadNotificationDao.findAll(); + assertEquals(NOTIFICATIONS, all); + } + + @Test + public void findAllOrderByEventTimeDesc() { + List notifications = swadNotificationDao.findAllOrderByEventTimeDesc(false); + List expected = Arrays.asList( + NOTIFICATIONS.get(2), + NOTIFICATIONS.get(1) + ); + + assertEquals(expected, notifications); + } + + @Test + public void findAllPendingToRead() { + List notifications = swadNotificationDao.findAllPendingToRead(); + List expected = Collections.singletonList( + NOTIFICATIONS.get(0) + ); + + assertEquals(expected, notifications); + } + + @Test + public void findById() { + SWADNotification notification = swadNotificationDao.findById(1); + assertEquals(NOTIFICATIONS.get(0), notification); + } + + @Test + public void countAll() { + long count = swadNotificationDao.countAll(); + assertEquals(3, count); + } + + @Test + public void findMaxEventTime() { + long maxEventTime = swadNotificationDao.findMaxEventTime(); + assertEquals(NOTIFICATIONS.get(0).getEventTime(), maxEventTime); + } + + @Test + public void insertSWADNotifications() { + List all = swadNotificationDao.findAll(); + assertEquals(NOTIFICATIONS, all); + } + + @Test + public void updateSWADNotifications() { + SWADNotification notification = new SWADNotification(2, 1, "eventType", 604800, "userNickname", "userSurname1", "userSurname2", "userFirstName", "userPhoto", "location", "summary", 0, "content", false, false); + swadNotificationDao.updateSWADNotifications(Collections.singletonList(notification)); + + List modified = swadNotificationDao.findAll(); + List expected = Arrays.asList( + NOTIFICATIONS.get(0), + notification, + NOTIFICATIONS.get(2) + ); + + assertEquals(expected, modified); + } + + @Test + public void updateAllBySeenLocal() { + swadNotificationDao.updateAllBySeenLocal(true); + + List modified = swadNotificationDao.findAll(); + List expected = NOTIFICATIONS.stream().peek(n -> n.setSeenLocal(true)) + .collect(Collectors.toList()); + + assertEquals(expected, modified); + } + + @Test + public void updateAllBySeenRemote() { + swadNotificationDao.updateAllBySeenRemote(true); + + List modified = swadNotificationDao.findAll(); + List expected = NOTIFICATIONS.stream().peek(n -> n.setSeenRemote(true)) + .collect(Collectors.toList()); + + assertEquals(expected, modified); + } + + @Test + public void deleteSWADNotifications() { + List all = swadNotificationDao.findAll(); + List expected = NOTIFICATIONS.subList(1, 3); + assertEquals(NOTIFICATIONS, all); + + int numDeleted = swadNotificationDao.deleteSWADNotifications(Collections.singletonList(NOTIFICATIONS.get(0))); + assertEquals(1, numDeleted); + assertEquals(expected, swadNotificationDao.findAll()); + } + + @Test + public void deleteAllByAge() { + List all = swadNotificationDao.findAll(); + List expected = Arrays.asList( + NOTIFICATIONS.get(0), + NOTIFICATIONS.get(2) + ); + assertEquals(NOTIFICATIONS, all); + + int numDeleted = swadNotificationDao.deleteAllByAge(604800); + assertEquals(1, numDeleted); + assertEquals(expected, swadNotificationDao.findAll()); + } + + @Test + public void deleteAll() { + List all = swadNotificationDao.findAll(); + assertEquals(NOTIFICATIONS, all); + + int numDeleted = swadNotificationDao.deleteAll(); + assertEquals(3, numDeleted); + assertEquals(Collections.EMPTY_LIST, swadNotificationDao.findAll()); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestAnswerDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestAnswerDaoTest.java new file mode 100644 index 00000000..ad9cc81d --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestAnswerDaoTest.java @@ -0,0 +1,86 @@ +package es.ugr.swad.swadroid.dao; + +import static junit.framework.TestCase.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import es.ugr.swad.swadroid.model.Model; +import es.ugr.swad.swadroid.model.TestAnswer; + +public class TestAnswerDaoTest extends DaoFixtureTest { + + private final Comparator compareById = Comparator.comparingLong(Model::getId); + + @Before + public void setUp() { + courseDao.insertCourses(COURSES); + testTagDao.insertTestTag(TEST_TAGS); + testQuestionDao.insertTestQuestion(TEST_QUESTIONS); + testTagsQuestionsDao.insertTestTagQuestion(TEST_TAGS_QUESTIONS); + testAnswerDao.insertTestAnswer(TEST_ANSWERS); + } + + @Test + public void findAll() { + List all = testAnswerDao.findAll(); + assertEquals(TEST_ANSWERS, all); + } + + @Test + public void insertTestAnswer() { + List all = testAnswerDao.findAll(); + assertEquals(TEST_ANSWERS, all); + } + + @Test + public void updateTestAnswer() { + TestAnswer testAnswer = new TestAnswer(2, 1, 2, false, "Answer2 Modified", "Feedback2"); + testAnswerDao.updateTestAnswer(Collections.singletonList(testAnswer)); + + List modified = testAnswerDao.findAll(); + List expected = new ArrayList<>(TEST_ANSWERS); + + modified.sort(compareById); + expected.sort(compareById); + + expected.set(1, testAnswer); + + assertEquals(expected, modified); + } + + @Test + public void deleteTestAnswer() { + List all = testAnswerDao.findAll(); + List expected = new ArrayList<>(TEST_ANSWERS); + List allSorted = new ArrayList<>(TEST_ANSWERS); + + expected.sort(compareById); + allSorted.sort(compareById); + + TestAnswer deleted = expected.remove(1); + + assertEquals(allSorted, all); + + int numDeleted = testAnswerDao.deleteTestAnswer(Collections.singletonList(deleted)); + assertEquals(1, numDeleted); + assertEquals(expected, testAnswerDao.findAll()); + } + + @Test + public void deleteAll() { + List all = testAnswerDao.findAll(); + List expected = new ArrayList<>(TEST_ANSWERS); + expected.sort(compareById); + assertEquals(expected, all); + + int numDeleted = testAnswerDao.deleteAll(); + assertEquals(18, numDeleted); + assertEquals(Collections.EMPTY_LIST, testAnswerDao.findAll()); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestConfigDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestConfigDaoTest.java new file mode 100644 index 00000000..43c239fa --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestConfigDaoTest.java @@ -0,0 +1,105 @@ +package es.ugr.swad.swadroid.dao; + +import static junit.framework.TestCase.assertEquals; + +import android.database.Cursor; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import es.ugr.swad.swadroid.model.TestConfig; + +public class TestConfigDaoTest extends DaoFixtureTest { + + @Before + public void setUp() { + testConfigDao.insertTestConfig(TEST_CONFIGS); + } + + @Test + public void findAll() { + List all = testConfigDao.findAll(); + assertEquals(TEST_CONFIGS, all); + } + + @Test + public void findByCourseCode() { + TestConfig byId = testConfigDao.findByCourseCode(1); + assertEquals(TEST_CONFIGS.get(0), byId); + } + + @Test + public void findCursorByCourseCode() { + Cursor cursor = testConfigDao.findCursorByCourseCode(1); + List ordered = new ArrayList<>(); + List expected = Collections.singletonList( + TEST_CONFIGS.get(0) + ); + + int idColNum = cursor.getColumnIndexOrThrow("id"); + int minColNum = cursor.getColumnIndexOrThrow("min"); + int defColNum = cursor.getColumnIndexOrThrow("def"); + int maxColNum = cursor.getColumnIndex("max"); + int feedbackColNum = cursor.getColumnIndex("feedback"); + int editTimeColNum = cursor.getColumnIndex("editTime"); + + while (cursor.moveToNext()) { + long id = cursor.getLong(idColNum); + int min = cursor.getInt(minColNum); + int def = cursor.getInt(defColNum); + int max = cursor.getInt(maxColNum); + String feedback = cursor.getString(feedbackColNum); + long editTime = cursor.getLong(editTimeColNum); + ordered.add(new TestConfig(id, min, def, max, feedback, editTime)); + } + + assertEquals(expected, ordered); + } + + @Test + public void insertTestConfig() { + List all = testConfigDao.findAll(); + assertEquals(TEST_CONFIGS, all); + } + + @Test + public void updateTestConfig() { + TestConfig testConfig = new TestConfig(2, 1, 3, 5, "F2 Modified", 0L); + testConfigDao.updateTestConfig(Collections.singletonList(testConfig)); + + List modified = testConfigDao.findAll(); + List expected = Arrays.asList( + TEST_CONFIGS.get(0), + testConfig, + TEST_CONFIGS.get(2) + ); + + assertEquals(expected, modified); + } + + @Test + public void deleteTestConfig() { + List all = testConfigDao.findAll(); + List expected = TEST_CONFIGS.subList(1, 3); + assertEquals(TEST_CONFIGS, all); + + int numDeleted = testConfigDao.deleteTestConfig(Collections.singletonList(TEST_CONFIGS.get(0))); + assertEquals(1, numDeleted); + assertEquals(expected, testConfigDao.findAll()); + } + + @Test + public void deleteAll() { + List all = testConfigDao.findAll(); + assertEquals(TEST_CONFIGS, all); + + int numDeleted = testConfigDao.deleteAll(); + assertEquals(3, numDeleted); + assertEquals(Collections.EMPTY_LIST, testConfigDao.findAll()); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestQuestionDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestQuestionDaoTest.java new file mode 100644 index 00000000..1f1f54af --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestQuestionDaoTest.java @@ -0,0 +1,153 @@ +package es.ugr.swad.swadroid.dao; + +import static junit.framework.Assert.assertTrue; +import static junit.framework.TestCase.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +import es.ugr.swad.swadroid.model.Model; +import es.ugr.swad.swadroid.model.TestAnswer; +import es.ugr.swad.swadroid.model.TestAnswersQuestion; +import es.ugr.swad.swadroid.model.TestQuestion; +import es.ugr.swad.swadroid.model.TestTag; + +public class TestQuestionDaoTest extends DaoFixtureTest { + + private final Comparator testQuestionComparator = Comparator.comparingLong(Model::getId); + private final Comparator testAnswerComparator = Comparator.comparingLong(Model::getId); + + @Before + public void setUp() throws Exception { + courseDao.insertCourses(COURSES); + testTagDao.insertTestTag(TEST_TAGS); + testQuestionDao.insertTestQuestion(TEST_QUESTIONS); + testTagsQuestionsDao.insertTestTagQuestion(TEST_TAGS_QUESTIONS); + testAnswerDao.insertTestAnswer(TEST_ANSWERS); + } + + @Test + public void findAll() { + List all = testQuestionDao.findAll(); + List expected = TEST_QUESTIONS; + expected.sort(testQuestionComparator); + assertEquals(expected, all); + } + + @Test + public void findAllCourseQuestionsOrderByRandomAndAnswerIndex() { + int crsCod = 1; + List questions = TEST_QUESTIONS.stream().filter(t -> t.getCrsCod() == crsCod).map(TestQuestion::getId).collect(Collectors.toList()); + TestAnswersQuestion answersQuestion = testQuestionDao.findAllCourseQuestionsOrderByRandomAndAnswerIndex(crsCod, 1).get(0); + + answersQuestion.getTestAnswers().sort(testAnswerComparator); + + assertTrue(questions.contains(answersQuestion.getTestQuestion().getId())); + assertEquals(TEST_ANSWERS.stream().filter(a -> a.getQstCod() == answersQuestion.getTestQuestion().getId()).collect(Collectors.toList()), + answersQuestion.getTestAnswers()); + } + + @Test + public void findAllCourseQuestionsByTagOrderByRandomAndAnswerIndex() { + int crsCod = 1; + List tags = TEST_TAGS.stream().map(TestTag::getTagTxt).collect(Collectors.toList()); + List questions = TEST_QUESTIONS.stream().filter(t -> t.getCrsCod() == crsCod).map(TestQuestion::getId).collect(Collectors.toList()); + TestAnswersQuestion answersQuestion = testQuestionDao.findAllCourseQuestionsByTagOrderByRandomAndAnswerIndex(crsCod, tags, 1).get(0); + + answersQuestion.getTestAnswers().sort(testAnswerComparator); + + assertTrue(questions.contains(answersQuestion.getTestQuestion().getId())); + assertEquals(TEST_ANSWERS.stream().filter(a -> a.getQstCod() == answersQuestion.getTestQuestion().getId()).collect(Collectors.toList()), + answersQuestion.getTestAnswers()); + } + + @Test + public void findAllCourseQuestionsByAnswerTypeOrderByRandomAndAnswerIndex() { + int crsCod = 1; + List answerTypes = Collections.singletonList(TestAnswer.TYPE_TRUE_FALSE); + List questions = TEST_QUESTIONS.stream().filter(t -> t.getCrsCod() == crsCod).map(TestQuestion::getId).collect(Collectors.toList()); + TestAnswersQuestion answersQuestion = testQuestionDao.findAllCourseQuestionsByAnswerTypeOrderByRandomAndAnswerIndex(crsCod, answerTypes, 1).get(0); + + answersQuestion.getTestAnswers().sort(testAnswerComparator); + + assertTrue(questions.contains(answersQuestion.getTestQuestion().getId())); + assertEquals(TEST_ANSWERS.stream().filter(a -> a.getQstCod() == answersQuestion.getTestQuestion().getId()).collect(Collectors.toList()), + answersQuestion.getTestAnswers()); + } + + @Test + public void findAllCourseQuestionsByTagAndAnswerTypeOrderByRandomAndAnswerIndex() { + int crsCod = 1; + List tags = TEST_TAGS.stream().map(TestTag::getTagTxt).collect(Collectors.toList()); + List answerTypes = Arrays.asList(TestAnswer.TYPE_UNIQUE_CHOICE, TestAnswer.TYPE_MULTIPLE_CHOICE); + List questions = TEST_QUESTIONS.stream().filter(t -> t.getCrsCod() == crsCod).map(TestQuestion::getId).collect(Collectors.toList()); + TestAnswersQuestion answersQuestion = testQuestionDao.findAllCourseQuestionsByTagAndAnswerTypeOrderByRandomAndAnswerIndex(crsCod, tags, answerTypes, 1).get(0); + + answersQuestion.getTestAnswers().sort(testAnswerComparator); + + assertTrue(questions.contains(answersQuestion.getTestQuestion().getId())); + assertEquals(TEST_ANSWERS.stream().filter(a -> a.getQstCod() == answersQuestion.getTestQuestion().getId()).collect(Collectors.toList()), + answersQuestion.getTestAnswers()); + } + + @Test + public void insertTestQuestion() { + List all = testQuestionDao.findAll(); + List expected = TEST_QUESTIONS; + expected.sort(testQuestionComparator); + assertEquals(expected, all); + } + + @Test + public void updateTestQuestion() { + TestQuestion testQuestion = new TestQuestion(1, 1, "Stem1 Modified", "TF", false, "Feedback1"); + testQuestionDao.updateTestQuestion(Collections.singletonList(testQuestion)); + + List modified = testQuestionDao.findAll(); + List expected = new ArrayList<>(TEST_QUESTIONS); + + modified.sort(testQuestionComparator); + expected.sort(testQuestionComparator); + + expected.set(0, testQuestion); + + assertEquals(expected, modified); + } + + @Test + public void deleteTestQuestion() { + List all = testQuestionDao.findAll(); + List expected = new ArrayList<>(TEST_QUESTIONS); + List allSorted = new ArrayList<>(TEST_QUESTIONS); + + expected.sort(testQuestionComparator); + allSorted.sort(testQuestionComparator); + + TestQuestion deleted = expected.remove(1); + + assertEquals(allSorted, all); + + int numDeleted = testQuestionDao.deleteTestQuestion(Collections.singletonList(deleted)); + assertEquals(1, numDeleted); + assertEquals(expected, testQuestionDao.findAll()); + } + + @Test + public void deleteAll() { + List all = testQuestionDao.findAll(); + List expected = new ArrayList<>(TEST_QUESTIONS); + expected.sort(testQuestionComparator); + assertEquals(expected, all); + + int numDeleted = testQuestionDao.deleteAll(); + assertEquals(9, numDeleted); + assertEquals(Collections.EMPTY_LIST, testQuestionDao.findAll()); + } +} \ No newline at end of file diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestTagDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestTagDaoTest.java new file mode 100644 index 00000000..69694b98 --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestTagDaoTest.java @@ -0,0 +1,82 @@ +package es.ugr.swad.swadroid.dao; + +import static junit.framework.TestCase.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import es.ugr.swad.swadroid.model.Model; +import es.ugr.swad.swadroid.model.TestTag; + +public class TestTagDaoTest extends DaoFixtureTest { + + private final Comparator compareById = Comparator.comparingLong(Model::getId); + + @Before + public void setUp() { + testTagDao.insertTestTag(TEST_TAGS); + } + + @Test + public void findAll() { + List all = testTagDao.findAll(); + List expected = TEST_TAGS; + expected.sort(compareById); + assertEquals(expected, all); + } + + @Test + public void insertTestTag() { + List all = testTagDao.findAll(); + List expected = TEST_TAGS; + expected.sort(compareById); + assertEquals(expected, all); + } + + @Test + public void updateTestTag() { + TestTag TestTag = new TestTag(1, "Tag 1 Modified"); + testTagDao.updateTestTag(Collections.singletonList(TestTag)); + + List modified = testTagDao.findAll(); + List expected = Arrays.asList( + TestTag, + TEST_TAGS.get(0), + TEST_TAGS.get(2) + ); + + assertEquals(expected, modified); + } + + @Test + public void deleteTestTag() { + List all = testTagDao.findAll(); + List expected = TEST_TAGS.subList(1, 3); + List allSorted = TEST_TAGS; + + expected.sort(compareById); + allSorted.sort(compareById); + assertEquals(allSorted, all); + + int numDeleted = testTagDao.deleteTestTag(Collections.singletonList(TEST_TAGS.get(0))); + assertEquals(1, numDeleted); + assertEquals(expected, testTagDao.findAll()); + } + + @Test + public void deleteAll() { + List all = testTagDao.findAll(); + List expected = TEST_TAGS; + expected.sort(compareById); + assertEquals(expected, all); + + int numDeleted = testTagDao.deleteAll(); + assertEquals(3, numDeleted); + assertEquals(Collections.EMPTY_LIST, testTagDao.findAll()); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestTagsQuestionsDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestTagsQuestionsDaoTest.java new file mode 100644 index 00000000..e6f5afd1 --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/TestTagsQuestionsDaoTest.java @@ -0,0 +1,75 @@ +package es.ugr.swad.swadroid.dao; + +import static junit.framework.TestCase.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import es.ugr.swad.swadroid.model.TestTagsQuestions; + +public class TestTagsQuestionsDaoTest extends DaoFixtureTest { + + @Before + public void setUp() { + courseDao.insertCourses(COURSES); + testTagDao.insertTestTag(TEST_TAGS); + testQuestionDao.insertTestQuestion(TEST_QUESTIONS); + testTagsQuestionsDao.insertTestTagQuestion(TEST_TAGS_QUESTIONS); + testAnswerDao.insertTestAnswer(TEST_ANSWERS); + } + + @Test + public void findAll() { + List all = testTagsQuestionsDao.findAll(); + assertEquals(TEST_TAGS_QUESTIONS, all); + } + + @Test + public void insertTestTagsQuestions() { + List all = testTagsQuestionsDao.findAll(); + assertEquals(TEST_TAGS_QUESTIONS, all); + } + + @Test + public void updateTestTagsQuestions() { + TestTagsQuestions testTagQuestions = new TestTagsQuestions(2, 2, 2); + testTagsQuestionsDao.updateTestTagQuestion(Collections.singletonList(testTagQuestions)); + + List modified = testTagsQuestionsDao.findAll(); + List expected = new ArrayList<>(TEST_TAGS_QUESTIONS); + + expected.set(1, testTagQuestions); + + assertEquals(expected, modified); + } + + @Test + public void deleteTestTagsQuestions() { + List all = testTagsQuestionsDao.findAll(); + List expected = new ArrayList<>(TEST_TAGS_QUESTIONS); + List allSorted = new ArrayList<>(TEST_TAGS_QUESTIONS); + + TestTagsQuestions deleted = expected.remove(1); + + assertEquals(allSorted, all); + + int numDeleted = testTagsQuestionsDao.deleteTestTagQuestion(Collections.singletonList(deleted)); + assertEquals(1, numDeleted); + assertEquals(expected, testTagsQuestionsDao.findAll()); + } + + @Test + public void deleteAll() { + List all = testTagsQuestionsDao.findAll(); + List expected = new ArrayList<>(TEST_TAGS_QUESTIONS); + assertEquals(expected, all); + + int numDeleted = testTagsQuestionsDao.deleteAll(); + assertEquals(9, numDeleted); + assertEquals(Collections.EMPTY_LIST, testTagsQuestionsDao.findAll()); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/UserAttendanceDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/UserAttendanceDaoTest.java new file mode 100644 index 00000000..ac41a296 --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/UserAttendanceDaoTest.java @@ -0,0 +1,99 @@ +package es.ugr.swad.swadroid.dao; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import es.ugr.swad.swadroid.model.UserAttendance; + +public class UserAttendanceDaoTest extends DaoFixtureTest { + + @Before + public void setUp() { + courseDao.insertCourses(COURSES); + eventDao.insertEvents(EVENTS); + userDao.insertUsers(USERS); + userAttendanceDao.insertAttendances(ATTENDANCES); + } + + @Test + public void findAll() { + List all = userAttendanceDao.findAll(); + assertEquals(ATTENDANCES, all); + } + + @Test + public void findByEventCode() { + List attendances = userAttendanceDao.findByEventCode(EVENTS.get(1).getId()); + List expected = Collections.singletonList( + new UserAttendance(1, 2, false) + ); + + assertEquals(expected, attendances); + } + + @Test + public void findByUserCodeAndEventCode() { + UserAttendance attendance = userAttendanceDao.findByUserCodeAndEventCode(USERS.get(0).getId(), EVENTS.get(1).getId()); + UserAttendance expected = new UserAttendance(1, 2, false); + assertEquals(expected, attendance); + } + + @Test + public void insertAttendances() { + List all = userAttendanceDao.findAll(); + assertEquals(ATTENDANCES, all); + } + + @Test + public void updateAttendances() { + UserAttendance attendance = new UserAttendance(2, 1, true); + userAttendanceDao.updateAttendances(Collections.singletonList(attendance)); + + List modified = userAttendanceDao.findAll(); + List expected = Arrays.asList( + new UserAttendance(1, 2, false), + attendance, + new UserAttendance(3, 3, false) + ); + + assertEquals(expected, modified); + } + + @Test + public void deleteAttendances() { + List all = userAttendanceDao.findAll(); + List expected = ATTENDANCES.subList(1, 3); + assertEquals(ATTENDANCES, all); + + int numDeleted = userAttendanceDao.deleteAttendances(Collections.singletonList(ATTENDANCES.get(0))); + assertEquals(1, numDeleted); + assertEquals(expected, userAttendanceDao.findAll()); + } + + @Test + public void deleteAttendancesByEventCode() { + List all = userAttendanceDao.findAll(); + List expected = ATTENDANCES.subList(1, 3); + assertEquals(ATTENDANCES, all); + + int numDeleted = userAttendanceDao.deleteAttendancesByEventCode(EVENTS.get(1).getId()); + assertEquals(1, numDeleted); + assertEquals(expected, userAttendanceDao.findAll()); + } + + @Test + public void deleteAll() { + List all = userAttendanceDao.findAll(); + assertEquals(ATTENDANCES, all); + + int numDeleted = userAttendanceDao.deleteAll(); + assertEquals(3, numDeleted); + assertEquals(Collections.EMPTY_LIST, userAttendanceDao.findAll()); + } +} diff --git a/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/UserDaoTest.java b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/UserDaoTest.java new file mode 100644 index 00000000..29464054 --- /dev/null +++ b/SWADroid/src/androidTest/java/es/ugr/swad/swadroid/dao/UserDaoTest.java @@ -0,0 +1,118 @@ +package es.ugr.swad.swadroid.dao; + +import static org.junit.Assert.assertEquals; + +import android.database.Cursor; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import es.ugr.swad.swadroid.model.User; + +public class UserDaoTest extends DaoFixtureTest { + + @Before + public void setUp() { + courseDao.insertCourses(COURSES); + eventDao.insertEvents(EVENTS); + userDao.insertUsers(USERS); + userAttendanceDao.insertAttendances(ATTENDANCES); + } + + @Test + public void findAll() { + List all = userDao.findAll(); + assertEquals(USERS, all); + } + + @Test + public void findAllByEventCodeCursor() { + Cursor cursor = userDao.findAllByEventCodeCursor(EVENTS.get(0).getId()); + List ordered = new ArrayList<>(); + List expected = Collections.singletonList( + new User(2, "wsKey", "userID1", "userNickname1", "userSurname1", "userSurname2", "userFirstname", "userPhoto", "20210101", 0) + ); + + int idColNum = cursor.getColumnIndexOrThrow("id"); + int wsKeyColNum = cursor.getColumnIndexOrThrow("wsKey"); + int userIDColNum = cursor.getColumnIndexOrThrow("userID"); + int userNicknameColNum = cursor.getColumnIndexOrThrow("userNickname"); + int userSurname1ColNum = cursor.getColumnIndex("userSurname1"); + int userSurname2ColNum = cursor.getColumnIndex("userSurname2"); + int userFirstNameColNum = cursor.getColumnIndex("userFirstname"); + int userPhotoColNum = cursor.getColumnIndex("userPhoto"); + int userBirthdayColNum = cursor.getColumnIndex("userBirthday"); + int userRoleColNum = cursor.getColumnIndex("userRole"); + + while (cursor.moveToNext()) { + long id = cursor.getLong(idColNum); + String wsKey = cursor.getString(wsKeyColNum); + String userID = cursor.getString(userIDColNum); + String userNickname = cursor.getString(userNicknameColNum); + String userSurname1 = cursor.getString(userSurname1ColNum); + String userSurname2 = cursor.getString(userSurname2ColNum); + String userFirstName = cursor.getString(userFirstNameColNum); + String userPhoto = cursor.getString(userPhotoColNum); + String userBirthday = cursor.getString(userBirthdayColNum); + int userRole = cursor.getInt(userRoleColNum); + ordered.add(new User(id, wsKey, userID, userNickname, userSurname1, userSurname2, userFirstName, userPhoto, userBirthday, userRole)); + } + + assertEquals(expected, ordered); + } + + @Test + public void findUserByUserNickname() { + User user = userDao.findUserByUserNickname(USERS.get(0).getUserNickname()); + User expected = USERS.get(0); + + assertEquals(expected, user); + } + + @Test + public void insertUsers() { + List all = userDao.findAll(); + assertEquals(USERS, all); + } + + @Test + public void updateUsers() { + User user = new User(2, "wsKey", "userID1 Modified", "userNickname1", "userSurname1", "userSurname2", "userFirstname", "userPhoto", "20210101", 0); + userDao.updateUsers(Collections.singletonList(user)); + + List modified = userDao.findAll(); + List expected = Arrays.asList( + new User(1, "wsKey", "userID2", "userNickname2", "userSurname1", "userSurname2", "userFirstname", "userPhoto", "20210101", 0), + user, + new User(3, "wsKey", "userID3", "userNickname3", "userSurname1", "userSurname2", "userFirstname", "userPhoto", "20210101", 0) + ); + + assertEquals(expected, modified); + } + + @Test + public void deleteUsers() { + List all = userDao.findAll(); + List expected = USERS.subList(1, 3); + assertEquals(USERS, all); + + int numDeleted = userDao.deleteUsers(Collections.singletonList(USERS.get(0))); + assertEquals(1, numDeleted); + assertEquals(expected, userDao.findAll()); + } + + @Test + public void deleteAll() { + List all = userDao.findAll(); + assertEquals(USERS, all); + + int numDeleted = userDao.deleteAll(); + assertEquals(3, numDeleted); + assertEquals(Collections.EMPTY_LIST, userDao.findAll()); + } +} diff --git a/SWADroid/src/main/AndroidManifest.xml b/SWADroid/src/main/AndroidManifest.xml index 0608166c..f06cc045 100644 --- a/SWADroid/src/main/AndroidManifest.xml +++ b/SWADroid/src/main/AndroidManifest.xml @@ -1,6 +1,5 @@