aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/service/PdpStatisticsServiceTest.java
blob: 7bfc99739eb1f887772aa94f87a2ec1b8ebce4d9 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2022 Bell Canada. All rights reserved.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.models.pdp.concepts.PdpStatistics;
import org.onap.policy.pap.main.repository.PdpStatisticsRepository;
import org.onap.policy.pap.main.rest.CommonPapRestServer;
import org.springframework.beans.factory.annotation.Autowired;

public class PdpStatisticsServiceTest extends CommonPapRestServer {

    private static final String NAME3 = "name3";
    private static final String NAME1 = "name1";
    private static final String LIST_IS_NULL = "pdpStatisticsList is marked .*ull but is null";
    private static final String GROUP0 = "group0";
    private static final String GROUP = "group";
    private static final String SUBGROUP = "subgroup";
    private static final Instant TIMESTAMP1 = Instant.ofEpochSecond(1078884319);
    private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1078884350);
    private static final Integer NUMBER_RECORDS = 10;

    @Autowired
    private PdpStatisticsService pdpStatisticsService;

    @Autowired
    private PdpStatisticsRepository pdpStatisticsRepository;

    private PdpStatistics pdpStatistics1;
    private PdpStatistics pdpStatistics2;
    private PdpStatistics pdpStatistics3;
    private PdpStatistics pdpStatistics4;

    /**
     * Setup before tests.
     *
     * @throws Exception the exception
     */
    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
        pdpStatistics1 = generatePdpStatistics(NAME1, TIMESTAMP1, GROUP, SUBGROUP);
        pdpStatistics2 = generatePdpStatistics("name2", TIMESTAMP1, GROUP, SUBGROUP);
        pdpStatistics3 = generatePdpStatistics(NAME1, TIMESTAMP2, GROUP, SUBGROUP);
        pdpStatistics4 = generatePdpStatistics(NAME3, TIMESTAMP2, GROUP0, SUBGROUP);
    }

    /**
     * Teardown after tests.
     */
    @Override
    @After
    public void tearDown() {
        pdpStatisticsRepository.deleteAll();
    }

    @Test
    public void testCreatePdpStatisticsSuccess() {
        List<PdpStatistics> createList = List.of(pdpStatistics1, pdpStatistics3, pdpStatistics4, pdpStatistics2);
        List<PdpStatistics> createdPdpStatisticsList = pdpStatisticsService.createPdpStatistics(createList);
        // these should match AND be in the same order
        assertThat(createdPdpStatisticsList).isEqualTo(createList);
    }

    @Test
    public void testCreatePdpStatisticsFailure() {

        assertThatThrownBy(() -> {
            pdpStatisticsService.createPdpStatistics(null);
        }).hasMessageMatching(LIST_IS_NULL);

        PdpStatistics pdpStatisticsErr = new PdpStatistics();
        pdpStatisticsErr.setPdpInstanceId("NULL");
        pdpStatisticsErr.setPdpGroupName(GROUP);
        assertThatThrownBy(() -> {
            pdpStatisticsService.createPdpStatistics(List.of(pdpStatisticsErr));
        }).hasMessageContaining("item \"name\" value \"NULL\" INVALID, is null");
    }

    @Test
    public void testFetchDatabaseStatistics() {

        List<PdpStatistics> createList = List.of(pdpStatistics1, pdpStatistics3, pdpStatistics4, pdpStatistics2);
        pdpStatisticsService.createPdpStatistics(createList);
        Map<String, Map<String, List<PdpStatistics>>> created =
            pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, null, null);
        assertThat(created).hasSize(2);
        assertThat(created.get(GROUP0)).hasSize(1);
        assertThat(created.get(GROUP0).get(SUBGROUP)).hasSize(1);
        assertThat(created.get(GROUP)).hasSize(1);
        assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(3);

        created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, TIMESTAMP2);
        assertThat(created).hasSize(2);
        assertThat(created.get(GROUP0)).hasSize(1);
        assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
        assertThat(created.get(GROUP)).hasSize(1);
        assertThat(created.get(GROUP).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics3));

        created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, null, TIMESTAMP1);
        assertThat(created).hasSize(1);
        assertThat(created.get(GROUP)).hasSize(1);
        assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);

        created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, null);
        assertThat(created).hasSize(2);

        created = pdpStatisticsService.fetchDatabaseStatistics(GROUP0, NUMBER_RECORDS, TIMESTAMP2, TIMESTAMP2);
        assertThat(created).hasSize(1);
        assertThat(created.get(GROUP0)).hasSize(1);
        assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));

        created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, null, TIMESTAMP1);
        assertThat(created).hasSize(1);
        assertThat(created.get(GROUP)).hasSize(1);
        assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);

        created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, TIMESTAMP2, null);
        assertThat(created).hasSize(1);

        created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, TIMESTAMP1, TIMESTAMP2);
        assertThat(created).hasSize(1);
        assertThat(created.get(GROUP)).hasSize(1);
        assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(3);

        created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, null, TIMESTAMP1);
        assertThat(created).hasSize(1);
        assertThat(created.get(GROUP)).hasSize(1);
        assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);

        created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, TIMESTAMP2, null);
        assertThat(created).hasSize(1);
        assertThat(created.get(GROUP).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics3));

        created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, TIMESTAMP1,
            TIMESTAMP2);
        assertThat(created).hasSize(1);
        assertThat(created.get(GROUP)).hasSize(1);
        assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);

        created =
            pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, null, TIMESTAMP1);
        assertThat(created).hasSize(1);
        assertThat(created.get(GROUP)).hasSize(1);
        assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(1);

        created =
            pdpStatisticsService.fetchDatabaseStatistics(GROUP0, SUBGROUP, NAME3, NUMBER_RECORDS, TIMESTAMP2, null);
        assertThat(created).hasSize(1);
        assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
    }

    private PdpStatistics generatePdpStatistics(String pdpInstanceId, Instant date, String group,
        String subgroup) {
        PdpStatistics pdpStatistics11 = new PdpStatistics();
        pdpStatistics11.setPdpInstanceId(pdpInstanceId);
        pdpStatistics11.setTimeStamp(date);
        pdpStatistics11.setPdpGroupName(group);
        pdpStatistics11.setPdpSubGroupName(subgroup);
        pdpStatistics11.setPolicyDeployCount(2);
        pdpStatistics11.setPolicyDeployFailCount(1);
        pdpStatistics11.setPolicyDeploySuccessCount(1);
        pdpStatistics11.setPolicyExecutedCount(2);
        pdpStatistics11.setPolicyExecutedFailCount(1);
        pdpStatistics11.setPolicyExecutedSuccessCount(1);
        pdpStatistics11.setEngineStats(new ArrayList<>());

        return pdpStatistics11;
    }
}