|
| 1 | +package com.marklogic.javaclient; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | + |
| 6 | +import java.util.Calendar; |
| 7 | +import java.util.TimeZone; |
| 8 | + |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.AfterClass; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.BeforeClass; |
| 13 | +import org.junit.Test; |
| 14 | +import org.skyscreamer.jsonassert.JSONAssert; |
| 15 | + |
| 16 | +import com.fasterxml.jackson.databind.JsonNode; |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | +import com.marklogic.client.DatabaseClient; |
| 19 | +import com.marklogic.client.DatabaseClientFactory; |
| 20 | +import com.marklogic.client.DatabaseClientFactory.Authentication; |
| 21 | +import com.marklogic.client.document.DocumentManager.Metadata; |
| 22 | +import com.marklogic.client.document.DocumentWriteSet; |
| 23 | +import com.marklogic.client.document.JSONDocumentManager; |
| 24 | +import com.marklogic.client.io.Format; |
| 25 | +import com.marklogic.client.io.JacksonHandle; |
| 26 | +import com.marklogic.client.pojo.PojoRepository; |
| 27 | +import com.marklogic.client.pojo.annotation.Id; |
| 28 | + |
| 29 | +/* |
| 30 | + * Purpose : To test the date-time data type. |
| 31 | + * |
| 32 | + */ |
| 33 | + |
| 34 | +public class TestJacksonDateTimeFormat extends BasicJavaClientREST { |
| 35 | + private static String dbName = "TestJacksonDateTimeFormatDB"; |
| 36 | + private static String [] fNames = {"TestJacksonDateTimeFormatDB-1"}; |
| 37 | + private static String restServerName = "REST-Java-Client-API-Server"; |
| 38 | + |
| 39 | + private long artifactId = 1L; |
| 40 | + private static int restPort = 8011; |
| 41 | + private DatabaseClient client; |
| 42 | + |
| 43 | + /* |
| 44 | + * This class is used to test writing and reading date-time |
| 45 | + * Class member expiryDate has been annotated with @Id. |
| 46 | + */ |
| 47 | + public static class SpecialArtifactDateTime { |
| 48 | + @Id |
| 49 | + public Calendar expiryDate; |
| 50 | + |
| 51 | + private String name; |
| 52 | + private long id; |
| 53 | + private Company manufacturer; |
| 54 | + private int inventory; |
| 55 | + |
| 56 | + public Calendar getExpiryDate() { |
| 57 | + return expiryDate; |
| 58 | + } |
| 59 | + public SpecialArtifactDateTime setExpiryDate(Calendar expiryDate) { |
| 60 | + this.expiryDate = expiryDate; return this; |
| 61 | + } |
| 62 | + |
| 63 | + public long getId() { |
| 64 | + return id; |
| 65 | + } |
| 66 | + public SpecialArtifactDateTime setId(long id) { |
| 67 | + this.id= id; return this; |
| 68 | + } |
| 69 | + public String getName() { |
| 70 | + return name; |
| 71 | + } |
| 72 | + public SpecialArtifactDateTime setName(String name) { |
| 73 | + this.name = name; return this; |
| 74 | + } |
| 75 | + public Company getManufacturer() { |
| 76 | + return manufacturer; |
| 77 | + } |
| 78 | + public SpecialArtifactDateTime setManufacturer(Company manufacturer) { |
| 79 | + this.manufacturer= manufacturer; return this; |
| 80 | + } |
| 81 | + public int getInventory() { |
| 82 | + return inventory; |
| 83 | + } |
| 84 | + public SpecialArtifactDateTime setInventory(int inventory) { |
| 85 | + this.inventory= inventory; return this; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /* |
| 90 | + * This class is used to test writing and reading date-time. |
| 91 | + * ObjectMapper's support for date time is used for handle date time formats. |
| 92 | + * Class member name has been annotated with @Id. |
| 93 | + * |
| 94 | + */ |
| 95 | + public static class SpArtifactDateTimeObjMapper { |
| 96 | + |
| 97 | + @Id |
| 98 | + public String name; |
| 99 | + private Calendar expiryDate; |
| 100 | + private long id; |
| 101 | + private Company manufacturer; |
| 102 | + private int inventory; |
| 103 | + |
| 104 | + public Calendar getExpiryDate() { |
| 105 | + return expiryDate; |
| 106 | + } |
| 107 | + public SpArtifactDateTimeObjMapper setExpiryDate(Calendar expiryDate) { |
| 108 | + this.expiryDate = expiryDate; return this; |
| 109 | + } |
| 110 | + |
| 111 | + public long getId() { |
| 112 | + return id; |
| 113 | + } |
| 114 | + public SpArtifactDateTimeObjMapper setId(long id) { |
| 115 | + this.id= id; return this; |
| 116 | + } |
| 117 | + public String getName() { |
| 118 | + return name; |
| 119 | + } |
| 120 | + public SpArtifactDateTimeObjMapper setName(String name) { |
| 121 | + this.name = name; return this; |
| 122 | + } |
| 123 | + public Company getManufacturer() { |
| 124 | + return manufacturer; |
| 125 | + } |
| 126 | + public SpArtifactDateTimeObjMapper setManufacturer(Company manufacturer) { |
| 127 | + this.manufacturer= manufacturer; return this; |
| 128 | + } |
| 129 | + public int getInventory() { |
| 130 | + return inventory; |
| 131 | + } |
| 132 | + public SpArtifactDateTimeObjMapper setInventory(int inventory) { |
| 133 | + this.inventory= inventory; return this; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public long getArtifactId() { |
| 138 | + return artifactId; |
| 139 | + } |
| 140 | + |
| 141 | + public void setArtifactId(long artifactId) { |
| 142 | + this.artifactId = artifactId; |
| 143 | + } |
| 144 | + |
| 145 | + @BeforeClass |
| 146 | + public static void setUpBeforeClass() throws Exception { |
| 147 | + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); |
| 148 | + System.out.println("In setup"); |
| 149 | + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); |
| 150 | + } |
| 151 | + |
| 152 | + @AfterClass |
| 153 | + public static void tearDownAfterClass() throws Exception { |
| 154 | + System.out.println("In tear down" ); |
| 155 | + tearDownJavaRESTServer(dbName, fNames, restServerName); |
| 156 | + } |
| 157 | + @Before |
| 158 | + public void setUp() throws Exception { |
| 159 | + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); |
| 160 | + } |
| 161 | + @After |
| 162 | + public void tearDown() throws Exception { |
| 163 | + // release client |
| 164 | + client.release(); |
| 165 | + } |
| 166 | + |
| 167 | + // Increment and return the current Id. |
| 168 | + public long getOneLongId() { |
| 169 | + long lTemp = getArtifactId(); |
| 170 | + setArtifactId(lTemp + 1); |
| 171 | + return lTemp == 1 ? 1: lTemp; |
| 172 | + } |
| 173 | + |
| 174 | + /* |
| 175 | + * This method is used to test POJO Repository read / search with date time. |
| 176 | + * Test should pass in the date-time field and POJORepository read should handle this request just fine. |
| 177 | + */ |
| 178 | + public SpecialArtifactDateTime setSpecialArtifactWithDateTime(long counter, String specialArtifactName, Calendar datetime) { |
| 179 | + Company acme = new Company(); |
| 180 | + acme.setName(specialArtifactName); |
| 181 | + acme.setWebsite("http://www.acme.com"); |
| 182 | + acme.setLatitude(41.998); |
| 183 | + acme.setLongitude(-87.966); |
| 184 | + SpecialArtifactDateTime cogs = new SpecialArtifactDateTime(); |
| 185 | + cogs.setId(counter); |
| 186 | + cogs.setName(specialArtifactName); |
| 187 | + cogs.setManufacturer(acme); |
| 188 | + cogs.setInventory(1000); |
| 189 | + cogs.setExpiryDate(datetime); |
| 190 | + |
| 191 | + return cogs; |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | + /* |
| 196 | + * This method is used to test POJO Repository read / search with date time in the SpArtifactDateTimeObjMapper name. |
| 197 | + * Test should pass in the Name field and POJORepository read should handle this request just fine. |
| 198 | + * ObjectMapper should handle the date time formats. |
| 199 | + */ |
| 200 | + public SpArtifactDateTimeObjMapper setSpArtifactDateTimeObjMapper(long counter, String specialArtifactName, Calendar datetime) { |
| 201 | + Company acme = new Company(); |
| 202 | + acme.setName(specialArtifactName); |
| 203 | + acme.setWebsite("http://www.acme.com"); |
| 204 | + acme.setLatitude(41.998); |
| 205 | + acme.setLongitude(-87.966); |
| 206 | + SpArtifactDateTimeObjMapper cogs = new SpArtifactDateTimeObjMapper(); |
| 207 | + cogs.setId(counter); |
| 208 | + cogs.setName(specialArtifactName); |
| 209 | + cogs.setManufacturer(acme); |
| 210 | + cogs.setInventory(1000); |
| 211 | + cogs.setExpiryDate(datetime); |
| 212 | + |
| 213 | + return cogs; |
| 214 | + } |
| 215 | + |
| 216 | + /* |
| 217 | + * This method is used when there is a need to validate one read and search with |
| 218 | + * date-time in SpecialArtifactDateTime class |
| 219 | + * |
| 220 | + */ |
| 221 | + public void validateSpecialArtifactDateTime(SpecialArtifactDateTime artifact, String artifactName, long longId, Calendar datetime) { |
| 222 | + System.out.println("Argumnt : " + datetime.toString()); |
| 223 | + System.out.println("Jackson POJO : " + artifact.getExpiryDate().toString()); |
| 224 | + |
| 225 | + assertNotNull("Artifact object should never be Null", artifact); |
| 226 | + assertNotNull("Id should never be Null",artifact.id); |
| 227 | + assertEquals("Id of the object is ",longId, artifact.getId()); |
| 228 | + assertEquals("Name of the object is ",artifactName, artifact.getName()); |
| 229 | + assertEquals("Inventory of the object is ",1000, artifact.getInventory()); |
| 230 | + assertEquals("Company name of the object is ",artifactName, artifact.getManufacturer().getName()); |
| 231 | + assertEquals("Web site of the object is ","http://www.acme.com", artifact.getManufacturer().getWebsite()); |
| 232 | + //Validate the calendar object's field, instead of object or string comparisions. |
| 233 | + assertEquals("Expiry date: MONTH ",datetime.get(Calendar.MONTH), artifact.getExpiryDate().get(Calendar.MONTH)); |
| 234 | + assertEquals("Expiry date: DAY_OF_MONTH ",datetime.get(Calendar.DAY_OF_MONTH), artifact.getExpiryDate().get(Calendar.DAY_OF_MONTH)); |
| 235 | + assertEquals("Expiry date: YEAR ",datetime.get(Calendar.YEAR), artifact.getExpiryDate().get(Calendar.YEAR)); |
| 236 | + assertEquals("Expiry date: HOUR ",datetime.get(Calendar.HOUR), artifact.getExpiryDate().get(Calendar.HOUR)); |
| 237 | + assertEquals("Expiry date: MINUTE ",datetime.get(Calendar.MINUTE), artifact.getExpiryDate().get(Calendar.MINUTE)); |
| 238 | + assertEquals("Expiry date: SECOND ",datetime.get(Calendar.SECOND), artifact.getExpiryDate().get(Calendar.SECOND)); |
| 239 | + assertEquals(-87.966, artifact.getManufacturer().getLongitude(), 0.00); |
| 240 | + assertEquals(41.998, artifact.getManufacturer().getLatitude(), 0.00); |
| 241 | + } |
| 242 | + |
| 243 | + |
| 244 | + /* |
| 245 | + * This method is used when there is a need to validate one read and search with |
| 246 | + * date-time in SpArtifactDateTimeObjMapper class |
| 247 | + * |
| 248 | + */ |
| 249 | + public void validateSpArtifactDateTimeObjMapper(SpArtifactDateTimeObjMapper artifact, String artifactName, long longId, Calendar datetime) { |
| 250 | + System.out.println("Argumnt : " + datetime.toString()); |
| 251 | + System.out.println("Jackson POJO : " + artifact.getExpiryDate().toString()); |
| 252 | + |
| 253 | + assertNotNull("Artifact object should never be Null", artifact); |
| 254 | + assertNotNull("Id should never be Null",artifact.id); |
| 255 | + assertEquals("Id of the object is ",longId, artifact.getId()); |
| 256 | + assertEquals("Name of the object is ",artifactName, artifact.getName()); |
| 257 | + assertEquals("Inventory of the object is ",1000, artifact.getInventory()); |
| 258 | + assertEquals("Company name of the object is ",artifactName, artifact.getManufacturer().getName()); |
| 259 | + assertEquals("Web site of the object is ","http://www.acme.com", artifact.getManufacturer().getWebsite()); |
| 260 | + //Validate the calendar object's field, instead of object or string comparisions. |
| 261 | + assertEquals("Expiry date: MONTH ",datetime.get(Calendar.MONTH), artifact.getExpiryDate().get(Calendar.MONTH)); |
| 262 | + assertEquals("Expiry date: DAY_OF_MONTH ",datetime.get(Calendar.DAY_OF_MONTH), artifact.getExpiryDate().get(Calendar.DAY_OF_MONTH)); |
| 263 | + assertEquals("Expiry date: YEAR ",datetime.get(Calendar.YEAR), artifact.getExpiryDate().get(Calendar.YEAR)); |
| 264 | + assertEquals("Expiry date: HOUR ",datetime.get(Calendar.HOUR), artifact.getExpiryDate().get(Calendar.HOUR)); |
| 265 | + assertEquals("Expiry date: MINUTE ",datetime.get(Calendar.MINUTE), artifact.getExpiryDate().get(Calendar.MINUTE)); |
| 266 | + assertEquals("Expiry date: SECOND ",datetime.get(Calendar.SECOND), artifact.getExpiryDate().get(Calendar.SECOND)); |
| 267 | + assertEquals(-87.966, artifact.getManufacturer().getLongitude(), 0.00); |
| 268 | + assertEquals(41.998, artifact.getManufacturer().getLatitude(), 0.00); |
| 269 | + } |
| 270 | + |
| 271 | + /* |
| 272 | + * To verify that a JSON String with datetime string can be written and read back using Jackson. |
| 273 | + */ |
| 274 | + @Test |
| 275 | + public void testWriteJSONDocsWithDateTimeAsString() throws Exception |
| 276 | + { |
| 277 | + String docId[] = { "/datetime.json" }; |
| 278 | + String jsonDate = new String("{\"expiryDate\": {\"java.util.GregorianCalendar\": \"2014-11-06,13:00\"}}"); |
| 279 | + |
| 280 | + |
| 281 | + JSONDocumentManager docMgr = client.newJSONDocumentManager(); |
| 282 | + docMgr.setMetadataCategories(Metadata.ALL); |
| 283 | + |
| 284 | + DocumentWriteSet writeset = docMgr.newWriteSet(); |
| 285 | + ObjectMapper mapper = new ObjectMapper(); |
| 286 | + |
| 287 | + JacksonHandle jacksonHandle1 = new JacksonHandle(); |
| 288 | + |
| 289 | + JsonNode dateNode = mapper.readTree(jsonDate); |
| 290 | + jacksonHandle1.set(dateNode); |
| 291 | + jacksonHandle1.withFormat(Format.JSON); |
| 292 | + |
| 293 | + writeset.add(docId[0], jacksonHandle1); |
| 294 | + docMgr.write(writeset); |
| 295 | + |
| 296 | + JacksonHandle jacksonhandle = new JacksonHandle(); |
| 297 | + docMgr.read(docId[0], jacksonhandle); |
| 298 | + JSONAssert.assertEquals(jsonDate, jacksonhandle.toString(), true); |
| 299 | + } |
| 300 | + |
| 301 | + /* |
| 302 | + * Purpose : This test is to validate read documents using read(Id) |
| 303 | + * POJO instance @Id field value with date-time. |
| 304 | + */ |
| 305 | + |
| 306 | + @Test |
| 307 | + public void testPOJORepoReadWithDateTime() { |
| 308 | + PojoRepository<SpecialArtifactDateTime,Calendar> pojoReposProducts = client.newPojoRepository(SpecialArtifactDateTime.class, Calendar.class); |
| 309 | + |
| 310 | + //Load object into database |
| 311 | + long longId = this.getOneLongId(); |
| 312 | + // Create a calendar object with GMT Time Zone, since Jackson by default uses it. |
| 313 | + // You can set TZ in ObjectMapper also. |
| 314 | + Calendar calTime = Calendar.getInstance(TimeZone.getTimeZone("GMT")); |
| 315 | + |
| 316 | + String artifactName = new String("Acme"); |
| 317 | + pojoReposProducts.write(this.setSpecialArtifactWithDateTime(longId, artifactName, calTime),"odd","numbers"); |
| 318 | + |
| 319 | + // Validate the artifact read back. |
| 320 | + SpecialArtifactDateTime artifact = pojoReposProducts.read(calTime); |
| 321 | + validateSpecialArtifactDateTime(artifact, artifactName, longId, calTime); |
| 322 | + } |
| 323 | + |
| 324 | + /* |
| 325 | + * Delete using date-time |
| 326 | + */ |
| 327 | + @Test |
| 328 | + public void testPOJORepoDeleteDateTime() { |
| 329 | + PojoRepository<SpecialArtifactDateTime,Calendar> pojoReposProducts = client.newPojoRepository(SpecialArtifactDateTime.class, Calendar.class); |
| 330 | + |
| 331 | + //Load object into database |
| 332 | + long longId = this.getOneLongId(); |
| 333 | + // Create a calendar object with GMT Time Zone, since Jackson by default uses it. |
| 334 | + // You can set TZ in ObjectMapper also. |
| 335 | + Calendar calTime = Calendar.getInstance(TimeZone.getTimeZone("GMT")); |
| 336 | + |
| 337 | + String artifactName = new String("Acme"); |
| 338 | + pojoReposProducts.write(this.setSpecialArtifactWithDateTime(longId, artifactName, calTime),"odd","numbers"); |
| 339 | + |
| 340 | + // Validate the artifact read back. |
| 341 | + SpecialArtifactDateTime artifact = pojoReposProducts.read(calTime); |
| 342 | + validateSpecialArtifactDateTime(artifact, artifactName, longId, calTime); |
| 343 | + |
| 344 | + pojoReposProducts.delete(calTime); |
| 345 | + // Validate the artifact read back. |
| 346 | + long count = pojoReposProducts.count(); |
| 347 | + |
| 348 | + assertEquals("Artifact with calendar as Id found - Delete did not work",0L, count); |
| 349 | + } |
| 350 | + |
| 351 | + @Test |
| 352 | + public void testReadWriteDateTimeObjectWithMapper() { |
| 353 | + PojoRepository<SpArtifactDateTimeObjMapper,String> pojoReposProducts = client.newPojoRepository(SpArtifactDateTimeObjMapper.class, String.class); |
| 354 | + |
| 355 | + //Load object into database |
| 356 | + long longId = this.getOneLongId(); |
| 357 | + // Create a calendar object with GMT Time Zone, since Jackson by default uses it. |
| 358 | + // You can set TZ in ObjectMapper also. |
| 359 | + Calendar calTime = Calendar.getInstance(TimeZone.getTimeZone("GMT")); |
| 360 | + |
| 361 | + String artifactName = new String("Acme"); |
| 362 | + pojoReposProducts.write(this.setSpArtifactDateTimeObjMapper(longId, artifactName, calTime),"odd","numbers"); |
| 363 | + |
| 364 | + // Validate the artifact read back. |
| 365 | + SpArtifactDateTimeObjMapper artifact = pojoReposProducts.read(artifactName); |
| 366 | + validateSpArtifactDateTimeObjMapper(artifact, artifactName, longId, calTime); |
| 367 | + } |
| 368 | + |
| 369 | + // Additional methods usingObjectmapper and File Streams with jacksonDataBind need to be here.artifactDel |
| 370 | +} |
0 commit comments