aboutsummaryrefslogtreecommitdiff
path: root/app/src/test/kotlin/com/pitchedapps/frost/internal/Internal.kt
blob: 061e7c38f9c03c08f2f7e7a2bf9c47093749677c (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
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.internal

import com.pitchedapps.frost.facebook.FB_USER_MATCHER
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.facebook.get
import com.pitchedapps.frost.facebook.requests.RequestAuth
import com.pitchedapps.frost.facebook.requests.getAuth
import com.pitchedapps.frost.utils.frostJsoup
import io.reactivex.Completable
import org.junit.Assume
import org.junit.Test
import java.io.File
import java.io.FileInputStream
import java.util.Properties
import java.util.concurrent.TimeUnit
import kotlin.reflect.full.starProjectedType
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.fail

/**
 * Created by Allan Wang on 21/12/17.
 */

private const val FILE = "priv.properties"

private val propPaths = arrayOf(FILE, "../$FILE")

val PROPS: Properties by lazy {
    val props = Properties()
    val file = propPaths.map(::File).firstOrNull { it.isFile }
    if (file == null) {
        println("$FILE not found at ${File(".").absolutePath}")
        return@lazy props
    }
    println("Found properties at ${file.absolutePath}")
    FileInputStream(file).use { props.load(it) }
    props
}

val COOKIE: String by lazy { PROPS.getProperty("COOKIE") ?: "" }
val USER_ID: Long by lazy { FB_USER_MATCHER.find(COOKIE)[1]?.toLong() ?: -1 }
val AUTH: RequestAuth by lazy {
    COOKIE.getAuth().apply {
        println("Auth:\nuser:$userId\nfb_dtsg: $fb_dtsg\nrev: $rev\ncomplete: $isComplete")
    }
}

val VALID_COOKIE: Boolean by lazy {
    val data = testJsoup(FbItem.SETTINGS.url)
    data.title() == "Settings"
}

fun testJsoup(url: String) = frostJsoup(COOKIE, url)

fun authDependent() {
    println("Auth Dependent")
    Assume.assumeTrue(COOKIE.isNotEmpty() && VALID_COOKIE)
}

/**
 * Check that component strings are nonempty and are properly parsed
 * To be used for data classes
 */
fun Any.assertComponentsNotEmpty() {
    val components = this::class.members.filter { it.name.startsWith("component") }
    if (components.isEmpty())
        fail("${this::class.simpleName} has no components")
    components.forEach {
        when (it.returnType) {
            String::class.starProjectedType -> {
                val result = it.call(this) as String
                assertTrue(result.isNotEmpty(), "${it.name} returned empty string")
                if (result.startsWith("https"))
                    assertTrue(result.startsWith("https://"), "${it.name} has poorly formatted output $result")
            }
        }
    }
}

fun <T : Comparable<T>> List<T>.assertDescending(tag: String) {
    assertEquals(sortedDescending(), this, "$tag not sorted in descending order")
}

interface CompletableCallback {
    fun onComplete()
    fun onError(message: String)
}

inline fun concurrentTest(crossinline caller: (callback: CompletableCallback) -> Unit) {
    val result = Completable.create { emitter ->
        caller(object : CompletableCallback {
            override fun onComplete() = emitter.onComplete()
            override fun onError(message: String) = emitter.onError(Throwable(message))
        })
    }.blockingGet(5, TimeUnit.SECONDS)
    if (result != null)
        throw RuntimeException("Concurrent fail: ${result.message}")
}

class InternalTest {
    @Test
    fun concurrentTest() = try {
        concurrentTest { result ->
            Thread().run {
                Thread.sleep(100)
                result.onError("Intentional fail")
            }
        }
        fail("Did not throw exception")
    } catch (e: Exception) {
        // pass
    }
}