Change nexus values to properties
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / test / java / OperationalStateValidatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 import org.openecomp.appc.exceptions.APPCException;
23 import org.junit.Test;
24 import org.openecomp.appc.adapter.netconf.OperationalStateValidator;
25 import org.openecomp.appc.adapter.netconf.OperationalStateValidatorFactory;
26 import org.openecomp.appc.adapter.netconf.VnfType;
27
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.fail;
30
31
32 public class OperationalStateValidatorTest {
33
34     @Test
35     public void testVNFValidResponse() {
36         String validResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
37                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"101\">\n" +
38                 "    <data>\n" +
39                 "        <ManagedElement xmlns=\"urn:org:openecomp:appc:Test\">\n" +
40                 "            <managedElementId>1</managedElementId>\n" +
41                 "            <VnfFunction xmlns=\"urn:org:openecomp:appc:Test\">\n" +
42                 "                <id>1</id>\n" +
43                 "                <ProcessorManagement>\n" +
44                 "                    <id>1</id>\n" +
45                 "                    <MatedPair>\n" +
46                 "                        <id>1</id>\n" +
47                 "                        <operationalState>ENABLED</operationalState>\n" +
48                 "                        <PayloadProcessor>\n" +
49                 "                            <id>processor_0_5</id>\n" +
50                 "                            <operationalState>ENABLED</operationalState>\n" +
51                 "                        </PayloadProcessor>\n" +
52                 "                        <PayloadProcessor>\n" +
53                 "                            <id>processor_0_7</id>\n" +
54                 "                            <operationalState>ENABLED</operationalState>\n" +
55                 "                        </PayloadProcessor>\n" +
56                 "                    </MatedPair>\n" +
57                 "                    <SystemController>\n" +
58                 "                        <id>SC-1</id>\n" +
59                 "                        <operationalState>ENABLED</operationalState>\n" +
60                 "                    </SystemController>\n" +
61                 "                    <SystemController>\n" +
62                 "                        <id>SC-2</id>\n" +
63                 "                        <operationalState>ENABLED</operationalState>\n" +
64                 "                    </SystemController>\n" +
65                 "                </ProcessorManagement>\n" +
66                 "            </VnfFunction>\n" +
67                 "        </ManagedElement>\n" +
68                 "    </data>\n" +
69                 "</rpc-reply>";
70         OperationalStateValidator operationalStateValidator = OperationalStateValidatorFactory.getOperationalStateValidator(VnfType.VNF);
71         assertValidResponse(validResponse, operationalStateValidator);
72     }
73
74     void assertInvalidResponse(String response, OperationalStateValidator operationalStateValidator) {
75         try {
76             operationalStateValidator.validateResponse(response);
77             fail("invalid resposne passed without exception!!!");
78         } catch (APPCException e) {
79             assertNotNull(e.getMessage());
80         }
81     }
82
83     @Test
84     public void testVNFInvalidResponses() {
85
86         OperationalStateValidator operationalStateValidator = OperationalStateValidatorFactory.getOperationalStateValidator(VnfType.VNF);
87         assertInvalidResponse(null, operationalStateValidator);
88
89         assertInvalidResponse("", operationalStateValidator);
90
91         String response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
92         assertInvalidResponse(response, operationalStateValidator);
93
94         response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
95                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"101\">\n" +
96                 "</rpc-reply>";
97         assertInvalidResponse(response, operationalStateValidator);
98
99         response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
100                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"101\">\n" +
101                 "    <data>\n" +
102                 "        <ManagedElement xmlns=\"urn:org:openecomp:appc:Test\">\n" +
103                 "            <managedElementId>1</managedElementId>\n" +
104                 "            <VnfFunction xmlns=\"urn:org:openecomp:appc:Test\">\n" +
105                 "                <id>1</id>\n" +
106                 "                <ProcessorManagement>\n" +
107                 "                    <id>1</id>\n" +
108                 "                    <MatedPair>\n" +
109                 "                        <id>1</id>\n" +
110                 "                        <operationalState>ENABLED</operationalState>\n" +
111                 "                        <PayloadProcessor>\n" +
112                 "                            <id>processor_0_5</id>\n" +
113                 "                            <operationalState>ENABLED</operationalState>\n" +
114                 "                        </PayloadProcessor>\n" +
115                 "                        <PayloadProcessor>\n" +
116                 "                            <id>processor_0_7</id>\n" +
117                 "                            <operationalState>ENABLED</operationalState>\n" +
118                 "                        </PayloadProcessor>\n" +
119                 "                    </MatedPair>\n" +
120                 "                    <SystemController>\n" +
121                 "                        <id>SC-1</id>\n" +
122                 "                        <operationalState>ENABLED</operationalState>\n" +
123                 "                    </SystemController>\n" +
124                 "                    <SystemController>\n" +
125                 "                        <id>SC-2</id>\n" +
126                 "                        <operationalState></operationalState>\n" +
127                 "                    </SystemController>\n" +
128                 "                </ProcessorManagement>\n" +
129                 "            </VnfFunction>\n" +
130                 "        </ManagedElement>\n" +
131                 "    </data>\n" +
132                 "</rpc-reply>";
133         assertInvalidResponse(response, operationalStateValidator);
134     }
135
136     void assertValidResponse(String response, OperationalStateValidator operationalStateValidator) {
137         try {
138             operationalStateValidator.validateResponse(response);
139         } catch (APPCException e) {
140             fail("Got unexpected exception. Validation failed. " + e.getMessage());
141         }
142     }
143
144     @Test
145     public void testMockValidResponse() {
146         String response = "valid";
147         OperationalStateValidator operationalStateValidator = OperationalStateValidatorFactory.getOperationalStateValidator("mock");
148         assertValidResponse(response, operationalStateValidator);
149
150         response = "";
151         assertValidResponse(response, operationalStateValidator);
152
153         response = null;
154         assertValidResponse(response, operationalStateValidator);
155     }
156
157     @Test
158     public void testMockInValidResponse() {
159         String response = "anything InValid anything.. ";
160         OperationalStateValidator operationalStateValidator = OperationalStateValidatorFactory.getOperationalStateValidator(VnfType.MOCK);
161         assertInvalidResponse(response, operationalStateValidator);
162     }
163
164     @Test
165     public void testGetOperationalStateValidatorForInValidVnfType() {
166         try{
167             OperationalStateValidatorFactory.getOperationalStateValidator("wrongVnfType");
168             fail("invalid vnfType without exception!!!");
169         } catch (Exception e) {
170             assertNotNull(e.getMessage());
171         }
172     }
173
174     @Test
175     public void testGetOperationalStateValidatorForValidVnfType() {
176         String vnfType = VnfType.VNF.name().toLowerCase();
177         assertGettingValidatorForValidVnf(vnfType);
178
179         vnfType = VnfType.VNF.name().toUpperCase();
180         assertGettingValidatorForValidVnf(vnfType);
181
182         vnfType = VnfType.MOCK.name().toLowerCase();
183         assertGettingValidatorForValidVnf(vnfType);
184
185         vnfType = VnfType.MOCK.name().toUpperCase();
186         assertGettingValidatorForValidVnf(vnfType);
187     }
188
189     void assertGettingValidatorForValidVnf(String vnfType) {
190         try{
191             OperationalStateValidator operationalStateValidator = OperationalStateValidatorFactory.getOperationalStateValidator(vnfType);
192             assertNotNull(operationalStateValidator);
193         } catch (Exception e) {
194             fail("valid vnfType throw exception!!!");
195         }
196     }
197 }