aboutsummaryrefslogtreecommitdiffstats
path: root/generic-resource-api/provider/src/test/java/org/onap/sdnc/northbound/PreloadNetworkTopologyRPCTest.java
blob: 6e0f1a2ee95612c5baea2c2ff275b8af6634db7e (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
package org.onap.sdnc.northbound;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.onap.sdnc.northbound.GenericResourceApiProvider.APP_NAME;
import static org.onap.sdnc.northbound.GenericResourceApiProvider.NO_SERVICE_LOGIC_ACTIVE;
import static org.onap.sdnc.northbound.util.MDSALUtil.build;
import static org.onap.sdnc.northbound.util.MDSALUtil.exec;
import static org.onap.sdnc.northbound.util.MDSALUtil.networkTopologyIdentifierStructureBuilder;
import static org.onap.sdnc.northbound.util.MDSALUtil.preloadNetworkTopologyInformationBuilder;
import static org.onap.sdnc.northbound.util.MDSALUtil.preloadNetworkTopologyOperationInput;
import static org.onap.sdnc.northbound.util.MDSALUtil.preloadNetworkTopologyOperationOutput;
import static org.onap.sdnc.northbound.util.MDSALUtil.requestInformation;
import static org.onap.sdnc.northbound.util.MDSALUtil.sdncRequestHeader;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.sdnc.northbound.util.PropBuilder;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.PreloadNetworkTopologyOperationInput;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.PreloadNetworkTopologyOperationOutput;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformation;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader.SvcAction;
import org.opendaylight.yangtools.yang.common.RpcResult;

@RunWith(MockitoJUnitRunner.class)
public class PreloadNetworkTopologyRPCTest extends GenericResourceApiProviderTest {

    private static final String SVC_OPERATION = "preload-network-topology-operation";

    @Before
    public void setUp() throws Exception {
        super.setUp();
        svcClient.setScvOperation(SVC_OPERATION);
    }

    @Test
    public void should_fail_when_invalid_network_topology() throws Exception {

        PreloadNetworkTopologyOperationInput input = build(preloadNetworkTopologyOperationInput());

        PreloadNetworkTopologyOperationOutput output =
            exec(genericResourceApiProvider::preloadNetworkTopologyOperation, input, RpcResult::getResult);

        assertEquals("403", output.getResponseCode());
        assertEquals("invalid input, null or empty preload-network-topology-information", output.getResponseMessage());
        assertEquals("Y", output.getAckFinalIndicator());
    }


    @Test
    public void should_fail_when_client_execution_failed() throws Exception {

        svcClient.mockHasGraph(true);
        svcClient.mockExecuteWoServiceDataPreload(new RuntimeException("test exception"));

        PreloadNetworkTopologyOperationInput input = build(preloadNetworkTopologyOperationInput()
            .setPreloadNetworkTopologyInformation(build(preloadNetworkTopologyInformationBuilder()
                .setNetworkTopologyIdentifierStructure(build(networkTopologyIdentifierStructureBuilder()
                    .setNetworkName("test-network-name")
                    .setNetworkType("test-network-type")))))
        );

        PreloadNetworkTopologyOperationOutput output =
            exec(genericResourceApiProvider::preloadNetworkTopologyOperation, input, RpcResult::getResult);

        assertEquals("500", output.getResponseCode());
        assertEquals("test exception", output.getResponseMessage());
        assertEquals("Y", output.getAckFinalIndicator());
    }

    @Test
    public void should_fail_when_client_has_no_graph() throws Exception {

        svcClient.mockHasGraph(false);

        PreloadNetworkTopologyOperationInput input = build(preloadNetworkTopologyOperationInput()
            .setPreloadNetworkTopologyInformation(build(preloadNetworkTopologyInformationBuilder()
                .setNetworkTopologyIdentifierStructure(build(networkTopologyIdentifierStructureBuilder()
                    .setNetworkName("test-network-name")
                    .setNetworkType("test-network-type")))))
        );

        PreloadNetworkTopologyOperationOutput output =
            exec(genericResourceApiProvider::preloadNetworkTopologyOperation, input, RpcResult::getResult);

        assertEquals("503", output.getResponseCode());
        assertEquals(NO_SERVICE_LOGIC_ACTIVE + APP_NAME + ": '" + SVC_OPERATION + "'", output.getResponseMessage());
        assertEquals("Y", output.getAckFinalIndicator());
    }

    @Test
    public void should_fail_when_failed_to_update_mdsal() throws Exception {

        PropBuilder svcResultProp = svcClient.createExecuteOKResult();
        svcClient.mockExecuteWoServiceData(svcResultProp);
        svcClient.mockHasGraph(true);
        WriteTransaction mockWriteTransaction = mock(WriteTransaction.class);
        when(mockWriteTransaction.submit()).thenThrow(new TransactionChainClosedException("test exception"));

        DataBroker spyDataBroker = Mockito.spy(dataBroker);
        when(spyDataBroker.newWriteOnlyTransaction()).thenReturn(mockWriteTransaction);
        genericResourceApiProvider.setDataBroker(spyDataBroker);

        PreloadNetworkTopologyOperationInput input = build(preloadNetworkTopologyOperationInput()
            .setPreloadNetworkTopologyInformation(build(preloadNetworkTopologyInformationBuilder()
                .setNetworkTopologyIdentifierStructure(build(networkTopologyIdentifierStructureBuilder()
                    .setNetworkName("test-network-name")
                    .setNetworkType("test-network-type")))))
        );

        PreloadNetworkTopologyOperationOutput output =
            exec(genericResourceApiProvider::preloadNetworkTopologyOperation, input, RpcResult::getResult);

        assertEquals("500", output.getResponseCode());
        assertEquals("test exception", output.getResponseMessage());
        assertEquals("Y", output.getAckFinalIndicator());
    }

    @Test
    public void should_complete_with_success_when_no_errors() throws Exception {

        svcClient.mockHasGraph(true);
        PropBuilder svcResultProp = svcClient.createExecuteOKResult();
        svcClient.mockExecute(svcResultProp);

        PreloadNetworkTopologyOperationInput input = build(preloadNetworkTopologyOperationInput()
            .setPreloadNetworkTopologyInformation(build(preloadNetworkTopologyInformationBuilder()
                .setNetworkTopologyIdentifierStructure(build(networkTopologyIdentifierStructureBuilder()
                    .setNetworkName("test-network-name")
                    .setNetworkType("test-network-type")))))
            .setSdncRequestHeader(build(sdncRequestHeader()
                .setSvcRequestId("test-svc-request-id")
                .setSvcAction(SvcAction.Assign)
            ))
            .setRequestInformation(build(requestInformation()
                .setRequestId("test-request-id")
                .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
            ))
        );

        PreloadNetworkTopologyOperationOutput output =
            exec(genericResourceApiProvider::preloadNetworkTopologyOperation, input, RpcResult::getResult);

        assertEquals("200", output.getResponseCode());
        assertEquals("Y", output.getAckFinalIndicator());

        PreloadNetworkTopologyOperationOutput expectedOutput = createExpectedOutput(svcResultProp, input);
        assertEquals(expectedOutput, output);
    }

    private PreloadNetworkTopologyOperationOutput createExpectedOutput(PropBuilder svcResultProp,
        PreloadNetworkTopologyOperationInput input) {
        return build(preloadNetworkTopologyOperationOutput()
            .setSvcRequestId(input.getSdncRequestHeader().getSvcRequestId())
            .setResponseCode(svcResultProp.get(svcClient.errorCode))
            .setAckFinalIndicator(svcResultProp.get(svcClient.ackFinal))
        );
    }

}