2 * Copyright (c) 2019 FRINX and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.binding.impl;
10 import static org.mockito.ArgumentMatchers.any;
12 import com.google.common.base.Optional;
13 import com.google.common.cache.LoadingCache;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.MockitoAnnotations;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
20 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
21 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
22 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 public class BindingDOMMountPointServiceAdapterTest {
32 private DOMMountPointService mountService;
33 // Use a real instance of codec, since its getCodecRegistry() method is final and cannot be mocked
34 private BindingToNormalizedNodeCodec codec;
36 private BindingNormalizedNodeCodecRegistry codecRegistry;
38 private ClassLoadingStrategy classLoadingStrategy;
42 MockitoAnnotations.initMocks(this);
43 codec = Mockito.spy(new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry));
44 Mockito.doAnswer(invocationOnMock -> Optional.of(Mockito.mock(DOMMountPoint.class)))
45 .when(mountService).getMountPoint(any(YangInstanceIdentifier.class));
46 Mockito.doReturn(YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(QName.create("(a)b"))))
47 .when(codec).toYangInstanceIdentifierBlocking(any(InstanceIdentifier.class));
48 Mockito.doReturn(InstanceIdentifier.create(DataObject.class))
49 .when(codecRegistry).fromYangInstanceIdentifier(any(YangInstanceIdentifier.class));
52 @Test(timeout = 30 * 1000)
53 public void testCaching() throws Exception {
54 BindingDOMMountPointServiceAdapter baService = new BindingDOMMountPointServiceAdapter(mountService, codec);
55 LoadingCache<DOMMountPoint, BindingMountPointAdapter> cache = baService.bindingMountpoints;
57 baService.getMountPoint(InstanceIdentifier.create(DataObject.class));
63 if (cache.asMap().keySet().size() == 0) {
64 // Cache has been cleared, the single cache entry was garbage collected