Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DataBaseAdapterTest.java « database « deck « nextcloud « niedermann « it « java « test « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8e1018b969e1fcfbfc65629845e460c5fd48cf3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package it.niedermann.nextcloud.deck.database;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static java.lang.reflect.Modifier.isProtected;
import static it.niedermann.nextcloud.deck.database.DeckDatabaseTestUtil.createAccount;
import static it.niedermann.nextcloud.deck.database.DeckDatabaseTestUtil.createBoard;
import static it.niedermann.nextcloud.deck.database.DeckDatabaseTestUtil.createCard;
import static it.niedermann.nextcloud.deck.database.DeckDatabaseTestUtil.createStack;
import static it.niedermann.nextcloud.deck.database.DeckDatabaseTestUtil.createUser;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;

import com.google.common.util.concurrent.MoreExecutors;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.concurrent.ExecutorService;

import it.niedermann.nextcloud.deck.TestUtil;
import it.niedermann.nextcloud.deck.model.Card;
import it.niedermann.nextcloud.deck.model.Stack;
import it.niedermann.nextcloud.deck.model.full.FullCard;
import it.niedermann.nextcloud.deck.model.interfaces.IRemoteEntity;

@RunWith(RobolectricTestRunner.class)
public class DataBaseAdapterTest {

    private DeckDatabase db;
    private DataBaseAdapter adapter;

    @Before
    public void createAdapter() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        final var constructor = DataBaseAdapter.class.getDeclaredConstructor(Context.class, DeckDatabase.class, ExecutorService.class, ExecutorService.class);
        if (isProtected(constructor.getModifiers())) {
            constructor.setAccessible(true);
            db = Room
                    .inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), DeckDatabase.class)
                    .allowMainThreadQueries()
                    .build();
            adapter = constructor.newInstance(ApplicationProvider.getApplicationContext(), db, MoreExecutors.newDirectExecutorService(), MoreExecutors.newDirectExecutorService());
        } else {
            throw new RuntimeException("Expected constructor to be protected.");
        }
    }

    @After
    public void closeDb() {
        if (db != null) {
            db.close();
        }
    }

    @Test
    public void testCreate() {
        final var account = createAccount(db.getAccountDao());
        final var user = createUser(db.getUserDao(), account);
        final var board = createBoard(db.getBoardDao(), account, user);
        final var fetchedBoard = adapter.getFullBoardByLocalIdDirectly(account.getId(), board.getLocalId());

        assertEquals(board.getTitle(), fetchedBoard.getBoard().getTitle());
    }

    @Test
    public void testFillSqlWithEntityListValues() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        final var user = createUser(db.getUserDao(), createAccount(db.getAccountDao()));
        final var builder = new StringBuilder();
        final var args = new ArrayList<>(1);
        final var entities = Collections.singletonList(user);

        final var fillSqlWithListValues = DataBaseAdapter.class.getDeclaredMethod("fillSqlWithListValues", StringBuilder.class, Collection.class, List.class);
        fillSqlWithListValues.setAccessible(true);
        fillSqlWithListValues.invoke(adapter, builder, args, entities);
        assertEquals("?", builder.toString());
        assertEquals(user.getLocalId(), ((IRemoteEntity) args.get(0)).getLocalId());
    }

    @Test
    public void testFillSqlWithListValues() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        final var user = createUser(db.getUserDao(), createAccount(db.getAccountDao()));
        final var builder = new StringBuilder();
        final var args = new ArrayList<>(1);
        final long leet = 1337L;
        final var entities = Collections.singletonList(leet);

        final Method fillSqlWithListValues = DataBaseAdapter.class.getDeclaredMethod("fillSqlWithListValues", StringBuilder.class, Collection.class, List.class);
        fillSqlWithListValues.setAccessible(true);
        fillSqlWithListValues.invoke(adapter, builder, args, entities);
        assertEquals("?", builder.toString());
        assertEquals(leet, args.get(0));
    }

    @Test
    public void testFillSqlWithMultipleListValues() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        final var builder = new StringBuilder();
        final var args = new ArrayList<>(2);
        final long leet = 1337L;
        final var entities = List.of(leet, leet + 1);

        final var fillSqlWithListValues = DataBaseAdapter.class.getDeclaredMethod("fillSqlWithListValues", StringBuilder.class, Collection.class, List.class);
        fillSqlWithListValues.setAccessible(true);
        fillSqlWithListValues.invoke(adapter, builder, args, entities);
        assertEquals("?, ?", builder.toString());
        assertEquals(leet, args.get(0));
        assertEquals(leet + 1, args.get(1));
    }

    @Test
    public void testSearchCards() throws InterruptedException {
        final var account = createAccount(db.getAccountDao());
        final var user = createUser(db.getUserDao(), account);
        final var board = createBoard(db.getBoardDao(), account, user);

        final var stack1 = createStack(db.getStackDao(), account, board);
        final var stack2 = createStack(db.getStackDao(), account, board);
        final var card1_1 = createCard(db.getCardDao(), account, stack1, "Foo", "Hello world");
        final var card1_2 = createCard(db.getCardDao(), account, stack1, "Bar", "Hello Bar");
        final var card1_3 = createCard(db.getCardDao(), account, stack2, "Baz", "");
        final var card2_1 = createCard(db.getCardDao(), account, stack2, "Qux", "Hello Foo");
        final var card2_2 = createCard(db.getCardDao(), account, stack2, "Lorem", "Ipsum");

        var result = TestUtil.getOrAwaitValue(adapter.searchCards(account.getId(), board.getLocalId(), "Hello", 3));
        assertEquals(2, result.size());
        assertEquals(2, countCardsOf(result, stack1));
        assertEquals(1, countCardsOf(result, stack2));
        assertTrue(containsCard(result, stack1, card1_1));
        assertTrue(containsCard(result, stack1, card1_2));
        assertTrue(containsCard(result, stack2, card2_1));
    }

    private int countCardsOf(@NonNull Map<Stack, List<FullCard>> map, @NonNull Stack stackToFind) {
        for (final var stack : map.keySet()) {
            if (Objects.equals(stack.getLocalId(), stackToFind.getLocalId())) {
                //noinspection ConstantConditions
                return map.get(stack).size();
            }
        }
        throw new NoSuchElementException();
    }

    private boolean containsCard(@NonNull Map<Stack, List<FullCard>> map, @NonNull Stack stackToFind, @NonNull Card cardToFind) {
        for (final var stack : map.keySet()) {
            if (Objects.equals(stack.getLocalId(), stackToFind.getLocalId())) {
                //noinspection ConstantConditions
                for (final var fullCard : map.get(stack)) {
                    if (Objects.equals(fullCard.getLocalId(), cardToFind.getLocalId())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}