aboutsummaryrefslogtreecommitdiffstats
path: root/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/service/ExpectationServiceTest.java
blob: 6f10f1078c907cab98e4ea7128446ec230d2d0a0 (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
/*
 *
 *  * Copyright (C) 2022 CMCC, Inc. and others. 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.
 *
 */

package org.onap.usecaseui.intentanalysis.service;

import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.onap.usecaseui.intentanalysis.bean.enums.ExpectationType;
import org.onap.usecaseui.intentanalysis.bean.enums.FulfilmentStatus;
import org.onap.usecaseui.intentanalysis.bean.enums.NotFulfilledState;
import org.onap.usecaseui.intentanalysis.bean.enums.ObjectType;
import org.onap.usecaseui.intentanalysis.bean.enums.OperatorType;
import org.onap.usecaseui.intentanalysis.bean.models.Condition;
import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
import org.onap.usecaseui.intentanalysis.bean.models.ExpectationObject;
import org.onap.usecaseui.intentanalysis.bean.models.ExpectationTarget;
import org.onap.usecaseui.intentanalysis.bean.models.FulfilmentInfo;
import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
import org.onap.usecaseui.intentanalysis.IntentAnalysisApplicationTests;
import org.onap.usecaseui.intentanalysis.util.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;


@SpringBootTest(classes = IntentAnalysisApplicationTests.class)
@RunWith(SpringRunner.class)
class ExpectationServiceTest extends AbstractJUnit4SpringContextTests {

    private static final String TEST_INTENT_ID_1 = "intentId1";

    private static final String TEST_INTENT_ID_2 = "intentId2";

    private static final String TEST_INTENT_ID_3 = "intent without affiliate";

    private static final String TEST_EXPECTATION_ID_1 = "expectationId1";

    private static final String TEST_EXPECTATION_ID_2= "expectationId2";

    @Autowired
    private ExpectationService expectationService;

    public Expectation createTestExpectation(String testName) {
        Expectation expectation = new Expectation();

        ExpectationObject object = new ExpectationObject();
        object.setObjectType(ObjectType.valueOf("SLICING"));
        object.setObjectInstance("objectInstance");

        Condition targetCondition = new Condition();
        targetCondition.setConditionId(testName + "-conditionId");
        targetCondition.setConditionName(testName + "conditionName");
        targetCondition.setOperator(OperatorType.valueOf("EQUALTO"));
        targetCondition.setConditionValue("conditionValue");
        List<Condition> targetConditionList = new ArrayList<>();
        targetConditionList.add(targetCondition);

        ExpectationTarget target = new ExpectationTarget();
        target.setTargetId(testName + "targetId");
        target.setTargetName(testName + "targetName");
        target.setTargetConditions(targetConditionList);
        List<ExpectationTarget> expectationTargetList = new ArrayList<>();
        expectationTargetList.add(target);

        FulfilmentInfo expectationFulfilmentInfo = new FulfilmentInfo();
        expectationFulfilmentInfo.setFulfillmentStatus(FulfilmentStatus.valueOf("NOT_FULFILLED"));
        expectationFulfilmentInfo.setNotFulfilledReason("NotFulfilledReason");
        expectationFulfilmentInfo.setNotFulfilledState(NotFulfilledState.valueOf("COMPLIANT"));

        expectation.setExpectationId(testName + "-expectationId");
        expectation.setExpectationName(testName + "expectationName");
        expectation.setExpectationType(ExpectationType.valueOf("DELIVERY"));
        expectation.setExpectationObject(object);
        expectation.setExpectationTargets(expectationTargetList);
        expectation.setExpectationFulfilmentInfo(expectationFulfilmentInfo);

        return expectation;
    }

    @BeforeEach
    public void setUp() {
        SpringContextUtil.setApplicationContext(applicationContext);
    }

    @Test
    public void testCreateIntentExpectationSuccess() {
        Expectation expectation = createTestExpectation("testCreateIntentExpectationSuccess");

        try {
            expectationService.createIntentExpectation(expectation, TEST_INTENT_ID_3);
        } catch (DataBaseException exception) {
            exception.printStackTrace();
        }

        Assert.assertNotNull(expectationService.getIntentExpectation(expectation.getExpectationId()));
    }

    @Test
    public void testCreateIntentExpectationListSuccess() {
        List<Expectation> expectationList = new ArrayList<>();
        Expectation expectation = createTestExpectation("testCreateIntentExpectationListSuccess");
        expectationList.add(expectation);

        try {
            expectationService.createIntentExpectationList(expectationList, TEST_INTENT_ID_3);
        } catch (DataBaseException exception) {
            exception.printStackTrace();
        }

        Assert.assertNotNull(expectationService.getIntentExpectation(expectation.getExpectationId()));
    }

    @Test
    public void testDeleteIntentExpectationListSuccess() {
        try {
            expectationService.deleteIntentExpectation(TEST_EXPECTATION_ID_1);
        } catch (DataBaseException exception) {
            exception.printStackTrace();
        }

        Assert.assertNull(expectationService.getIntentExpectation(TEST_EXPECTATION_ID_1));
    }

    @Test
    public void deleteIntentExpectationSuccess() {
        try {
            expectationService.deleteIntentExpectationList(TEST_INTENT_ID_2);
        } catch (DataBaseException exception) {
            exception.printStackTrace();
        }

        Assert.assertTrue(CollectionUtils.isEmpty(expectationService.getIntentExpectationList(TEST_INTENT_ID_2)));
    }

    @Test
    public void testUpdateIntentExpectationListSuccess() {
        List<Expectation> expectationList = new ArrayList<>();
        Expectation expectation = createTestExpectation("testUpdateIntentExpectationListSuccess");
        expectationList.add(expectation);

        try {
            expectationService.updateIntentExpectationList(expectationList, TEST_INTENT_ID_2);
        } catch (DataBaseException exception) {
            exception.printStackTrace();
        }

        Expectation updatedExpectation = expectationService.getIntentExpectationList(TEST_INTENT_ID_2).get(0);
        Assert.assertEquals(updatedExpectation.getExpectationId(), expectation.getExpectationId());
    }

    @Test
    public void testGetIntentExpectationListSuccess() {
        List<Expectation> expectationList = expectationService.getIntentExpectationList(TEST_INTENT_ID_1);
        Assert.assertFalse(CollectionUtils.isEmpty(expectationList));
    }

    @Test
    public void testGetIntentExpectationSuccess() {
        Expectation expectationGotten = expectationService.getIntentExpectation(TEST_EXPECTATION_ID_2);
        Assert.assertNotNull(expectationGotten);
    }

}