X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=mappingservice%2Fimplementation%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Flispflowmapping%2Fimplementation%2Flisp%2FMappingServiceTest.java;h=eb62809e6c4bff2a64386e4e608f743e73b0a42d;hb=ba581e20c92a17643f5fe36f10e1d025917c5860;hp=08dc14739173ac45f6182c49a62dbdc1fac1c4a5;hpb=699cbe9463c89dbac5b12bdff13ed9a2d4b2e996;p=lispflowmapping.git diff --git a/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MappingServiceTest.java b/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MappingServiceTest.java index 08dc14739..eb62809e6 100644 --- a/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MappingServiceTest.java +++ b/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MappingServiceTest.java @@ -1,145 +1,577 @@ /* - * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.lispflowmapping.implementation.lisp; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; -import java.util.Arrays; - -import org.jmock.api.Invocation; -import org.junit.Before; -import org.junit.Ignore; +import java.util.ArrayList; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; import org.opendaylight.lispflowmapping.implementation.MappingService; import org.opendaylight.lispflowmapping.implementation.MappingSystem; -import org.opendaylight.lispflowmapping.implementation.mdsal.DataStoreBackEnd; -import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO; -import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry; +import org.opendaylight.lispflowmapping.implementation.mdsal.AuthenticationKeyDataListener; +import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd; +import org.opendaylight.lispflowmapping.implementation.mdsal.MappingDataListener; +import org.opendaylight.lispflowmapping.implementation.util.DSBEInputUtil; +import org.opendaylight.lispflowmapping.implementation.util.RPCInputConvertorUtil; import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys; import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil; -import org.opendaylight.lispflowmapping.lisp.util.MaskUtil; -import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container + .MappingRecordBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeysInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingsInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeysInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingOutputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin; -import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey; -import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey; -import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkeyBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeysInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingsInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateKeyInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateKeyInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateKeysInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingsInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container + .MappingAuthkey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container + .MappingAuthkeyBuilder; +import org.opendaylight.yangtools.yang.common.RpcError; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -/** - * - * @author Florin Coras - * - */ -public class MappingServiceTest extends BaseTestCase { +@RunWith(MockitoJUnitRunner.class) +public class MappingServiceTest { - private ILispDAO dao; - private MappingService mapService; - private DataStoreBackEnd dsbe; - private MappingSystem mapSystem; + @InjectMocks private static MappingService mappingService; + @Mock(name = "mappingSystem") private static MappingSystem mappingSystem; + @Mock(name = "dsbe") private static DataStoreBackEnd dsbe; + @Mock(name = "mappingServiceRpc") private static BindingAwareBroker + .RpcRegistration mappingServiceRpc; + @Mock(name = "keyListener") private static AuthenticationKeyDataListener keyListener; + @Mock(name = "mappingListener") private static MappingDataListener mappingListener; - private Eid eid; - private String eidIpv4String = "10.31.0.5"; + private static final String IPV4_STRING = "1.2.3.0"; + private static final Eid IPV4_EID = LispAddressUtil.asIpv4Eid(IPV4_STRING); - @Override - @Before - public void before() throws Exception { - super.before(); - dao = context.mock(ILispDAO.class); - dsbe = context.mock(DataStoreBackEnd.class); + private static final RpcResult RPC_RESULT_SUCCESS = RpcResultBuilder.success().build(); + private static final MappingAuthkey MAPPING_AUTHKEY = new MappingAuthkeyBuilder() + .setKeyString("password") + .setKeyType(1).build(); + private static final SiteId SITE_ID = new SiteId(new byte[] {0, 1, 2, 3, 4, 5, 6, 7}); + private static final XtrId XTR_ID = new XtrId(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); + private static final Object DUMMY_OBJECT = "dummy_object"; - // map-caches init and table creation - allowing(dao).putTable(with(MappingOrigin.Northbound.toString()));will(returnValue(dao)); - allowing(dao).putTable(with(MappingOrigin.Southbound.toString()));will(returnValue(dao)); + /** + * Tests {@link MappingService#addKey} method. + * + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void addKeyTest() throws ExecutionException, InterruptedException { + final MappingAuthkey mappingAuthkey = new MappingAuthkeyBuilder() + .setKeyString("dummy-password") + .setKeyType(2).build(); + final AddKeyInput addKeyInput = new AddKeyInputBuilder() + .setMappingAuthkey(mappingAuthkey) + .setEid(IPV4_EID).build(); + Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(MAPPING_AUTHKEY); - mapSystem = new MappingSystem(dao, true, true, true); + // input + final RpcResult rpc = RpcResultBuilder.failed().withError(RpcError.ErrorType.PROTOCOL, "data-exists", + "Key already exists! Please use update-key if you want to change it.").build(); + final RpcError error = rpc.getErrors().iterator().next(); // equals() not implemented int RpcError - mapService = new MappingService(); - mapService.setDaoService(dao); - inject(mapService, "dsbe", dsbe); - inject(mapService, "mappingSystem", mapSystem); + // result + final Future> result = mappingService.addKey(addKeyInput); + final RpcError errorResult = result.get().getErrors().iterator().next(); - eid = LispAddressUtil.asIpv4PrefixEid(eidIpv4String + "/32"); + assertEquals(1, result.get().getErrors().size()); + assertEquals(error.getMessage(), errorResult.getMessage()); + assertEquals(error.getApplicationTag(), errorResult.getApplicationTag()); + assertEquals(error.getCause(), errorResult.getCause()); + assertEquals(error.getErrorType(), errorResult.getErrorType()); + assertEquals(error.getInfo(), errorResult.getInfo()); + assertEquals(error.getTag(), errorResult.getTag()); + assertEquals(error.getSeverity(), errorResult.getSeverity()); + assertEquals(rpc.getResult(), result.get().getResult()); + assertEquals(rpc.isSuccessful(), result.get().isSuccessful()); + } + /** + * Tests {@link MappingService#addKey} method with null MappingAuthkey. + * + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void addKeyTest_withNullMapAuthkey() throws ExecutionException, InterruptedException { + final AddKeyInput addKeyInput = new AddKeyInputBuilder().setEid(IPV4_EID).build(); + Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(null); + + final Future> result = mappingService.addKey(addKeyInput); + Mockito.verify(dsbe).addAuthenticationKey(Mockito.any()); + + assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors()); + assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult()); + assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful()); } - @SuppressWarnings("unchecked") + /** + * Tests {@link MappingService#addMapping} method. + * + * @throws ExecutionException + * @throws InterruptedException + */ @Test - @Ignore - public void handleAddAuthenticationKey() throws Exception { - MappingAuthkey authKey = new MappingAuthkeyBuilder().setKeyType(1).setKeyString("pass").build(); - MappingEntry keyMappingEntry = new MappingEntry<>(SubKeys.AUTH_KEY, authKey); - Eid key = getDefaultKey(); - MappingEntry[] authKeys =(MappingEntry[]) - (Arrays.asList(keyMappingEntry).toArray()); - addDsbeAddKeyExpectation(); - oneOf(dao).put(weq(key), weq(authKeys)); - mapService.addAuthenticationKey(eid, authKey); + public void addMappingTest() throws ExecutionException, InterruptedException { + final MappingRecordBuilder mappingRecordBuilder = getDefaultMappingRecordBuilder(); + final AddMappingInput addMappingInput = new AddMappingInputBuilder() + .setMappingRecord(mappingRecordBuilder.build()).build(); + + final Future> result = mappingService.addMapping(addMappingInput); + Mockito.verify(dsbe).addMapping(RPCInputConvertorUtil.toMapping(addMappingInput)); + + assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors()); + assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult()); + assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful()); } + /** + * Tests {@link MappingService#getKey} method. + * + * @throws ExecutionException + * @throws InterruptedException + */ @Test - @Ignore - public void handleGetAuthenticationKey() throws Exception { - Eid key = getDefaultKey(); - oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY)); - ret("password"); - assertEquals("password", mapService.getAuthenticationKey(eid)); + public void getKeyTest() throws ExecutionException, InterruptedException { + final GetKeyInput getKeyInput = new GetKeyInputBuilder().setEid(IPV4_EID).build(); + final RpcResult rpcResult = RpcResultBuilder + .success(new GetKeyOutputBuilder().setMappingAuthkey(MAPPING_AUTHKEY).build()).build(); + + Mockito.when(mappingSystem.getAuthenticationKey(getKeyInput.getEid())).thenReturn(MAPPING_AUTHKEY); + + final Future> result = mappingService.getKey(getKeyInput); + assertEquals(rpcResult.getErrors(), result.get().getErrors()); + assertEquals(rpcResult.getResult(), result.get().getResult()); + assertEquals(rpcResult.isSuccessful(), result.get().isSuccessful()); } + /** + * Tests {@link MappingService#getKey} method with null MappingAuthkey. + * + * @throws ExecutionException + * @throws InterruptedException + */ @Test - public void handleGetAuthenticationKeyNoIteration() throws Exception { - mapSystem.setIterateMask(false); - Eid key = getDefaultKey(); - Eid passKey = getKey(30); - oneOf(dao).getSpecific(weq(key), with(SubKeys.AUTH_KEY)); - allowing(dao).getSpecific(weq(passKey), with(SubKeys.AUTH_KEY)); - ret("password"); - assertEquals(null, mapService.getAuthenticationKey(eid)); + public void getKeyTest_withNullMappingAuthkey() throws ExecutionException, InterruptedException { + // input + final GetKeyInput getKeyInput = new GetKeyInputBuilder().setEid(IPV4_EID).build(); + Mockito.when(mappingSystem.getAuthenticationKey(getKeyInput.getEid())).thenReturn(null); + + final RpcResult rpc = RpcResultBuilder.failed() + .withError(RpcError.ErrorType.APPLICATION, "data-missing", "Key was not found in the mapping database") + .build(); + final RpcError error = rpc.getErrors().iterator().next(); + + // result + final Future> result = mappingService.getKey(getKeyInput); + final RpcError errorResult = result.get().getErrors().iterator().next(); + + assertEquals(1, result.get().getErrors().size()); + assertEquals(error.getMessage(), errorResult.getMessage()); + assertEquals(error.getApplicationTag(), errorResult.getApplicationTag()); + assertEquals(error.getCause(), errorResult.getCause()); + assertEquals(error.getErrorType(), errorResult.getErrorType()); + assertEquals(error.getInfo(), errorResult.getInfo()); + assertEquals(error.getTag(), errorResult.getTag()); + assertEquals(error.getSeverity(), errorResult.getSeverity()); + assertEquals(rpc.getResult(), result.get().getResult()); + assertEquals(rpc.isSuccessful(), result.get().isSuccessful()); } + /** + * Tests {@link MappingService#getMapping} method. + * + * @throws ExecutionException + * @throws InterruptedException + */ @Test - @Ignore - public void handleRemoveAuthenticationKey() throws Exception { - Eid key = getDefaultKey(); - addDsbeRemoveKeyExpectation(); - oneOf(dao).removeSpecific(weq(key), with(SubKeys.AUTH_KEY)); - mapService.removeAuthenticationKey(eid); + public void getMappingTest() throws ExecutionException, InterruptedException { + // input + final GetMappingInput getMappingInput = new GetMappingInputBuilder().setEid(IPV4_EID).build(); + final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build(); + + Mockito.when(mappingSystem.getMapping(getMappingInput.getEid())).thenReturn(mappingRecord); + + final RpcResult rpc = RpcResultBuilder + .success(new GetMappingOutputBuilder().setMappingRecord(mappingRecord)).build(); + + //result + final Future> result = (mappingService.getMapping(getMappingInput)); + final RpcResult rpcResult = result.get(); + + assertEquals(rpc.getResult(), rpcResult.getResult()); + assertEquals(rpc.getErrors(), rpcResult.getErrors()); + assertEquals(rpc.isSuccessful(), rpcResult.isSuccessful()); } - private Eid getDefaultKey() { - return getKey(32); + /** + * Tests {@link MappingService#getMapping} method with null MappingRecord. + * + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void getMappingTest_withNullMapRecord() throws ExecutionException, InterruptedException { + // input + final GetMappingInput getMappingInput = new GetMappingInputBuilder().setEid(IPV4_EID).build(); + Mockito.when(mappingSystem.getMapping(getMappingInput.getEid())).thenReturn(null); + + final RpcResult rpc = RpcResultBuilder.failed().withError(RpcError.ErrorType.APPLICATION, + "data-missing", "No mapping was found in the mapping database").build(); + final RpcError error = rpc.getErrors().iterator().next(); + + //result + final Future> result = (mappingService.getMapping(getMappingInput)); + final RpcError errorResult = result.get().getErrors().iterator().next(); + + assertEquals(1, result.get().getErrors().size()); + assertEquals(error.getMessage(), errorResult.getMessage()); + assertEquals(error.getApplicationTag(), errorResult.getApplicationTag()); + assertEquals(error.getCause(), errorResult.getCause()); + assertEquals(error.getErrorType(), errorResult.getErrorType()); + assertEquals(error.getInfo(), errorResult.getInfo()); + assertEquals(error.getTag(), errorResult.getTag()); + assertEquals(error.getSeverity(), errorResult.getSeverity()); + assertEquals(rpc.getResult(), result.get().getResult()); + assertEquals(rpc.isSuccessful(), result.get().isSuccessful()); } - private Eid getKey(int mask) { - Eid key = LispAddressUtil.asIpv4PrefixEid(eidIpv4String + "/" + Integer.toString(mask)); - return MaskUtil.normalize(key, (short) mask); + /** + * Tests {@link MappingService#removeKey} method. + * + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void removeKeyTest() throws ExecutionException, InterruptedException { + final RemoveKeyInput removeKeyInput = new RemoveKeyInputBuilder().setEid(IPV4_EID).build(); + + final Future> result = mappingService.removeKey(removeKeyInput); + Mockito.verify(dsbe).removeAuthenticationKey(RPCInputConvertorUtil.toAuthenticationKey(removeKeyInput)); + + assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors()); + assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult()); + assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful()); + } + + /** + * Tests {@link MappingService#removeMapping} method. + * + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void removeMappingTest() throws ExecutionException, InterruptedException { + final RemoveMappingInput removeMappingInput = new RemoveMappingInputBuilder().setEid(IPV4_EID).build(); + + final Future> result = mappingService.removeMapping(removeMappingInput); + Mockito.verify(dsbe).removeMapping(RPCInputConvertorUtil.toMapping(removeMappingInput)); + + assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors()); + assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult()); + assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful()); + } + + /** + * Tests {@link MappingService#updateKey} method. + * + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void updateKeyTest() throws ExecutionException, InterruptedException { + final UpdateKeyInput updateKeyInput = new UpdateKeyInputBuilder().setEid(IPV4_EID).build(); + Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(MAPPING_AUTHKEY); + + final Future> result = mappingService.updateKey(updateKeyInput); + Mockito.verify(dsbe).updateAuthenticationKey(RPCInputConvertorUtil.toAuthenticationKey(updateKeyInput)); + + assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors()); + assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult()); + assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful()); } - private void addDsbeAddKeyExpectation() { - ValueSaverAction dsbeAddKeySaverAction = new ValueSaverAction() { - @Override - public Object invoke(Invocation invocation) throws Throwable { - mapSystem.addAuthenticationKey(lastValue.getEid(), lastValue.getMappingAuthkey()); - return null; - } - }; - oneOf(dsbe).addAuthenticationKey(with(dsbeAddKeySaverAction)); will(dsbeAddKeySaverAction); + /** + * Tests {@link MappingService#updateKey} method with null MappingAuthkey. + * + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void updateKeyTest_withNullMapAuthkey() throws ExecutionException, InterruptedException { + final UpdateKeyInput updateKeyInput = new UpdateKeyInputBuilder().setEid(IPV4_EID).build(); + Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(null); + + // input + final RpcResult rpc = RpcResultBuilder.failed().withError(RpcError.ErrorType.PROTOCOL, "data-missing", + "Key doesn't exist! Please use add-key if you want to create a new authentication key.").build(); + final RpcError error = rpc.getErrors().iterator().next(); + + // result + final Future> result = mappingService.updateKey(updateKeyInput); + final RpcError errorResult = result.get().getErrors().iterator().next(); + + assertEquals(1, result.get().getErrors().size()); + assertEquals(error.getMessage(), errorResult.getMessage()); + assertEquals(error.getApplicationTag(), errorResult.getApplicationTag()); + assertEquals(error.getCause(), errorResult.getCause()); + assertEquals(error.getErrorType(), errorResult.getErrorType()); + assertEquals(error.getInfo(), errorResult.getInfo()); + assertEquals(error.getTag(), errorResult.getTag()); + assertEquals(error.getSeverity(), errorResult.getSeverity()); + assertEquals(rpc.getResult(), result.get().getResult()); + assertEquals(rpc.isSuccessful(), result.get().isSuccessful()); } - private void addDsbeRemoveKeyExpectation() { - ValueSaverAction dsbeRemoveKeySaverAction = new ValueSaverAction() { - @Override - public Object invoke(Invocation invocation) throws Throwable { - mapSystem.removeAuthenticationKey(lastValue.getEid()); - return null; - } - }; - oneOf(dsbe).removeAuthenticationKey(with(dsbeRemoveKeySaverAction)); will(dsbeRemoveKeySaverAction); + /** + * Tests {@link MappingService#updateMapping} method. + * + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void updateMappingTest() throws ExecutionException, InterruptedException { + final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build(); + final UpdateMappingInput updateMappingInput = new UpdateMappingInputBuilder() + .setMappingRecord(mappingRecord).build(); + + final Future> result = mappingService.updateMapping(updateMappingInput); + Mockito.verify(dsbe).updateMapping(RPCInputConvertorUtil.toMapping(updateMappingInput)); + + assertEquals(RPC_RESULT_SUCCESS.getErrors(), result.get().getErrors()); + assertEquals(RPC_RESULT_SUCCESS.getResult(), result.get().getResult()); + assertEquals(RPC_RESULT_SUCCESS.isSuccessful(), result.get().isSuccessful()); } + /** + * Tests {@link MappingService#addMapping} method from southbound. + * + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void addMappingTest_fromSouthbound() throws ExecutionException, InterruptedException { + // input + final MappingRecord data = getDefaultMappingRecordBuilder() + .setXtrId(XTR_ID).build(); + + mappingService.addMapping(MappingOrigin.Southbound, IPV4_EID, SITE_ID, data, false); + + Mockito.verify(mappingSystem).addMapping(MappingOrigin.Southbound, IPV4_EID, data, false); + Mockito.verify(dsbe).addMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, IPV4_EID, SITE_ID, data)); + Mockito.verify(dsbe).addXtrIdMapping(DSBEInputUtil.toXtrIdMapping(data)); + } + + /** + * Tests {@link MappingService#addMapping} method from northbound. + * + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void addMappingTest_fromNorthbound() throws ExecutionException, InterruptedException { + // input + final MappingOrigin origin = MappingOrigin.Northbound; + final MappingRecord data = getDefaultMappingRecordBuilder() + .setXtrId(XTR_ID).build(); + + mappingService.addMapping(origin, IPV4_EID, SITE_ID, data, false); + Mockito.verify(dsbe).addMapping(DSBEInputUtil.toMapping(origin, IPV4_EID, SITE_ID, data)); + Mockito.verifyZeroInteractions(mappingSystem); + Mockito.verifyNoMoreInteractions(dsbe); + } + + /** + * Tests {@link MappingService#getMapping(MappingOrigin, Eid)} method. + */ + @Test + public void getMappingTest_withOriginAndEid() { + Mockito.when(mappingSystem.getMapping(Mockito.any(MappingOrigin.class), Mockito.any(Eid.class))) + .thenReturn(DUMMY_OBJECT); + assertEquals(DUMMY_OBJECT, mappingService.getMapping(MappingOrigin.Northbound, IPV4_EID)); + } + + /** + * Tests {@link MappingService#getMapping(Eid)} method. + */ + @Test + public void getMappingTest_withEid() { + Mockito.when(mappingSystem.getMapping(Mockito.any(Eid.class))).thenReturn(DUMMY_OBJECT); + assertEquals(DUMMY_OBJECT, mappingService.getMapping(IPV4_EID)); + } + + /** + * Tests {@link MappingService#getMapping(Eid)} method. + */ + @Test + public void getMappingTest_withSrcAndDstEid() { + Mockito.when(mappingSystem.getMapping(Mockito.any(Eid.class), Mockito.any(Eid.class))).thenReturn(DUMMY_OBJECT); + assertEquals(DUMMY_OBJECT, mappingService.getMapping(IPV4_EID, IPV4_EID)); + } + + /** + * Tests {@link MappingService#removeMapping} method. + */ + @Test + public void removeMappingTest_withMapOrginAndEid() { + mappingService.removeMapping(MappingOrigin.Northbound, IPV4_EID); + Mockito.verify(dsbe).removeMapping(DSBEInputUtil.toMapping(MappingOrigin.Northbound, IPV4_EID)); + } + + /** + * Tests {@link MappingService#addAuthenticationKey} method. + */ + @Test + public void addAuthenticationKeyTest() { + mappingService.addAuthenticationKey(IPV4_EID, MAPPING_AUTHKEY); + Mockito.verify(dsbe).addAuthenticationKey(DSBEInputUtil.toAuthenticationKey(IPV4_EID, MAPPING_AUTHKEY)); + } + + /** + * Tests {@link MappingService#getAuthenticationKey(Eid)} method. + */ + @Test + public void getAuthenticationKeyTest() { + Mockito.when(mappingSystem.getAuthenticationKey(IPV4_EID)).thenReturn(MAPPING_AUTHKEY); + assertEquals(MAPPING_AUTHKEY, mappingService.getAuthenticationKey(IPV4_EID)); + } + + /** + * Tests {@link MappingService#removeAuthenticationKey} method. + */ + @Test + public void removeAuthenticationKeyTest() { + mappingService.removeAuthenticationKey(IPV4_EID); + Mockito.verify(dsbe).removeAuthenticationKey(DSBEInputUtil.toAuthenticationKey(IPV4_EID, null)); + } + + /** + * Tests {@link MappingService#addData} method. + */ + @Test + public void addDataTest() { + mappingService.addData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD, DUMMY_OBJECT); + Mockito.verify(mappingSystem).addData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD, DUMMY_OBJECT); + } + + /** + * Tests {@link MappingService#getData} method. + */ + @Test + public void getDataTest() { + Mockito.when(mappingSystem.getData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD)) + .thenReturn(DUMMY_OBJECT); + assertEquals(DUMMY_OBJECT, mappingService.getData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD)); + } + + /** + * Tests {@link MappingService#removeData} method. + */ + @Test + public void removeDataTest() { + mappingService.removeData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD); + Mockito.verify(mappingSystem).removeData(MappingOrigin.Northbound, IPV4_EID, SubKeys.RECORD); + } + + /** + * Tests {@link MappingService#printMappings} method. + */ + @Test + public void printMappingsTest() { + mappingService.printMappings(); + Mockito.verify(mappingSystem).printMappings(); + } + + /** + * Tests {@link MappingService#close} method. + */ + @Test + public void close() throws Exception { + mappingService.close(); + Mockito.verify(mappingServiceRpc).close(); + Mockito.verify(keyListener).closeDataChangeListener(); + Mockito.verify(mappingListener).closeDataChangeListener(); + } + + /** + * Tests {@link MappingService#cleanCachedMappings} method. + */ + @Test + public void cleanCachedMappingsTest() throws Exception { + mappingService.cleanCachedMappings(); + Mockito.verify(mappingSystem).cleanCaches(); + Mockito.verify(dsbe).removeAllDatastoreContent(); + } + + /** + * Tests {@link MappingService} not implemented methods. + */ + @Test + public void nullReturnMethodTest() throws ExecutionException, InterruptedException { + assertNull(mappingService.removeKeys(Mockito.mock(RemoveKeysInput.class))); + assertNull(mappingService.removeMappings(Mockito.mock(RemoveMappingsInput.class))); + assertNull(mappingService.getKeys(Mockito.mock(GetKeysInput.class))); + assertNull(mappingService.addMappings(Mockito.mock(AddMappingsInput.class))); + assertNull(mappingService.updateKeys(Mockito.mock(UpdateKeysInput.class))); + assertNull(mappingService.removeAllMappings()); + assertNull(mappingService.getAllKeys()); + assertNull(mappingService.updateMappings(Mockito.mock(UpdateMappingsInput.class))); + assertNull(mappingService.addKeys(Mockito.mock(AddKeysInput.class))); + assertNull(mappingService.getAllMappings()); + } + + private static MappingRecordBuilder getDefaultMappingRecordBuilder() { + return new MappingRecordBuilder() + .setAction(MappingRecord.Action.NoAction) + .setAuthoritative(false) + .setLocatorRecord(new ArrayList<>()) + .setMapVersion((short) 0) + .setRecordTtl(60) + .setEid(IPV4_EID); + } }