aboutsummaryrefslogtreecommitdiffstats
path: root/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/service/ContextServiceTest.java
blob: 9ef0c7338e96730adabc5c3bf8ab53bc0e59ef9a (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
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.IntentAnalysisApplicationTests;
import org.onap.usecaseui.intentanalysis.bean.models.Context;
import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
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;


@SpringBootTest(classes = IntentAnalysisApplicationTests.class)
@RunWith(SpringRunner.class)
class ContextServiceTest 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 = "expectation without affiliate";

    private static final String TEST_CONTEXT_ID_1 = "d64f3a5f-b091-40a6-bca0-1bc6b1ce8f43";

    @Autowired
    private ContextService contextService;

    public Context createTestContext(String testName) {
        Context context = new Context();
        context.setContextId(testName + "-contextId");
        context.setContextName(testName + "contextName");

        return context;
    }

    public List<Context> createTestContextList(String testName) {
        List<Context> contextList = new ArrayList<>();
        contextList.add(createTestContext(testName));
        return contextList;
    }

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

    @Test
    void testCreateContextListSuccess() {
        List<Context> contextList = createTestContextList("testCreateContextListSuccess");

        try {
            contextService.createContextList(contextList, TEST_INTENT_ID_3);
        } catch (DataBaseException exception) {
            exception.printStackTrace();
        }

        Assert.assertNotNull(contextService.getContextList(TEST_INTENT_ID_3));
    }

    @Test
    void testCreateContextSuccess() {
        Context context = createTestContext("testCreateContextSuccess");

        try {
            contextService.createContext(context, TEST_INTENT_ID_1);
        } catch (DataBaseException exception) {
            exception.printStackTrace();
        }

        Assert.assertNotNull(contextService.getContext(context.getContextId()));
    }

    @Test
    void testDeleteContextSuccess() {
        contextService.deleteContext(TEST_CONTEXT_ID_1);
        Context context = contextService.getContext(TEST_CONTEXT_ID_1);
        Assert.assertNull(context);
    }

    @Test
    void testUpdateContextListSuccess() {
        List<Context> contextList = contextService.getContextList(TEST_INTENT_ID_2);
        Context context = contextList.get(0);
        context.setContextName("new context name");

        try {
            contextService.updateContextList(contextList, TEST_INTENT_ID_2);
        } catch (DataBaseException exception) {
            exception.printStackTrace();
        }

        Context contextTmp = contextService.getContextList(TEST_INTENT_ID_2).get(0);
        Assert.assertEquals("new context name", contextTmp.getContextName());
    }

    @Test
    void testUpdateContextListSuccessToNull() {
        List<Context> contextList = createTestContextList("testUpdateContextListSuccess_2");

        try {
            contextService.updateContextList(contextList, TEST_EXPECTATION_ID);
        } catch (DataBaseException exception) {
            exception.printStackTrace();
        }

        Assert.assertNotNull(contextService.getContextList(TEST_EXPECTATION_ID));
    }

    @Test
    void testGetContextListSuccess() {
        List<Context> contextList = contextService.getContextList(TEST_INTENT_ID_1);
        Assert.assertNotNull(contextList);
    }
}