Merge "Remove unused controller config dependencies"
[netconf.git] / netconf / config-netconf-connector / src / test / java / org / opendaylight / netconf / confignetconfconnector / operations / editconfig / EditConfigTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.confignetconfconnector.operations.editconfig;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyMapOf;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19
20 import com.google.common.base.Optional;
21 import com.google.common.collect.HashMultimap;
22 import com.google.common.collect.Maps;
23 import com.google.common.collect.Multimap;
24 import java.util.Collections;
25 import java.util.Map;
26 import javax.management.ObjectName;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
32 import org.opendaylight.controller.config.facade.xml.ConfigExecution;
33 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
34 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement;
35 import org.opendaylight.controller.config.facade.xml.mapping.config.InstanceConfigElementResolved;
36 import org.opendaylight.controller.config.facade.xml.mapping.config.ModuleElementDefinition;
37 import org.opendaylight.controller.config.facade.xml.mapping.config.ModuleElementResolved;
38 import org.opendaylight.controller.config.facade.xml.mapping.config.ServiceRegistryWrapper;
39 import org.opendaylight.controller.config.facade.xml.mapping.config.Services;
40 import org.opendaylight.controller.config.facade.xml.osgi.YangStoreService;
41 import org.opendaylight.controller.config.facade.xml.strategy.EditConfigStrategy;
42 import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType;
43 import org.opendaylight.controller.config.facade.xml.transactions.TransactionProvider;
44 import org.opendaylight.controller.config.util.ConfigRegistryClient;
45 import org.opendaylight.controller.config.util.ConfigTransactionClient;
46 import org.opendaylight.controller.config.util.xml.XmlElement;
47 import org.opendaylight.controller.config.util.xml.XmlUtil;
48 import org.opendaylight.netconf.confignetconfconnector.operations.ValidateTest;
49
50 public class EditConfigTest {
51
52     @Mock
53     private YangStoreService yangStoreSnapshot;
54     @Mock
55     private TransactionProvider provider;
56     @Mock
57     private ConfigRegistryClient configRegistry;
58     @Mock
59     private ConfigTransactionClient configTransactionClient;
60     @Mock
61     private ObjectName mockOn;
62
63     private ConfigSubsystemFacade cfgFacade;
64
65     @Before
66     public void setUp() throws Exception {
67         MockitoAnnotations.initMocks(this);
68
69         doReturn("mockON").when(mockOn).toString();
70         doReturn(mockOn).when(provider).getTestTransaction();
71         doNothing().when(provider).validateTestTransaction(any(ObjectName.class));
72
73         doReturn(mockOn).when(provider).getTestTransaction();
74         doNothing().when(provider).abortTestTransaction(any(ObjectName.class));
75         doReturn(mockOn).when(provider).getOrCreateTransaction();
76
77         doNothing().when(provider).wipeTestTransaction(any(ObjectName.class));
78
79         doReturn(configTransactionClient).when(configRegistry).getConfigTransactionClient(any(ObjectName.class));
80         doReturn("mockConfigTransactionClient").when(configTransactionClient).toString();
81
82         doReturn(mockOn).when(configTransactionClient).lookupConfigBean(anyString(), anyString());
83
84         cfgFacade = new ConfigSubsystemFacade(configRegistry, configRegistry, yangStoreSnapshot, provider);
85     }
86
87     @Test
88     public void test() throws Exception {
89         EditConfig edit = new EditConfig(cfgFacade, ValidateTest.NETCONF_SESSION_ID_FOR_REPORTING);
90         EditConfigStrategy editStrat = mock(EditConfigStrategy.class);
91
92         doNothing().when(editStrat).executeConfiguration(anyString(), anyString(), anyMapOf(String.class,
93                 AttributeConfigElement.class),
94                 any(ConfigTransactionClient.class), any(ServiceRegistryWrapper.class));
95
96         ConfigExecution editConfigExecution = mockExecution(editStrat);
97
98         edit.getResponseInternal(XmlUtil.newDocument(), editConfigExecution);
99
100         verify(provider).getTestTransaction();
101         verify(provider).validateTestTransaction(mockOn);
102         verify(provider).abortTestTransaction(mockOn);
103
104         verify(provider).getOrCreateTransaction();
105
106         // For every instance execute strat
107         verify(editStrat, times(2/* Test */ + 2/* Set */ + 2/*Handle missing instance Test*/
108                 + 2 /*Handle missing instance Set*/)).executeConfiguration(anyString(),
109                 anyString(), anyMapOf(String.class, AttributeConfigElement.class),
110                 any(ConfigTransactionClient.class), any(ServiceRegistryWrapper.class));
111     }
112
113     private ConfigExecution mockExecution(EditConfigStrategy editStrat) throws Exception {
114         ConfigExecution mock = mock(ConfigExecution.class);
115         doReturn(getMapping(editStrat)).when(mock).getResolvedXmlElements(any(ConfigTransactionClient.class));
116         doReturn(getMappingDefinition(editStrat)).when(mock).getModulesDefinition(any(ConfigTransactionClient.class));
117         doReturn(EditStrategyType.merge).when(mock).getDefaultStrategy();
118         doReturn(true).when(mock).shouldSet();
119         doReturn(true).when(mock).shouldTest();
120         doReturn(mockServices()).when(mock).getServiceRegistryWrapper(any(ConfigTransactionClient.class));
121         doReturn(new Services()).when(mock).getServices();
122         doReturn(XmlElement.fromDomElement(XmlUtil.readXmlToElement("<abc/>"))).when(mock).getConfigElement();
123         return mock;
124     }
125
126     private Object getMappingDefinition(EditConfigStrategy editStrat) {
127         final Map<String, Multimap<String, ModuleElementDefinition>> result = Maps.newHashMap();
128
129         Multimap<String, ModuleElementDefinition> innerMultimap = HashMultimap.create();
130         Map<String, AttributeConfigElement> attributes = getSimpleAttributes();
131
132         ModuleElementDefinition mockedDefinition = mock(ModuleElementDefinition.class);
133         doReturn(editStrat).when(mockedDefinition).getEditStrategy();
134         doReturn("i1").when(mockedDefinition).getInstanceName();
135         innerMultimap.put("m1", mockedDefinition);
136
137         ModuleElementDefinition mockedDefinition2 = mock(ModuleElementDefinition.class);
138         doReturn(editStrat).when(mockedDefinition2).getEditStrategy();
139         doReturn("i2").when(mockedDefinition2).getInstanceName();
140         innerMultimap.put("m1", mockedDefinition2);
141
142         result.put("n1", innerMultimap);
143
144         return result;
145     }
146
147     private static ServiceReferenceReadableRegistry mockServiceRegistry() {
148         ServiceReferenceReadableRegistry mock = mock(ServiceReferenceReadableRegistry.class);
149         doReturn(
150                 Collections.emptyMap())
151                 .when(mock).getServiceMapping();
152         doReturn("mockedServiceReg").when(mock).toString();
153
154         return mock;
155     }
156
157     static ServiceRegistryWrapper mockServices() {
158         return new ServiceRegistryWrapper(mockServiceRegistry());
159     }
160
161     private Map<String, Multimap<String, ModuleElementResolved>> getMapping(EditConfigStrategy editStrat) {
162         final Map<String, Multimap<String, ModuleElementResolved>> result = Maps.newHashMap();
163
164         Multimap<String, ModuleElementResolved> innerMultimap = HashMultimap.create();
165         Map<String, AttributeConfigElement> attributes = getSimpleAttributes();
166
167         InstanceConfigElementResolved ice1 = mock(InstanceConfigElementResolved.class);
168         doReturn(attributes).when(ice1).getConfiguration();
169         doReturn(editStrat).when(ice1).getEditStrategy();
170         innerMultimap.put("m1", new ModuleElementResolved("i1", ice1));
171
172         InstanceConfigElementResolved ice2 = mock(InstanceConfigElementResolved.class);
173         doReturn(attributes).when(ice2).getConfiguration();
174         doReturn(editStrat).when(ice2).getEditStrategy();
175         innerMultimap.put("m1", new ModuleElementResolved("i2", ice2));
176
177         result.put("n1", innerMultimap);
178
179         return result;
180     }
181
182     static Map<String, AttributeConfigElement> getSimpleAttributes() {
183         final Map<String, AttributeConfigElement> attributes = Maps.newHashMap();
184         AttributeConfigElement ace1 = mock(AttributeConfigElement.class);
185         doReturn("abcd").when(ace1).getResolvedDefaultValue();
186         doReturn(Optional.<String>of("abc")).when(ace1).getResolvedValue();
187         doReturn("mockedAce1").when(ace1).toString();
188         doReturn("jmxNameAce1").when(ace1).getJmxName();
189         attributes.put("a1", ace1);
190         return attributes;
191     }
192
193 }