e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay/*
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay * The contents of this file are subject to the terms of the Common Development and
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay * Distribution License (the License). You may not use this file except in compliance with the
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay * License.
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay *
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay * specific language governing permission and limitations under the License.
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay *
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay * When distributing Covered Software, include this CDDL Header Notice in each file and include
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay * Header, with the fields enclosed by brackets [] replaced by your own identifying
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay * information: "Portions copyright [year] [name of copyright owner]".
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay *
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay * Copyright 2015 ForgeRock AS.
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay */
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemaypackage org.forgerock.openidm.provisioner.impl;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay
1b3e20347b77bd88ff6eb08ca1587ed14ebab90aAlin Briciimport static org.assertj.core.api.Assertions.assertThat;
1b3e20347b77bd88ff6eb08ca1587ed14ebab90aAlin Briciimport static org.forgerock.json.JsonValue.json;
1b3e20347b77bd88ff6eb08ca1587ed14ebab90aAlin Briciimport static org.forgerock.json.JsonValue.object;
1b3e20347b77bd88ff6eb08ca1587ed14ebab90aAlin Briciimport static org.forgerock.json.resource.Responses.newResourceResponse;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemayimport static org.mockito.Matchers.any;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemayimport static org.mockito.Mockito.mock;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemayimport static org.mockito.Mockito.verify;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemayimport static org.mockito.Mockito.when;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemayimport org.forgerock.audit.events.AuditEvent;
b84068e6021ec8a830c26c4494f6e335d1f9c0efBrendan Millerimport org.forgerock.openidm.router.IDMConnectionFactory;
21dcdac963f79c098a5ea1a2c5c5e109429c9786Brendan Millerimport org.forgerock.services.context.Context;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemayimport org.forgerock.json.resource.Connection;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemayimport org.forgerock.json.resource.CreateRequest;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemayimport org.mockito.ArgumentCaptor;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemayimport org.testng.annotations.Test;
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemaypublic class SystemObjectSetServiceTest {
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay @Test
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay public void testAuditScheduledService() throws Exception {
1b3e20347b77bd88ff6eb08ca1587ed14ebab90aAlin Brici // given
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay final SystemObjectSetService systemObjectSetService = new SystemObjectSetService();
b84068e6021ec8a830c26c4494f6e335d1f9c0efBrendan Miller final IDMConnectionFactory connectionFactory = mock(IDMConnectionFactory.class);
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay final Connection connection = mock(Connection.class);
e435bd6f4bb55de339fe53b592ee9c8fd75a23b6Brendan Miller final Context context = mock(Context.class);
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay final AuditEvent auditEvent = mock(AuditEvent.class);
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay final ArgumentCaptor<CreateRequest> argumentCaptor = ArgumentCaptor.forClass(CreateRequest.class);
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay systemObjectSetService.bindConnectionFactory(connectionFactory);
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay
1b3e20347b77bd88ff6eb08ca1587ed14ebab90aAlin Brici // when
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay when(connectionFactory.getConnection()).thenReturn(connection);
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay when(connection.create(any(Context.class), argumentCaptor.capture()))
1b3e20347b77bd88ff6eb08ca1587ed14ebab90aAlin Brici .thenReturn(newResourceResponse("id", "rev", null));
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay when(auditEvent.getValue()).thenReturn(json(object()));
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay
e435bd6f4bb55de339fe53b592ee9c8fd75a23b6Brendan Miller systemObjectSetService.auditScheduledService(context, auditEvent);
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay
1b3e20347b77bd88ff6eb08ca1587ed14ebab90aAlin Brici // then
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay verify(connection).create(any(Context.class), any(CreateRequest.class));
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay assertThat(argumentCaptor.getValue().getContent()).isEqualTo(auditEvent.getValue());
1b3e20347b77bd88ff6eb08ca1587ed14ebab90aAlin Brici assertThat(argumentCaptor.getValue().getResourcePath()).isEqualTo("audit/access");
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay }
e86d0731d2d88f053f4f50ebc7060346a3f7934dJason Lemay}