|
3 | 3 |
|
4 | 4 | package software.aws.toolkits.jetbrains.services.cloudwatch.logs
|
5 | 5 |
|
| 6 | +import com.intellij.ui.table.JBTable |
6 | 7 | import com.intellij.util.text.DateFormatUtil
|
| 8 | +import com.intellij.util.text.SyncDateFormat |
| 9 | +import com.intellij.util.ui.ListTableModel |
7 | 10 | import org.assertj.core.api.Assertions.assertThat
|
8 | 11 | import org.junit.Test
|
| 12 | +import org.mockito.kotlin.mock |
| 13 | +import org.mockito.kotlin.whenever |
| 14 | +import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream |
| 15 | +import software.aws.toolkits.jetbrains.services.cloudwatch.logs.editor.LogStreamsDateColumn |
9 | 16 | import software.aws.toolkits.jetbrains.services.cloudwatch.logs.editor.TimeFormatConversion
|
10 | 17 | import java.text.SimpleDateFormat
|
| 18 | +import java.time.LocalDate |
| 19 | +import java.time.LocalTime |
| 20 | +import java.time.ZoneOffset |
| 21 | +import java.time.format.DateTimeFormatter |
| 22 | +import java.util.Locale |
| 23 | +import javax.swing.RowSorter |
| 24 | +import javax.swing.SortOrder |
11 | 25 |
|
12 | 26 | class TableUtilsTest {
|
13 |
| - private val sampleTime: Long = 1621173813000 |
| 27 | + @Test |
| 28 | + fun `test short MDY sorting`() { |
| 29 | + val dateFormat = SimpleDateFormat("M/d/yy", Locale.ENGLISH) |
| 30 | + |
| 31 | + val model = ListTableModel( |
| 32 | + arrayOf(LogStreamsDateColumn(SyncDateFormat(dateFormat))), |
| 33 | + mutableListOf( |
| 34 | + mockLogStream(stringToEpoch("6/11/21", dateFormat)), |
| 35 | + mockLogStream(stringToEpoch("2/1/21", dateFormat)), |
| 36 | + mockLogStream(stringToEpoch("7/11/21", dateFormat)), |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + val table = JBTable(model).also { |
| 41 | + it.rowSorter.sortKeys = listOf(RowSorter.SortKey(0, SortOrder.DESCENDING)) |
| 42 | + } |
| 43 | + |
| 44 | + assertThat(table).satisfies { |
| 45 | + assertThat(it.getValueAt(0, 0)).isEqualTo("7/11/21") |
| 46 | + assertThat(it.getValueAt(1, 0)).isEqualTo("6/11/21") |
| 47 | + assertThat(it.getValueAt(2, 0)).isEqualTo("2/1/21") |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `test medium MDY sorting`() { |
| 53 | + val dateFormat = SimpleDateFormat("MMM d, y", Locale.ENGLISH) |
| 54 | + |
| 55 | + val model = ListTableModel( |
| 56 | + arrayOf(LogStreamsDateColumn(SyncDateFormat(dateFormat))), |
| 57 | + mutableListOf( |
| 58 | + mockLogStream(stringToEpoch("Jun 11, 2021", dateFormat)), |
| 59 | + mockLogStream(stringToEpoch("Feb 1, 2021", dateFormat)), |
| 60 | + mockLogStream(stringToEpoch("Jul 11, 2021", dateFormat)), |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + val table = JBTable(model).also { |
| 65 | + it.rowSorter.sortKeys = listOf(RowSorter.SortKey(0, SortOrder.DESCENDING)) |
| 66 | + } |
| 67 | + |
| 68 | + assertThat(table).satisfies { |
| 69 | + assertThat(it.getValueAt(0, 0)).isEqualTo("Jul 11, 2021") |
| 70 | + assertThat(it.getValueAt(1, 0)).isEqualTo("Jun 11, 2021") |
| 71 | + assertThat(it.getValueAt(2, 0)).isEqualTo("Feb 1, 2021") |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun `test medium DMY sorting`() { |
| 77 | + val dateFormat = SimpleDateFormat("d MMM y", Locale.ENGLISH) |
| 78 | + |
| 79 | + val model = ListTableModel( |
| 80 | + arrayOf(LogStreamsDateColumn(SyncDateFormat(dateFormat))), |
| 81 | + mutableListOf( |
| 82 | + mockLogStream(stringToEpoch("11 Jun 2021", dateFormat)), |
| 83 | + mockLogStream(stringToEpoch("1 Feb 2021", dateFormat)), |
| 84 | + mockLogStream(stringToEpoch("11 Jul 2021", dateFormat)), |
| 85 | + ) |
| 86 | + ) |
| 87 | + |
| 88 | + val table = JBTable(model).also { |
| 89 | + it.rowSorter.sortKeys = listOf(RowSorter.SortKey(0, SortOrder.DESCENDING)) |
| 90 | + } |
| 91 | + |
| 92 | + assertThat(table).satisfies { |
| 93 | + assertThat(it.getValueAt(0, 0)).isEqualTo("11 Jul 2021") |
| 94 | + assertThat(it.getValueAt(1, 0)).isEqualTo("11 Jun 2021") |
| 95 | + assertThat(it.getValueAt(2, 0)).isEqualTo("1 Feb 2021") |
| 96 | + } |
| 97 | + } |
14 | 98 |
|
15 | 99 | @Test
|
16 | 100 | fun `convert epoch time to string date time with seconds included`() {
|
| 101 | + val epochTime = 1621173813000 |
17 | 102 | val showSeconds = true
|
18 |
| - val correctTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(sampleTime) |
19 |
| - val time = TimeFormatConversion.convertEpochTimeToStringDateTime(sampleTime, showSeconds) |
| 103 | + val correctTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(epochTime) |
| 104 | + val time = TimeFormatConversion.convertEpochTimeToStringDateTime(epochTime, showSeconds) |
20 | 105 | assertThat(time).isEqualTo(correctTime)
|
21 | 106 | }
|
22 | 107 |
|
23 | 108 | @Test
|
24 | 109 | fun `convert epoch time to string date time with seconds excluded`() {
|
| 110 | + val epochTime = 1621173813000 |
25 | 111 | val showSeconds = false
|
26 |
| - val correctTime = DateFormatUtil.getDateTimeFormat().format(sampleTime) |
27 |
| - val time = TimeFormatConversion.convertEpochTimeToStringDateTime(sampleTime, showSeconds) |
| 112 | + val correctTime = DateFormatUtil.getDateTimeFormat().format(epochTime) |
| 113 | + val time = TimeFormatConversion.convertEpochTimeToStringDateTime(epochTime, showSeconds) |
28 | 114 | assertThat(time).isEqualTo(correctTime)
|
29 | 115 | }
|
| 116 | + |
| 117 | + private fun stringToEpoch(string: String, formatter: SimpleDateFormat) = |
| 118 | + LocalDate.parse(string, DateTimeFormatter.ofPattern(formatter.toPattern(), Locale.ENGLISH)) |
| 119 | + .atTime(LocalTime.NOON.atOffset(ZoneOffset.UTC)) |
| 120 | + .toInstant() |
| 121 | + .toEpochMilli() |
| 122 | + |
| 123 | + private fun mockLogStream(epoch: Long): LogStream { |
| 124 | + val stream: LogStream = mock() |
| 125 | + whenever(stream.lastEventTimestamp()).thenReturn(epoch) |
| 126 | + |
| 127 | + return stream |
| 128 | + } |
30 | 129 | }
|
0 commit comments