|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license |
| 3 | + * agreements. See the NOTICE file distributed with this work for additional information regarding |
| 4 | + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the |
| 5 | + * "License"); you may not use this file except in compliance with the License. You may obtain a |
| 6 | + * copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 11 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 12 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 13 | + * the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package org.apache.geode.management.internal.beans; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.text.SimpleDateFormat; |
| 21 | +import java.time.LocalDate; |
| 22 | +import java.util.Date; |
| 23 | + |
| 24 | +import org.junit.BeforeClass; |
| 25 | +import org.junit.ClassRule; |
| 26 | +import org.junit.Rule; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import org.apache.geode.cache.Region; |
| 30 | +import org.apache.geode.cache.RegionShortcut; |
| 31 | +import org.apache.geode.management.DistributedSystemMXBean; |
| 32 | +import org.apache.geode.management.internal.json.QueryResultFormatter; |
| 33 | +import org.apache.geode.test.junit.assertions.TabularResultModelAssert; |
| 34 | +import org.apache.geode.test.junit.rules.GfshCommandRule; |
| 35 | +import org.apache.geode.test.junit.rules.MBeanServerConnectionRule; |
| 36 | +import org.apache.geode.test.junit.rules.ServerStarterRule; |
| 37 | + |
| 38 | +public class DistributedSystemMBeanIntegrationTest { |
| 39 | + |
| 40 | + public static final String SELECT = "select * from /testRegion r where r.id=1"; |
| 41 | + |
| 42 | + @ClassRule |
| 43 | + public static ServerStarterRule server = new ServerStarterRule() |
| 44 | + .withNoCacheServer() |
| 45 | + .withJMXManager() |
| 46 | + .withRegion(RegionShortcut.REPLICATE, "testRegion") |
| 47 | + .withAutoStart(); |
| 48 | + |
| 49 | + @Rule |
| 50 | + public MBeanServerConnectionRule connectionRule = |
| 51 | + new MBeanServerConnectionRule(server::getJmxPort); |
| 52 | + |
| 53 | + @Rule |
| 54 | + public GfshCommandRule gfsh = new GfshCommandRule(); |
| 55 | + |
| 56 | + private static Date date; |
| 57 | + private static java.sql.Date sqlDate; |
| 58 | + private static LocalDate localDate; |
| 59 | + |
| 60 | + @BeforeClass |
| 61 | + public static void setupClass() throws Exception { |
| 62 | + Region<Object, Object> testRegion = server.getCache().getRegion("testRegion"); |
| 63 | + localDate = LocalDate.of(2020, 1, 1); |
| 64 | + sqlDate = java.sql.Date.valueOf(localDate); |
| 65 | + date = new Date(sqlDate.getTime()); |
| 66 | + Data data = new Data(1, date, sqlDate, localDate); |
| 67 | + testRegion.put(1, data); |
| 68 | + } |
| 69 | + |
| 70 | + // this is to make sure dates are formatted correctly |
| 71 | + @Test |
| 72 | + public void queryUsingMBean() throws Exception { |
| 73 | + connectionRule.connect(server.getJmxPort()); |
| 74 | + SimpleDateFormat formater = |
| 75 | + new SimpleDateFormat(QueryResultFormatter.DATE_FORMAT_PATTERN); |
| 76 | + String dateString = formater.format(date); |
| 77 | + DistributedSystemMXBean bean = connectionRule.getProxyMXBean(DistributedSystemMXBean.class); |
| 78 | + String result = bean.queryData(SELECT, "server", 100); |
| 79 | + System.out.println(result); |
| 80 | + assertThat(result) |
| 81 | + .contains("\"java.util.Date\",\"" + dateString + "\"") |
| 82 | + .contains("\"java.sql.Date\",\"" + dateString + "\"") |
| 83 | + .contains("\"java.time.LocalDate\",\"2020-01-01\""); |
| 84 | + } |
| 85 | + |
| 86 | + // this is simply to document the current behavior of gfsh |
| 87 | + // gfsh doesn't attempt tp format the date objects as of now |
| 88 | + @Test |
| 89 | + public void queryUsingGfsh() throws Exception { |
| 90 | + gfsh.connectAndVerify(server.getJmxPort(), GfshCommandRule.PortType.jmxManager); |
| 91 | + TabularResultModelAssert tabularAssert = |
| 92 | + gfsh.executeAndAssertThat("query --query='" + SELECT + "'") |
| 93 | + .statusIsSuccess() |
| 94 | + .hasTableSection(); |
| 95 | + tabularAssert.hasColumn("id").containsExactly("1"); |
| 96 | + tabularAssert.hasColumn("date").containsExactly(date.getTime() + ""); |
| 97 | + tabularAssert.hasColumn("sqlDate").containsExactly(sqlDate.getTime() + ""); |
| 98 | + tabularAssert.hasColumn("localDate") |
| 99 | + .asList().asString().contains("\"year\":2020,\"month\":\"JANUARY\""); |
| 100 | + } |
| 101 | + |
| 102 | + public static class Data { |
| 103 | + private int id; |
| 104 | + private Date date; |
| 105 | + private java.sql.Date sqlDate; |
| 106 | + private LocalDate localDate; |
| 107 | + |
| 108 | + public Data() {} |
| 109 | + |
| 110 | + public Data(int id, Date date, java.sql.Date sqlDate, LocalDate localDate) { |
| 111 | + this.id = id; |
| 112 | + this.date = date; |
| 113 | + this.sqlDate = sqlDate; |
| 114 | + this.localDate = localDate; |
| 115 | + } |
| 116 | + |
| 117 | + public int getId() { |
| 118 | + return id; |
| 119 | + } |
| 120 | + |
| 121 | + public void setId(int id) { |
| 122 | + this.id = id; |
| 123 | + } |
| 124 | + |
| 125 | + public Date getDate() { |
| 126 | + return date; |
| 127 | + } |
| 128 | + |
| 129 | + public void setDate(Date date) { |
| 130 | + this.date = date; |
| 131 | + } |
| 132 | + |
| 133 | + public java.sql.Date getSqlDate() { |
| 134 | + return sqlDate; |
| 135 | + } |
| 136 | + |
| 137 | + public void setSqlDate(java.sql.Date sqlDate) { |
| 138 | + this.sqlDate = sqlDate; |
| 139 | + } |
| 140 | + |
| 141 | + public LocalDate getLocalDate() { |
| 142 | + return localDate; |
| 143 | + } |
| 144 | + |
| 145 | + public void setLocalDate(LocalDate localDate) { |
| 146 | + this.localDate = localDate; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments