eb530d55b59c02e83a7004628f5bb193d05e947c
[usecase-ui/intent-analysis.git] /
1 /*
2  *
3  *  * Copyright (C) 2022 CMCC, Inc. and others. All rights reserved.
4  *  *
5  *  * Licensed under the Apache License, Version 2.0 (the "License");
6  *  * you may not use this file except in compliance with the License.
7  *  * You may obtain a copy of the License at
8  *  *
9  *  *     http://www.apache.org/licenses/LICENSE-2.0
10  *  *
11  *  * Unless required by applicable law or agreed to in writing, software
12  *  * distributed under the License is distributed on an "AS IS" BASIS,
13  *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  * See the License for the specific language governing permissions and
15  *  * limitations under the License.
16  *
17  */
18
19 package org.onap.usecaseui.intentanalysis.service;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.junit.Assert;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.runner.RunWith;
27 import org.onap.usecaseui.intentanalysis.bean.enums.ObjectType;
28 import org.onap.usecaseui.intentanalysis.bean.models.Context;
29 import org.onap.usecaseui.intentanalysis.bean.models.ExpectationObject;
30 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
31 import org.onap.usecaseui.intentanalysis.IntentAnalysisApplicationTests;
32 import org.onap.usecaseui.intentanalysis.util.SpringContextUtil;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.boot.test.context.SpringBootTest;
35 import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
36 import org.springframework.test.context.junit4.SpringRunner;
37
38 @SpringBootTest(classes = IntentAnalysisApplicationTests.class)
39 @RunWith(SpringRunner.class)
40 class ExpectationObjectServiceTest extends AbstractJUnit4SpringContextTests {
41
42     private static final String TEST_EXPECTATION_ID_1 = "expectationId1";
43
44     private static final String TEST_EXPECTATION_ID_2 = "expectation without affiliate";
45
46     private static final String TEST_EXPECTATION_ID_3 = "expectationId3";
47
48     @Autowired
49     private ExpectationObjectService expectationObjectService;
50
51     public ExpectationObject createTestObject(String testName) {
52         ExpectationObject expectationObject = new ExpectationObject();
53         expectationObject.setObjectType(ObjectType.SLICING);
54         expectationObject.setObjectInstance("true");
55
56         List<Context> contextList = new ArrayList<>();
57         Context context = new Context();
58         context.setContextId(testName + "-contextId");
59         context.setContextName(testName + "-contextName");
60         contextList.add(context);
61         expectationObject.setObjectContexts(contextList);
62
63         return expectationObject;
64     }
65
66     @BeforeEach
67     void setUp() {
68         SpringContextUtil.setApplicationContext(applicationContext);
69     }
70
71     @Test
72     void testCreateExpectationObjectFalse() {
73         ExpectationObject expectationObject = createTestObject("testCreateExpectationObjectFalse");
74
75         try {
76             expectationObjectService.createExpectationObject(expectationObject, TEST_EXPECTATION_ID_1);
77         } catch (DataBaseException exception) {
78             exception.printStackTrace();
79             String msg = String.format("It already exists an object for the expectation %s, update might work.", TEST_EXPECTATION_ID_1);
80             Assert.assertEquals(msg, exception.getMessage());
81         }
82     }
83
84     @Test
85     void testCreateExpectationObjectSuccess() {
86         ExpectationObject expectationObject = createTestObject("testCreateExpectationObjectSuccess");
87
88         try {
89             expectationObjectService.createExpectationObject(expectationObject, TEST_EXPECTATION_ID_2);
90         } catch (DataBaseException exception) {
91             exception.printStackTrace();
92         }
93         Assert.assertNotNull(expectationObjectService.getExpectationObject(TEST_EXPECTATION_ID_2));
94     }
95
96     @Test
97     void testGetExpectationObjectSuccess() {
98         ExpectationObject expectationObject = expectationObjectService.getExpectationObject(TEST_EXPECTATION_ID_1);
99         Assert.assertNotNull(expectationObject);
100     }
101
102     @Test
103     void testUpdateExpectationObjectSuccess() {
104         String testName = "testUpdateExpectationObjectSuccess_1";
105         ExpectationObject expectationObject = createTestObject(testName);
106
107         try {
108             expectationObjectService.updateExpectationObject(expectationObject, TEST_EXPECTATION_ID_1);
109         } catch (DataBaseException exception) {
110             exception.printStackTrace();
111         }
112
113         ExpectationObject expectationObjectTmp = expectationObjectService.getExpectationObject(TEST_EXPECTATION_ID_1);
114         Assert.assertEquals(expectationObjectTmp.getObjectContexts().get(0).getContextName(), testName + "-contextName");
115     }
116
117     @Test
118     void testUpdateExpectationObjectToNullSuccess() {
119         ExpectationObject expectationObject = new ExpectationObject();
120         expectationObject.setObjectType(ObjectType.CCVPN);
121
122         try {
123             expectationObjectService.updateExpectationObject(expectationObject, TEST_EXPECTATION_ID_2);
124         } catch (DataBaseException exception) {
125             exception.printStackTrace();
126         }
127
128         ExpectationObject expectationObjectUpdated = expectationObjectService.getExpectationObject(TEST_EXPECTATION_ID_2);
129         Assert.assertNotNull(expectationObjectUpdated);
130     }
131
132     @Test
133     void  testDeleteExpectationObjectSuccess() {
134         try {
135             expectationObjectService.deleteExpectationObject(TEST_EXPECTATION_ID_3);
136         } catch (DataBaseException exception) {
137             exception.printStackTrace();
138         }
139
140         ExpectationObject expectationObject = expectationObjectService.getExpectationObject(TEST_EXPECTATION_ID_3);
141         Assert.assertNull(expectationObject);
142     }
143 }