Add support for multiple unknow message recipients

This commit is contained in:
Mikołaj Pich 2019-01-20 00:25:27 +01:00
parent 038763a968
commit 3537efcd73
3 changed files with 40 additions and 10 deletions

View file

@ -59,7 +59,7 @@ class MessagesRepository(private val api: MessagesService) {
Observable.fromArray(message.recipient!!
.split("; ")
.map { recipient -> recipient.normalizeRecipient() }
.joinBy(it) { (name, recipient) -> name == recipient.name }
.replaceWithRecipients(it)
.ifEmpty {
listOf(Recipient("0", message.recipient.normalizeRecipient(), 0, 0, 2, "unknown"))
})
@ -93,7 +93,7 @@ class MessagesRepository(private val api: MessagesService) {
}
private fun String.normalizeRecipient(): String {
return this.substringBeforeLast(" -").substringBefore(" [")
return this.substringBeforeLast("-").substringBefore(" [").trim()
}
private fun getDate(date: LocalDateTime?): String {
@ -101,9 +101,13 @@ class MessagesRepository(private val api: MessagesService) {
return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
}
private inline fun <T : Any, U : Any> List<T>.joinBy(collection: List<U>, filter: (Pair<T, U>) -> Boolean): List<U> {
return map { t ->
collection.filter { filter(Pair(t, it)) }
private fun List<String>.replaceWithRecipients(recipients: List<Recipient>): List<Recipient> {
return map { origin ->
recipients.filter { recipient ->
origin == recipient.name
}.ifEmpty {
listOf(Recipient("0", origin, 0, 0, 2, "unknown"))
}
}.flatten()
}
}

View file

@ -37,7 +37,7 @@ class MessagesTest : BaseLocalTest() {
server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("Adresaci.json").readText()))
server.start(3000)
assertEquals(5, api.getSentMessages(null, null).blockingGet().size)
assertEquals(6, api.getSentMessages(null, null).blockingGet().size)
}
@Test
@ -47,7 +47,7 @@ class MessagesTest : BaseLocalTest() {
// server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("Adresaci.json").readText()))
server.start(3000)
assertEquals(5, api.getSentMessages(null, null).blockingGet().size)
assertEquals(6, api.getSentMessages(null, null).blockingGet().size)
}
@Test
@ -118,16 +118,34 @@ class MessagesTest : BaseLocalTest() {
val message = api.getSentMessages(null, null).blockingGet()[4]
// assertEquals("Tracz Janusz; Kowalska Joanna", message.recipient)
assertEquals("Tracz Janusz", message.recipient)
assertEquals("Tracz Janusz; Kowalska Joanna", message.recipient)
assertEquals(18, message.recipientId)
val recipients = message.recipients
assertEquals(2, recipients.size)
assertEquals("Tracz Janusz", recipients[0].name)
assertEquals(18, recipients[0].loginId)
}
assertEquals(1, recipients.size)
@Test
fun getMessagesSent_multiRecipientsWithAllBroken() {
server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("WiadomosciWyslane.json").readText()))
server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("JednostkiUzytkownika.json").readText()))
server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("Adresaci.json").readText()))
server.start(3000)
api.getSentMessages(null, null).blockingGet()[5].run {
assertEquals("Tracz Antoni (TA); Kowalska Joanna", recipient)
assertEquals(0, recipientId)
assertEquals(2, recipients.size)
assertEquals("Tracz Antoni (TA)", recipients[0].name)
assertEquals(0, recipients[0].loginId)
assertEquals("Kowalska Joanna", recipients[1].name)
assertEquals(0, recipients[1].loginId)
}
}
@Test

View file

@ -40,6 +40,14 @@
"Nieprzeczytane": "1",
"Przeczytane": "0",
"Id": 32801
},
{
"Data": "2018-06-11T09:38:35.000Z",
"Temat": "Wiadomość tekstowa",
"Adresaci": "Tracz Antoni (TA)- pracownik (Fake123456); Kowalska Joanna - pracownik (Fake123456)",
"Nieprzeczytane": "1",
"Przeczytane": "0",
"Id": 32801
}
]
}