89dd2f554fc3f27e5641e9e38924b131487e9c85
[so.git] / common / src / test / java / org / onap / so / client / sdno / SDNOValidatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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.client.sdno;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.Arrays;
33 import java.util.UUID;
34
35 import org.hamcrest.core.StringContains;
36 import org.junit.Before;
37 import org.junit.Ignore;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.mockito.Spy;
44 import org.onap.aai.domain.yang.GenericVnf;
45 import org.onap.so.client.dmaap.Consumer;
46 import org.onap.so.client.dmaap.exceptions.DMaaPConsumerFailure;
47 import org.onap.so.client.exceptions.SDNOException;
48 import org.onap.so.client.sdno.beans.SDNO;
49 import org.onap.so.client.sdno.dmaap.SDNOHealthCheckDmaapConsumer;
50
51 import com.fasterxml.jackson.databind.ObjectMapper;
52
53
54 public class SDNOValidatorTest {
55
56         
57         @Mock private Consumer mrConsumer;
58         @Spy private SDNOHealthCheckDmaapConsumer dmaapConsumer;
59         private final String fileLocation = "src/test/resources/org/onap/so/client/sdno/";
60         private final String uuid = "xyz123";
61         @Rule public ExpectedException thrown = ExpectedException.none();
62         
63         @Before
64         public void setUpTests() {
65                 MockitoAnnotations.initMocks(this);
66         }
67         
68         @Test
69         public void success() throws IOException, Exception {
70                 when(dmaapConsumer.getConsumer()).thenReturn(mrConsumer);
71                 when(mrConsumer.fetch()).thenReturn(Arrays.asList(new String[]{getJson("response.json"), getJson("output-success.json")}));
72                 
73                 SDNOValidatorImpl validator = new SDNOValidatorImpl();
74                 SDNOValidatorImpl spy = spy(validator);
75                 when(dmaapConsumer.getRequestId()).thenReturn("xyz123");
76                 doReturn(dmaapConsumer).when(spy).getConsumer(any(String.class));
77                 boolean result = spy.pollForResponse("xyz123");
78                 assertEquals("result is true", result, true);
79         }
80         
81         @Test
82         public void failure() throws IOException, Exception {
83                 when(dmaapConsumer.getConsumer()).thenReturn(mrConsumer);
84                 when(mrConsumer.fetch()).thenReturn(Arrays.asList(new String[]{getJson("response.json"), getJson("output-failure.json")}));
85                 
86                 SDNOValidatorImpl validator = new SDNOValidatorImpl();
87                 SDNOValidatorImpl spy = spy(validator);
88                 when(dmaapConsumer.getRequestId()).thenReturn("xyz123");
89                 doReturn(dmaapConsumer).when(spy).getConsumer(any(String.class));
90                 thrown.expect(SDNOException.class);
91                 thrown.expectMessage(new StringContains("my error message"));
92                 boolean result = spy.pollForResponse("xyz123");
93                 
94         }
95         @Ignore
96         @Test
97         public void run() throws Exception {
98                 SDNOValidatorImpl validator = new SDNOValidatorImpl();
99                 UUID uuid = UUID.randomUUID();
100                 GenericVnf vnf = new GenericVnf();
101                 vnf.setVnfId("test");
102                 vnf.setIpv4OamAddress("1.2.3.4");
103                 vnf.setVnfType("VPE");
104                 SDNO request = validator.buildRequestDiagnostic(vnf, uuid, "mechid");
105                 ObjectMapper mapper = new ObjectMapper();
106                 String json = mapper.writeValueAsString(request);
107                 validator.submitRequest(json);
108                 thrown.expect(DMaaPConsumerFailure.class);
109                 boolean result = validator.pollForResponse(uuid.toString());
110                 System.out.println(json);
111
112         }
113         private String getJson(String filename) throws IOException {
114                 return new String(Files.readAllBytes(Paths.get(fileLocation + filename)));
115         }
116 }