493167ac372ad7eda7259585d0d2283b186c99b2
[ccsdk/features.git] / sdnr / wt / devicemanager-onap / onf14 / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / onf14 / TestOnf14NetworkElementFactory.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14;
19
20 import static org.junit.Assert.assertTrue;
21 import com.google.common.io.Files;
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Optional;
26 import org.junit.AfterClass;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.impl.Onf14NetworkElementFactory;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
36 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
37 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
38 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
39 import org.opendaylight.yang.gen.v1.urn.onf.yang.core.model._1._4.rev191127.ControlConstruct;
40
41 public class TestOnf14NetworkElementFactory extends Mockito {
42
43     private static NetconfAccessor accessor;
44     private static Optional<NetconfDomAccessor> domAccessor;
45     private static Capabilities capabilities;
46     private static DeviceManagerServiceProvider serviceProvider;
47     private static ConfigurationFileRepresentation configurationRepresentation;
48     private static String filename = "test.properties";
49
50     // @formatter:off
51     private static final String TESTCONFIG_CONTENT = "[dmonf14]\n"
52             + "useDomApi=true\n"
53             + "";
54     // @formatter:on
55
56
57     @BeforeClass
58     public static void init() throws InterruptedException, IOException {
59         Files.asCharSink(new File(filename), StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
60         configurationRepresentation = new ConfigurationFileRepresentation(filename);
61         capabilities = mock(Capabilities.class);
62         accessor = mock(NetconfAccessor.class);
63         domAccessor = Optional.of(mock(NetconfDomAccessor.class));
64         serviceProvider = mock(DeviceManagerServiceProvider.class);
65
66         when(accessor.getCapabilites()).thenReturn(capabilities);
67         when(serviceProvider.getDataProvider()).thenReturn(mock(DataProvider.class));
68         when(serviceProvider.getConfigurationFileRepresentation()).thenReturn(configurationRepresentation);
69     }
70
71     @Test
72     public void testCreateOnf14Dom() throws Exception {
73         when(accessor.getCapabilites().isSupportingNamespace(ControlConstruct.QNAME)).thenReturn(true);
74         when(accessor.getNetconfDomAccessor()).thenReturn(domAccessor);
75         when(domAccessor.get().getBindingNormalizedNodeSerializer()).thenReturn(mock(BindingNormalizedNodeSerializer.class));
76         Onf14NetworkElementFactory factory = new Onf14NetworkElementFactory();
77         factory.init(serviceProvider);
78         assertTrue((factory.create(accessor, serviceProvider)).isPresent());
79     }
80
81     @Test
82     public void testCreateOnf14Binding() throws Exception {
83         when(accessor.getCapabilites().isSupportingNamespace(ControlConstruct.QNAME)).thenReturn(true);
84         when(accessor.getNetconfBindingAccessor()).thenReturn(Optional.of(mock(NetconfBindingAccessor.class)));
85         Onf14NetworkElementFactory factory = new Onf14NetworkElementFactory();
86         assertTrue(factory.create(accessor, serviceProvider).isPresent());
87     }
88
89     @Test
90     public void testCreateNone() throws Exception {
91         when(accessor.getNetconfBindingAccessor()).thenReturn(Optional.of(mock(NetconfBindingAccessor.class)));
92         when(accessor.getCapabilites().isSupportingNamespace(ControlConstruct.QNAME)).thenReturn(false);
93         Onf14NetworkElementFactory factory = new Onf14NetworkElementFactory();
94         assertTrue(factory.create(accessor, serviceProvider).isEmpty());
95     }
96
97     @AfterClass
98     public static void cleanUp() {
99         File file = new File(filename);
100         if (file.exists()) {
101             System.out.println("File exists, Deleting it");
102             file.delete();
103         }
104     }
105 }
106