/audit/src/test/java/com/proofpoint/audit/testing/TestTestingAuditLogModule.java

https://github.com/ssindkar/platform · Java · 93 lines · 69 code · 9 blank · 15 comment · 0 complexity · 47b25eb3b08f8156282db5c87df00c3c MD5 · raw file

  1. /*
  2. * Copyright 2017 Proofpoint, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a 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
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.proofpoint.audit.testing;
  17. import com.fasterxml.jackson.annotation.JsonProperty;
  18. import com.google.auto.value.AutoValue;
  19. import com.google.common.collect.ImmutableList;
  20. import com.google.common.collect.ImmutableMap;
  21. import com.google.inject.Guice;
  22. import com.google.inject.Injector;
  23. import com.google.inject.Key;
  24. import com.google.inject.TypeLiteral;
  25. import com.proofpoint.audit.AuditLogger;
  26. import com.proofpoint.json.JsonModule;
  27. import com.proofpoint.tracetoken.TraceTokenManager;
  28. import org.testng.annotations.BeforeMethod;
  29. import org.testng.annotations.Test;
  30. import static com.proofpoint.audit.AuditLoggerBinder.auditLoggerBinder;
  31. import static org.testng.Assert.assertEquals;
  32. public class TestTestingAuditLogModule
  33. {
  34. private AuditLogger<TestingRecord> logger;
  35. private TestingAuditLog auditLog;
  36. private final TestingRecord record1 = new AutoValue_TestTestingAuditLogModule_TestingRecord("record1");
  37. private final TestingRecord record2 = new AutoValue_TestTestingAuditLogModule_TestingRecord("record2");
  38. private final TestingRecord record3 = new AutoValue_TestTestingAuditLogModule_TestingRecord("record3");
  39. @BeforeMethod
  40. public void setup()
  41. {
  42. TraceTokenManager.clearRequestToken();
  43. Injector injector = Guice.createInjector(
  44. new TestingAuditLogModule(),
  45. new JsonModule(),
  46. binder -> auditLoggerBinder(binder).bind(TestingRecord.class)
  47. );
  48. logger = injector.getInstance(Key.get(new TypeLiteral<AuditLogger<TestingRecord>>() {}));
  49. auditLog = injector.getInstance(TestingAuditLog.class);
  50. }
  51. @Test
  52. public void testPostSingle()
  53. {
  54. logger.audit(record1);
  55. assertEquals(auditLog.getRecords(), ImmutableList.of(ImmutableMap.of(
  56. "type", "com.proofpoint.audit.testing.TestTestingAuditLogModule.TestingRecord",
  57. "value", "record1"))
  58. );
  59. }
  60. @Test
  61. public void testPostMultiple()
  62. {
  63. logger.audit(record1);
  64. TraceTokenManager.registerRequestToken("token1");
  65. logger.audit(record2);
  66. TraceTokenManager.clearRequestToken();
  67. logger.audit(record3);
  68. assertEquals(auditLog.getRecords(), ImmutableList.of(
  69. ImmutableMap.of("type", "com.proofpoint.audit.testing.TestTestingAuditLogModule.TestingRecord",
  70. "value", "record1"),
  71. ImmutableMap.of("type", "com.proofpoint.audit.testing.TestTestingAuditLogModule.TestingRecord",
  72. "traceToken", ImmutableMap.of("id", "token1"),
  73. "value", "record2"),
  74. ImmutableMap.of("type", "com.proofpoint.audit.testing.TestTestingAuditLogModule.TestingRecord",
  75. "value", "record3")
  76. ));
  77. }
  78. @AutoValue
  79. abstract static class TestingRecord
  80. {
  81. @JsonProperty
  82. public abstract String getValue();
  83. }
  84. }