/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
- /*
- * Copyright 2017 Proofpoint, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.proofpoint.audit.testing;
- import com.fasterxml.jackson.annotation.JsonProperty;
- import com.google.auto.value.AutoValue;
- import com.google.common.collect.ImmutableList;
- import com.google.common.collect.ImmutableMap;
- import com.google.inject.Guice;
- import com.google.inject.Injector;
- import com.google.inject.Key;
- import com.google.inject.TypeLiteral;
- import com.proofpoint.audit.AuditLogger;
- import com.proofpoint.json.JsonModule;
- import com.proofpoint.tracetoken.TraceTokenManager;
- import org.testng.annotations.BeforeMethod;
- import org.testng.annotations.Test;
- import static com.proofpoint.audit.AuditLoggerBinder.auditLoggerBinder;
- import static org.testng.Assert.assertEquals;
- public class TestTestingAuditLogModule
- {
- private AuditLogger<TestingRecord> logger;
- private TestingAuditLog auditLog;
- private final TestingRecord record1 = new AutoValue_TestTestingAuditLogModule_TestingRecord("record1");
- private final TestingRecord record2 = new AutoValue_TestTestingAuditLogModule_TestingRecord("record2");
- private final TestingRecord record3 = new AutoValue_TestTestingAuditLogModule_TestingRecord("record3");
- @BeforeMethod
- public void setup()
- {
- TraceTokenManager.clearRequestToken();
- Injector injector = Guice.createInjector(
- new TestingAuditLogModule(),
- new JsonModule(),
- binder -> auditLoggerBinder(binder).bind(TestingRecord.class)
- );
- logger = injector.getInstance(Key.get(new TypeLiteral<AuditLogger<TestingRecord>>() {}));
- auditLog = injector.getInstance(TestingAuditLog.class);
- }
- @Test
- public void testPostSingle()
- {
- logger.audit(record1);
- assertEquals(auditLog.getRecords(), ImmutableList.of(ImmutableMap.of(
- "type", "com.proofpoint.audit.testing.TestTestingAuditLogModule.TestingRecord",
- "value", "record1"))
- );
- }
- @Test
- public void testPostMultiple()
- {
- logger.audit(record1);
- TraceTokenManager.registerRequestToken("token1");
- logger.audit(record2);
- TraceTokenManager.clearRequestToken();
- logger.audit(record3);
- assertEquals(auditLog.getRecords(), ImmutableList.of(
- ImmutableMap.of("type", "com.proofpoint.audit.testing.TestTestingAuditLogModule.TestingRecord",
- "value", "record1"),
- ImmutableMap.of("type", "com.proofpoint.audit.testing.TestTestingAuditLogModule.TestingRecord",
- "traceToken", ImmutableMap.of("id", "token1"),
- "value", "record2"),
- ImmutableMap.of("type", "com.proofpoint.audit.testing.TestTestingAuditLogModule.TestingRecord",
- "value", "record3")
- ));
- }
- @AutoValue
- abstract static class TestingRecord
- {
- @JsonProperty
- public abstract String getValue();
- }
- }