|
| 1 | +package org.javawebstack.orm.test.other; |
| 2 | + |
| 3 | +import lombok.AllArgsConstructor; |
| 4 | +import lombok.Getter; |
| 5 | +import org.apache.commons.lang3.RandomStringUtils; |
| 6 | +import org.javawebstack.orm.test.shared.util.QueryStringUtil; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.util.*; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 12 | + |
| 13 | + |
| 14 | +/* |
| 15 | + * The scope of this class is to test the QueryUtility as it will be used to write other tests. |
| 16 | + */ |
| 17 | +class QueryUtilTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + void testGetSectionSimple() { |
| 21 | + List<SectionRecord> list = new LinkedList<>(Arrays.asList( |
| 22 | + new SectionRecord("SELECT", RandomStringUtils.randomAlphanumeric(3, 12)), |
| 23 | + new SectionRecord("FROM", RandomStringUtils.randomAlphanumeric(3, 10)), |
| 24 | + new SectionRecord("WHERE", RandomStringUtils.randomAlphanumeric(3, 10)), |
| 25 | + new SectionRecord("ORDER BY", RandomStringUtils.randomAlphanumeric(3, 10)), |
| 26 | + new SectionRecord("GROUP BY", RandomStringUtils.randomAlphanumeric(3, 10)), |
| 27 | + new SectionRecord("HAVING", RandomStringUtils.randomAlphanumeric(3, 10)), |
| 28 | + new SectionRecord("LIMIT", RandomStringUtils.randomNumeric(1, 100)), |
| 29 | + new SectionRecord("OFFSET", RandomStringUtils.randomNumeric(1, 100)) |
| 30 | + )); |
| 31 | + |
| 32 | + this.performStandardTestOnList(list); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testCasingDoesNotMatter() { |
| 38 | + List<SectionRecord> list = new LinkedList<>(Arrays.asList( |
| 39 | + new SectionRecord("select", RandomStringUtils.randomAlphanumeric(3, 12)), |
| 40 | + new SectionRecord("fRom", RandomStringUtils.randomAlphanumeric(3, 10)), |
| 41 | + new SectionRecord("where", RandomStringUtils.randomAlphanumeric(3, 10)), |
| 42 | + new SectionRecord("ordEr by", RandomStringUtils.randomAlphanumeric(3, 10)), |
| 43 | + new SectionRecord("Group By", RandomStringUtils.randomAlphanumeric(3, 10)), |
| 44 | + new SectionRecord("hAvIng", RandomStringUtils.randomAlphanumeric(3, 10)), |
| 45 | + new SectionRecord("limit", RandomStringUtils.randomNumeric(1, 100)), |
| 46 | + new SectionRecord("offset", RandomStringUtils.randomNumeric(1, 100)) |
| 47 | + )); |
| 48 | + |
| 49 | + this.performStandardTestOnList(list); |
| 50 | + } |
| 51 | + |
| 52 | + // Example from here: https://www.freecodecamp.org/news/sql-example/ |
| 53 | + @Test |
| 54 | + void testGetUsualCase() { |
| 55 | + List<SectionRecord> list = new LinkedList<>(Arrays.asList( |
| 56 | + new SectionRecord("SELECT", "`Customers`.`CustomerName`, `Orders`.`OrderID`"), |
| 57 | + new SectionRecord("FROM", "`Customers`"), |
| 58 | + new SectionRecord("FULL OUTER JOIN", "`Orders` ON `Customers`.`CustomerID`=`Orders`.`CustomerID`"), |
| 59 | + new SectionRecord("ORDER BY", "`Customers`.`CustomerName`") |
| 60 | + )); |
| 61 | + |
| 62 | + this.performStandardTestOnList(list); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testTrapExpressionsWhichAreEscaped() { |
| 67 | + List<SectionRecord> list = new LinkedList<>(Arrays.asList( |
| 68 | + new SectionRecord("SELECT", "`FROM`.`SELECT`, `GROUP BY`.`having`"), |
| 69 | + new SectionRecord("FROM", "`order by`"), |
| 70 | + new SectionRecord("WHERE", "`from` LIKE `where` AND 'from' = 'where'"), |
| 71 | + new SectionRecord("FULL OUTER JOIN", "`from` ON `FROM`.`SELECT`=`select`.`from`"), |
| 72 | + new SectionRecord("ORDER BY", "`limit`.`where`") |
| 73 | + )); |
| 74 | + |
| 75 | + this.performStandardTestOnList(list); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testMultipleOccurrences() { |
| 80 | + List<SectionRecord> list = new LinkedList<>(Arrays.asList( |
| 81 | + new SectionRecord("SELECT", RandomStringUtils.randomAlphanumeric(3, 12)), |
| 82 | + new SectionRecord("FROM", RandomStringUtils.randomAlphanumeric(3, 12)), |
| 83 | + new SectionRecord("JOIN", RandomStringUtils.randomAlphanumeric(3, 12)), |
| 84 | + new SectionRecord("JOIN", RandomStringUtils.randomAlphanumeric(3, 12)), |
| 85 | + new SectionRecord("JOIN", RandomStringUtils.randomAlphanumeric(3, 12)) |
| 86 | + )); |
| 87 | + |
| 88 | + String queryString = this.getQueryStringFromList(list); |
| 89 | + QueryStringUtil util = new QueryStringUtil(queryString); |
| 90 | + |
| 91 | + SectionRecord currentRecord = list.get(0); |
| 92 | + assertEquals(currentRecord.getValue(), util.getTopLevelSectionsByKeyword(currentRecord.getKey()).get(0)); |
| 93 | + |
| 94 | + currentRecord = list.get(1); |
| 95 | + assertEquals(currentRecord.getValue(), util.getTopLevelSectionsByKeyword(currentRecord.getKey()).get(0)); |
| 96 | + |
| 97 | + currentRecord = list.get(2); |
| 98 | + assertEquals(currentRecord.getValue(), util.getTopLevelSectionsByKeyword(currentRecord.getKey()).get(0)); |
| 99 | + |
| 100 | + currentRecord = list.get(3); |
| 101 | + assertEquals(currentRecord.getValue(), util.getTopLevelSectionsByKeyword(currentRecord.getKey()).get(1)); |
| 102 | + |
| 103 | + currentRecord = list.get(4); |
| 104 | + assertEquals(currentRecord.getValue(), util.getTopLevelSectionsByKeyword(currentRecord.getKey()).get(2)); |
| 105 | + |
| 106 | + } |
| 107 | + /* |
| 108 | + * Boilerplate Code Reduction Methods |
| 109 | + */ |
| 110 | + |
| 111 | + private String getQueryStringFromList(List<SectionRecord> list) { |
| 112 | + StringBuilder builder = new StringBuilder(); |
| 113 | + |
| 114 | + for(SectionRecord entry : list) |
| 115 | + builder |
| 116 | + .append(entry.getKey()) |
| 117 | + .append(" ") |
| 118 | + .append(entry.getValue()) |
| 119 | + .append(" "); |
| 120 | + |
| 121 | + return builder.toString().trim(); |
| 122 | + } |
| 123 | + |
| 124 | + /* |
| 125 | + * The standard test in this case will be that each section cnly exists once and as defined per list. |
| 126 | + */ |
| 127 | + private void performStandardTestOnList(List<SectionRecord> list) { |
| 128 | + String query = this.getQueryStringFromList(list); |
| 129 | + QueryStringUtil verification = new QueryStringUtil(query); |
| 130 | + |
| 131 | + for (SectionRecord entry : list) { |
| 132 | + List<String> foundSections = verification.getTopLevelSectionsByKeyword(entry.getKey()); |
| 133 | + String firstSection = foundSections.get(0); |
| 134 | + assertEquals(1, foundSections.size(), "More than one section or no section was found, but only one unique section was expected."); |
| 135 | + assertEquals( |
| 136 | + entry.getValue(), |
| 137 | + firstSection, |
| 138 | + String.format( |
| 139 | + "The section name %s has been %s instead of %s.", |
| 140 | + entry.getKey(), |
| 141 | + firstSection, |
| 142 | + entry.getValue() |
| 143 | + ) |
| 144 | + ); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Getter |
| 149 | + @AllArgsConstructor |
| 150 | + static class SectionRecord { |
| 151 | + String key; |
| 152 | + String value; |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments