aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/services/AaiResponseTranslatorTest.java
blob: 841ea10acb36b0c8f07910ecad62c339859f8e96 (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
package org.onap.vid.services;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.onap.vid.aai.AaiResponseTranslator;
import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
import org.testng.annotations.Test;

import java.io.IOException;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class AaiResponseTranslatorTest {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Test
    public void extractPortMirroringConfigData_givenValidAaiResponse_yieldCloudRegionId() throws IOException {

        final JsonNode aaiPayload = objectMapper.readTree("" +
                "{" +
                "  \"results\": [{" +
                "      \"id\": \"2979590232\"," +
                "      \"node-type\": \"cloud-region\"," +
                "      \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/att-aic/SDNO-S-BcloudReg-E1802\"," +
                "      \"properties\": {" +
                "        \"cloud-owner\": \"att-aic\"," +
                "        \"cloud-region-id\": \"THE-EXPECTED-REGION-ID\"," +
                "        \"sriov-automation\": false," +
                "        \"resource-version\": \"1513631040564\"" +
                "      }" +
                "    }," +
                "    {" +
                "      \"id\": \"2979598424\"," +
                "      \"node-type\": \"generic-vnf\"," +
                "      \"url\": \"/aai/v12/network/generic-vnfs/generic-vnf/SOURCE-gVnf-E1802\"," +
                "      \"properties\": {" +
                "        \"vnf-id\": \"SOURCE-gVnf-E1802\"," +
                "        \"vnf-name\": \"SOURCE-vnf-SDNO\"," +
                "        \"vnf-type\": \"S-1-SDNO\"," +
                "        \"service-id\": \"a9a77d5a-123e-4-SDNO\"," +
                "        \"orchestration-status\": \"active\"," +
                "        \"in-maint\": true," +
                "        \"is-closed-loop-disabled\": false," +
                "        \"resource-version\": \"1513631043149\"" +
                "      }" +
                "    }" +
                "  ]" +
                "}");

        PortMirroringConfigData portMirroringConfigData =
                new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);

        assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
        assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));

    }

    @Test
    public void extractPortMirroringConfigData_givenKindOfValidAaiResponse_yieldCloudRegionId() throws IOException {
        // some completley different response, but with
        // the results[cloud-region]->properties->cloud-region-id

        final JsonNode aaiPayload = objectMapper.readTree("" +
                "{  " +
                "  \"results\": [{  " +
                "      \"node-type\": \"generic-vnf\",  " +
                "      \"url\": \"configuration entries) so that git\"  " +
                "    },  " +
                "    {},  " +
                "    {  " +
                "      \"node-type\": \"cloud-region\",  " +
                "      \"but it will not switch\": \"tip commits are reachable\",  " +
                "      \"named\": [{  " +
                "        \"resource-version\": \"1513631040564\"  " +
                "      }],  " +
                "      \"properties\": {  " +
                "        \"cloud-region-id\": \"THE-EXPECTED-REGION-ID\",  " +
                "        \"oldbranch> will be renamed\": false  " +
                "      }  " +
                "    },  " +
                "    {  " +
                "      \"node-type\": [\"generic-vnf\", \"can be overridden by using\"]  " +
                "    }  " +
                "  ]  " +
                "}");

        PortMirroringConfigData portMirroringConfigData =
                new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);

        assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataOk.class)));
        assertThat(((PortMirroringConfigDataOk) portMirroringConfigData).getCloudRegionId(), is("THE-EXPECTED-REGION-ID"));

    }

    public void extractPortMirroringConfigData_givenAaiResponseWithoutRegionIdName_yieldException() throws IOException {

        final JsonNode aaiPayload = objectMapper.readTree("" +
                "{" +
                "  \"results\": [{" +
                "      \"node-type\": \"cloud-region\"," +
                "      \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/att-aic/SDNO-S-BcloudReg-E1802\"," +
                "      \"properties\": {" +
                "        \"resource-version\": \"1513631040564\"" +
                "      }" +
                "    }" +
                "  ]" +
                "}");

        PortMirroringConfigData portMirroringConfigData =
                new AaiResponseTranslator().extractPortMirroringConfigData(aaiPayload);

        assertThat(portMirroringConfigData, is(instanceOf(PortMirroringConfigDataError.class)));
        assertThat(((PortMirroringConfigDataError) portMirroringConfigData).getErrorDescription(),
                containsString("The node-type 'cloud-region' does not contain the property 'cloud-region-id'"));
        assertThat(((PortMirroringConfigDataError) portMirroringConfigData).getRawAaiResponse(),
                containsString(aaiPayload.toString())
        );

    }

    /*
    More tests:
    [x]  cloud-region-id field is missing -- descriptive exception is thrown, including the problematic payload itself
    [ ]  cloud-region-id field is empty -- descriptive exception etc.
    [ ]  node-type=="cloud-region" entry is empty -- descriptive exception etc.
     */
}