Fix star import and enable checkstyle rule to prevent it.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / test / java / org / opendaylight / controller / 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.controller.netconf.confignetconfconnector.operations.editconfig;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.HashMultimap;
13 import com.google.common.collect.Maps;
14 import com.google.common.collect.Multimap;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mock;
18 import org.mockito.MockitoAnnotations;
19 import org.opendaylight.controller.config.util.ConfigRegistryClient;
20 import org.opendaylight.controller.config.util.ConfigTransactionClient;
21 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
22 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.InstanceConfigElementResolved;
26 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
27 import org.opendaylight.controller.netconf.confignetconfconnector.operations.ValidateTest;
28 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfigXmlParser.EditConfigExecution;
29 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
30 import org.opendaylight.controller.netconf.util.xml.XmlElement;
31 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
32
33 import javax.management.ObjectName;
34 import java.util.Map;
35
36 import static org.mockito.Matchers.any;
37 import static org.mockito.Matchers.anyMap;
38 import static org.mockito.Matchers.anyString;
39 import static org.mockito.Mockito.doNothing;
40 import static org.mockito.Mockito.doReturn;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.times;
43 import static org.mockito.Mockito.verify;
44
45 public class EditConfigTest {
46
47     @Mock
48     private YangStoreSnapshot yangStoreSnapshot;
49     @Mock
50     private TransactionProvider provider;
51     @Mock
52     private ConfigRegistryClient configRegistry;
53     @Mock
54     private ConfigTransactionClient configTransactionClient;
55     @Mock
56     private ObjectName mockOn;
57
58     @Before
59     public void setUp() throws Exception {
60         MockitoAnnotations.initMocks(this);
61
62         doReturn("mockON").when(mockOn).toString();
63         doReturn(mockOn).when(provider).getTestTransaction();
64         doNothing().when(provider).validateTestTransaction(any(ObjectName.class));
65
66         doReturn(mockOn).when(provider).getTestTransaction();
67         doNothing().when(provider).abortTestTransaction(any(ObjectName.class));
68         doReturn(mockOn).when(provider).getOrCreateTransaction();
69
70         doReturn(configTransactionClient).when(configRegistry).getConfigTransactionClient(any(ObjectName.class));
71         doReturn("mockConfigTransactionClient").when(configTransactionClient).toString();
72
73         doReturn(mockOn).when(configTransactionClient).lookupConfigBean(anyString(), anyString());
74     }
75
76     @Test
77     public void test() throws NetconfDocumentedException {
78         EditConfig edit = new EditConfig(yangStoreSnapshot, provider, configRegistry,
79                 ValidateTest.NETCONF_SESSION_ID_FOR_REPORTING);
80         EditConfigStrategy editStrat = mock(EditConfigStrategy.class);
81         doNothing().when(editStrat).executeConfiguration(anyString(), anyString(), anyMap(),
82                 any(ConfigTransactionClient.class));
83         Map<String, Multimap<String, ModuleElementResolved>> resolvedXmlElements = getMapping(editStrat);
84
85         Config cfg = mock(Config.class);
86         XmlElement xmlElement = mock(XmlElement.class);
87         doReturn(resolvedXmlElements).when(cfg).fromXml(xmlElement);
88
89         EditConfigExecution editConfigExecution = new EditConfigExecution(null, cfg, xmlElement,
90                 EditConfigXmlParser.TestOption.testThenSet);
91
92         edit.getResponseInternal(XmlUtil.newDocument(), editConfigExecution);
93
94         verify(provider).getTestTransaction();
95         verify(provider).validateTestTransaction(mockOn);
96         verify(provider).abortTestTransaction(mockOn);
97
98         verify(provider).getOrCreateTransaction();
99
100         // For every instance execute strat
101         verify(editStrat, times(2/* Test */+ 2/* Set */)).executeConfiguration(anyString(), anyString(), anyMap(),
102                 any(ConfigTransactionClient.class));
103     }
104
105     private Map<String, Multimap<String, ModuleElementResolved>> getMapping(EditConfigStrategy editStrat) {
106         Map<String, Multimap<String, ModuleElementResolved>> result = Maps.newHashMap();
107
108         Multimap<String, ModuleElementResolved> innerMultimap = HashMultimap.create();
109         Map<String, AttributeConfigElement> attributes = getSimpleAttributes();
110
111         InstanceConfigElementResolved ice1 = mock(InstanceConfigElementResolved.class);
112         doReturn(attributes).when(ice1).getConfiguration();
113         doReturn(editStrat).when(ice1).getEditStrategy();
114         innerMultimap.put("m1", new ModuleElementResolved("i1", ice1));
115
116         InstanceConfigElementResolved ice2 = mock(InstanceConfigElementResolved.class);
117         doReturn(attributes).when(ice2).getConfiguration();
118         doReturn(editStrat).when(ice2).getEditStrategy();
119         innerMultimap.put("m1", new ModuleElementResolved("i2", ice2));
120
121         result.put("n1", innerMultimap);
122
123         return result;
124     }
125
126     static Map<String, AttributeConfigElement> getSimpleAttributes() {
127         Map<String, AttributeConfigElement> attributes = Maps.newHashMap();
128         AttributeConfigElement ace1 = mock(AttributeConfigElement.class);
129         doReturn("abcd").when(ace1).getResolvedDefaultValue();
130         doReturn(Optional.<String> of("abc")).when(ace1).getResolvedValue();
131         doReturn("mockedAce1").when(ace1).toString();
132         doReturn("jmxNameAce1").when(ace1).getJmxName();
133         attributes.put("a1", ace1);
134         return attributes;
135     }
136
137 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.