40e2e4ee185a659cacf3ae6730bccca030012596
[so/adapters/so-cnf-adapter.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2021 Samsung Electronics Co. Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.cnf.service.aai;
22
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.onap.so.adapters.cnf.model.instantiation.AaiRequest;
29 import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceGvk;
30 import org.onap.so.adapters.cnf.model.statuscheck.K8sRbInstanceResourceStatus;
31 import org.onap.so.adapters.cnf.model.statuscheck.K8sStatus;
32 import org.onap.so.adapters.cnf.model.statuscheck.K8sStatusMetadata;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.test.context.junit4.SpringRunner;
35
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import static org.junit.Assert.assertEquals;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42
43 @SpringBootTest
44 @RunWith(SpringRunner.class)
45 public class AaiResponseParserTest {
46
47     private final static String INSTANCE_ID = "k8splugin.io/rb-instance-id";
48     private final static String INSTANCE_ID_VALUE = "rb-instance-id_value";
49
50     @InjectMocks
51     private AaiResponseParser aaiResponseParser;
52
53     @Mock
54     private AaiIdGeneratorService aaiIdGeneratorService;
55
56
57     @Test
58     public void shouldParseAaiResponse() {
59         // given
60         String id = "id";
61         String name = "name";
62         String group = "group";
63         String version = "version";
64         String kind = "kind";
65         String namespace = "namespace";
66         Map<String, String> labelsMap = new HashMap<>();
67         labelsMap.put("key", "value");
68         labelsMap.put(INSTANCE_ID, INSTANCE_ID_VALUE);
69         K8sRbInstanceResourceStatus status = mock(K8sRbInstanceResourceStatus.class);
70         AaiRequest aaiRequest = mock(AaiRequest.class);
71         K8sRbInstanceGvk gvk = mock(K8sRbInstanceGvk.class);
72         K8sStatusMetadata metadata = mock(K8sStatusMetadata.class);
73         K8sStatus k8sStatus = mock(K8sStatus.class);
74
75         // when
76         when(status.getGvk()).thenReturn(gvk);
77         when(status.getStatus()).thenReturn(k8sStatus);
78         when(k8sStatus.getK8sStatusMetadata()).thenReturn(metadata);
79         when(aaiIdGeneratorService.generateId(status, aaiRequest)).thenReturn(id);
80         when(status.getName()).thenReturn(name);
81         when(gvk.getGroup()).thenReturn(group);
82         when(gvk.getVersion()).thenReturn(version);
83         when(gvk.getKind()).thenReturn(kind);
84         when(metadata.getNamespace()).thenReturn(namespace);
85         when(aaiRequest.getInstanceId()).thenReturn(id);
86
87         when(metadata.getLabels()).thenReturn(labelsMap);
88
89         // then
90         KubernetesResource actual = aaiResponseParser.parse(status, aaiRequest);
91
92         Assert.assertNotNull(actual);
93         assertEquals(id, actual.getId());
94         assertEquals(name, actual.getName());
95         assertEquals(group, actual.getGroup());
96         assertEquals(version, actual.getVersion());
97         assertEquals(kind, actual.getKind());
98         assertEquals(namespace, actual.getNamespace());
99         assertEquals(2, actual.getLabels().size());
100         assertEquals("k8splugin.io/rb-instance-id=rb-instance-id_value", actual.getLabels().get(0));
101         assertEquals("key=value", actual.getLabels().get(1));
102         assertEquals("http://so-cnf-adapter:8090/api/cnf-adapter/v1/instance/id/query?ApiVersion=version&Kind=kind&Name=name&Namespace=namespace", actual.getSelflink());
103     }
104 }