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;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.spy;
15 import com.google.common.base.Optional;
16 import com.google.common.cache.LoadingCache;
17 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
24 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
25 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 public class BindingDOMMountPointServiceAdapterTest {
35 private DOMMountPointService mountService;
36 // Use a real instance of codec, since its getCodecRegistry() method is final and cannot be mocked
37 private BindingToNormalizedNodeCodec codec;
39 private BindingNormalizedNodeCodecRegistry codecRegistry;
41 private ClassLoadingStrategy classLoadingStrategy;
46 MockitoAnnotations.initMocks(this);
47 codec = spy(new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry));
48 doReturn(Optional.of(mock(DOMMountPoint.class)))
49 .when(mountService).getMountPoint(any(YangInstanceIdentifier.class));
50 doReturn(YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(QName.create("(a)b"))))
51 .when(codec).toYangInstanceIdentifierBlocking(any(InstanceIdentifier.class));
52 doReturn(InstanceIdentifier.create(DataObject.class))
53 .when(codecRegistry).fromYangInstanceIdentifier(any(YangInstanceIdentifier.class));
56 @Test(timeout = 30 * 1000)
57 public void testCaching() throws Exception {
58 BindingDOMMountPointServiceAdapter baService = new BindingDOMMountPointServiceAdapter(mountService, codec);
59 LoadingCache<DOMMountPoint, BindingMountPointAdapter> cache = baService.bindingMountpoints;
61 baService.getMountPoint(InstanceIdentifier.create(DataObject.class));
67 if (cache.asMap().keySet().size() == 0) {
68 // Cache has been cleared, the single cache entry was garbage collected