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