aboutsummaryrefslogtreecommitdiffstats
path: root/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImplTest.java
blob: e31c0987898daae45a4894ca0d3a794afa59430f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*-
 * ============LICENSE_START=======================================================
 * ONAP : CCSDK.apps
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (C) 2018 IBM.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.apps.ms.neng.service.extinf.impl;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException;
import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigRequest;
import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigResponse;
import org.onap.ccsdk.apps.ms.neng.core.rs.interceptors.PolicyManagerAuthorizationInterceptor;
import org.onap.ccsdk.apps.ms.neng.extinf.props.PolicyManagerProps;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;

@RunWith(MockitoJUnitRunner.class)
public class PolicyFinderServiceImplTest {
    @InjectMocks
    @Spy
    PolicyFinderServiceImpl policyFinder;
    @Spy
    PolicyManagerProps policManProps;
    @Mock
    RestTemplateBuilder policyMgrRestTempBuilder;
    @Mock
    PolicyManagerAuthorizationInterceptor authInt;
    @Mock
    RestTemplate restTemplate;

    @Test
    public void testConfig() throws Exception {
        doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall( 
            Matchers.any(), Matchers.any(), Matchers.any());
        assertNotNull(policyFinder.getConfig("policy"));
    }
    
    @Test
    public void testFindPolicy() throws Exception {
        doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall( 
            Matchers.any(), Matchers.any(), Matchers.any());
        assertNull(policyFinder.findPolicy("policy"));
    }

    @Test(expected = NengException.class)
    public void testHandleError_Not_Found() throws Exception {
        HttpStatusCodeException e = new HttpClientErrorException(HttpStatus.NOT_FOUND,"",
                        null,StandardCharsets.US_ASCII);
        policyFinder.handleError(e);
    }
    
    @SuppressWarnings("unchecked")
    @Test
    public void testmakeOutboundCall() throws Exception {
        Map<String, Object> configMap = buildPolicyResponse();
        Object resp = Arrays.asList(new Object[] {configMap});
        ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.OK);
        Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);

        policManProps.setUrl("http://policyManager.onap.org");

        GetConfigRequest request = new GetConfigRequest();
        request.setPolicyName("policy");
        GetConfigResponse configResp = policyFinder.makeOutboundCall("",request, GetConfigResponse.class);
        assertNotNull(configResp);
    }

    @SuppressWarnings("unchecked")
    @Test(expected = NengException.class)
    public void testmakeOutboundCall_500() throws Exception {
        Map<String, Object> configMap = buildPolicyResponse();
        Object resp = Arrays.asList(new Object[] {configMap});
        ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR);
        Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);

        policManProps.setUrl("http://policyManager.onap.org");

        GetConfigRequest request = new GetConfigRequest();
        request.setPolicyName("policy");
        policyFinder.makeOutboundCall("",request, GetConfigResponse.class);
    }

    @Test
    public void testGetRestTemplate() throws Exception {
        PolicyFinderServiceImpl service = new PolicyFinderServiceImpl();
        RestTemplateBuilder policyRestTemplateBuilder = new RestTemplateBuilder();
        service.setPolicyMgrRestTempBuilder(policyRestTemplateBuilder);
        service.setAuthInt(new PolicyManagerAuthorizationInterceptor());

        assertNotNull(service.getPolicyMgrRestTempBuilder());
        assertNotNull(service.getAuthInt());
        assertNotNull(service.getRestTemplate(Boolean.FALSE));
    }

    @Test
    public void testTransformConfigObject() throws Exception {
        String config = "{\"riskLevel\":\"4\",\"riskType\":\"test\","
                        + "\"policyName\":\"1806SriovBigJson\",\"service\":\"SDNC-GenerateName\","
                        + "\"guard\":\"False\",\"description\":\"1806SriovBigJson\","
                        + "\"templateVersion\":\"1607\",\"priority\":\"4\",\"version\":\"pannny_nnnn\","
                        + "\"content\":{\"policy-instance-name\":\"1806NameGenerationPolicyForSRIOV\","
                        + "\"naming-models\":[{\"naming-properties\":[{\"property-operation\":\"substr(5)\","
                        + "\"property-name\":\"COMPLEX\"},{\"property-name\":\"SEQUENCE\","
                        + "\"increment-sequence\":{\"max\":\"zzz\",\"scope\":\"ENTIRETY\","
                        + "\"start-value\":\"001\",\"length\":\"3\",\"increment\":\"1\","
                        + "\"sequence-type\":\"alpha-numeric\"}},{\"property-name\":\"NF_NAMING_CODE\"}],"
                        + "\"naming-type\":\"VNF\",\"nfRole\":\"vPE\","
                        + "\"naming-recipe\":\"COMPLEX|SEQUENCE|NF_NAMING_CODE\"},"
                        + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
                        + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
                        + "{\"max\":\"999\",\"scope\":\"ENTIRETY\",\"start-value\":\"001\",\"length\":\"3\","
                        + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}},"
                        + "{\"property-operation\":\"substr(-3)\",\"property-name\":\"NFC_NAMING_CODE\"}],"
                        + "\"naming-type\":\"VM\",\"nfRole\":\"vPE\","
                        + "\"naming-recipe\":\"VNF_NAME|SEQUENCE|NFC_NAMING_CODE\"},"
                        + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
                        + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
                        + "{\"property-name\":\"VF_MODULE_LABEL\"},{\"property-name\":\"VF_MODULE_TYPE\"},"
                        + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
                        + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\",\"length\":\"2\","
                        + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
                        + "\"naming-type\":\"VF-MODULE\",\"nfRole\":\"vPE\","
                        + "\"naming-recipe\":\"VNF_NAME|DELIMITER|VF_MODULE_LABEL|DELIMITER"
                        + "|VF_MODULE_TYPE|DELIMITER|SEQUENCE\"},"
                        + "{\"naming-properties\":[{\"property-name\":\"VF-MODULE_NAME\"},"
                        + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
                        + "{\"property-value\":\"volumegroup\",\"property-name\":\"CONSTANT\"}],"
                        + "\"naming-type\":\"VOLUME_GROUP\",\"nfRole\":\"vPE\","
                        + "\"naming-recipe\":\"VF-MODULE_NAME|DELIMITER|CONSTANT\"},"
                        + "{\"naming-properties\":[{\"property-name\":\"VOLUME_GROUP_NAME\"},"
                        + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
                        + "{\"property-value\":\"volume\",\"property-name\":\"CONSTANT\"},"
                        + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
                        + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
                        + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
                        + "\"naming-type\":\"VOLUME\",\"nfRole\":\"vPE\","
                        + "\"naming-recipe\":\"VOLUME_GROUP_NAME|DELIMITER|CONSTANT|DELIMITER|SEQUENCE\"},"
                        + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
                        + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
                        + "{\"property-value\":\"affinity\",\"property-name\":\"CONSTANT\"}],"
                        + "\"naming-type\":\"AFFINITY\",\"nfRole\":\"vPE\","
                        + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT\"},"
                        + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
                        + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
                        + "{\"property-value\":\"INT\",\"property-name\":\"CONSTANT\"},"
                        + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
                        + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
                        + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
                        + "\"naming-type\":\"INTERNAL_NETWORK\",\"nfRole\":\"vPE\","
                        + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT|SEQUENCE\"}]}}";
        Map<Object, Object> configMap = new HashMap<>();
        configMap.put("config", config);
        ObjectMapper objectmapper = new ObjectMapper();
        List<Map<Object, Object>> respList = new ArrayList<>();
        respList.add(configMap);
        policyFinder.transformConfigObject(objectmapper, respList);
        assertNotNull(respList.get(0).get("config"));
    }

    Map<String, Object> buildPolicyResponse() {
        Map<String, Object> policyDataMap = new HashMap<>();
        policyDataMap.put("policy-instance-name", "SDNC_Policy.Config_MS_VNFCNamingPolicy");
        Map<String, Object> namingModelMap = new HashMap<>();
        namingModelMap.put("nf-role", "vPE");
        namingModelMap.put("naming-type", "VNF");
        namingModelMap.put("naming-recipe", "COMPLEX|NF-NAMING-CODE|Field2|Field3|Field4");
        Map<String, Object> namingPropertyMap = new HashMap<>();
        Map<String, Object> propertyMap1 = new HashMap<>();
        propertyMap1.put("property-name", "COMPLEX");
        Map<String, Object> propertyMap2 = new HashMap<>();
        propertyMap2.put("property-name", "NF-NAMING-CODE");
        namingPropertyMap.put("", Arrays.asList(new Object[] {propertyMap1, propertyMap2}));
        namingModelMap.put("naming-properties", namingPropertyMap);
        policyDataMap.put("naming-models", Arrays.asList(new Object[] {namingModelMap}));
        Map<String, Object> configMap = new HashMap<>();
        Map<String, Object> contentMap = new HashMap<>();
        contentMap.put("content", policyDataMap);
        configMap.put("config", contentMap);
        return configMap;
    }

    @SuppressWarnings("unchecked")
    @Test(expected = NengException.class)
    public void testmakeOutboundCall_500_statusExp() throws Exception {
        HttpClientErrorException exp = new HttpClientErrorException(HttpStatus.METHOD_NOT_ALLOWED, "{error}");
        Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenThrow(exp);
        policManProps.setUrl("http://policyManager.onap.org");
        GetConfigRequest request = new GetConfigRequest();
        request.setPolicyName("policy");
        policyFinder.makeOutboundCall("", request, GetConfigResponse.class);
    }
    
    @SuppressWarnings("unchecked")
    @Test(expected = NengException.class)
    public void testmakeOutboundCall_500_statusExp_notFound() throws Exception {
        HttpClientErrorException exp = new HttpClientErrorException(HttpStatus.NOT_FOUND, "{error}");
        Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenThrow(exp);
        policManProps.setUrl("http://policyManager.onap.org");
        GetConfigRequest request = new GetConfigRequest();
        request.setPolicyName("policy");
        policyFinder.makeOutboundCall("", request, GetConfigResponse.class);
    }

    @Test
    public void testmakeOutboundCall_err_policy() throws Exception {
        Map<String, Object> configMap = buildPolicyResponse();      
        Object resp = configMap;
        GetConfigResponse configResp = new GetConfigResponse();
        configResp.setResponse(resp);
        doReturn(configResp).when(policyFinder).getConfig("policy");
        assertNull(policyFinder.findPolicy("policy"));
    }
    
    @Test
    public void testmakeOutboundCall_err_policy_empty() throws Exception {
        Object resp = Arrays.asList(new Object[]{});
        GetConfigResponse configResp = new GetConfigResponse();
        configResp.setResponse(resp);
        doReturn(configResp).when(policyFinder).getConfig("policy");
        assertNull(policyFinder.findPolicy("policy"));
    }
    
}