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