aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/a1pesimulator/service/VesBrokerServiceImplTest.java
blob: 2c2d9ffef44dfd4837a5de783e557b79543a4441 (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
/*
 * Copyright (C) 2021 Samsung Electronics
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package org.onap.a1pesimulator.service;

import static org.mockito.Mockito.when;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.onap.a1pesimulator.data.ReportingMethodEnum;
import org.onap.a1pesimulator.data.ves.VesEvent;
import org.onap.a1pesimulator.service.ves.RanVesBrokerService;
import org.onap.a1pesimulator.service.ves.RanVesSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(SpringRunner.class)
@SpringBootTest
public class VesBrokerServiceImplTest {

    private static final String VES_COLLECTOR_URL = "someProtocol://someVesCollectorIP:someVesCollectorPort/somePath";

    @Autowired
    private RanVesBrokerService vesBrokerService;
    @Autowired
    private RanVesSender vesSender;
    @Autowired
    private ObjectMapper mapper;
    @Mock
    private RestTemplate restTemplate;

    @Before
    public void before() {
        ReflectionTestUtils.setField(vesSender, "restTemplate", restTemplate);
    }

    @Test
    public void testStartSendingVes() throws Exception {
        ResponseEntity<String> responseEntity = new ResponseEntity<>(HttpStatus.OK);

        when(restTemplate.exchange(ArgumentMatchers.eq(VES_COLLECTOR_URL), ArgumentMatchers.eq(HttpMethod.POST),
                ArgumentMatchers.any(HttpEntity.class), ArgumentMatchers.eq(String.class))).thenReturn(responseEntity);

        ResponseEntity<String> response = vesBrokerService.startSendingVesEvents("CustomIdentifier",
                loadEventFromFile("VesBrokerControllerTest_pm_ves.json"), 10, ReportingMethodEnum.VES);

        Assert.assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
    }

    private VesEvent loadEventFromFile(String fileName) throws Exception {
        return mapper.readValue(loadFileContent(fileName), VesEvent.class);
    }

    private String loadFileContent(String fileName) throws IOException, URISyntaxException {
        Path path = Paths.get(VesBrokerServiceImplTest.class.getResource(fileName).toURI());
        return new String(Files.readAllBytes(path));
    }
}