15b9cb43a03ebcc411a94e3da76e34cd46c06e17
[clamp.git] / src / test / java / org / onap / clamp / loop / LoopLogServiceTestItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Huawei Technologies Co., Ltd.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END============================================
19  * ===================================================================
20  *
21  */
22
23 package org.onap.clamp.loop;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26
27 import com.google.gson.JsonObject;
28
29 import java.util.Set;
30
31 import javax.transaction.Transactional;
32
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.onap.clamp.clds.Application;
36 import org.onap.clamp.clds.util.JsonUtils;
37 import org.onap.clamp.loop.log.LogType;
38 import org.onap.clamp.loop.log.LoopLog;
39 import org.onap.clamp.loop.log.LoopLogService;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.test.context.junit4.SpringRunner;
43
44 @RunWith(SpringRunner.class)
45 @SpringBootTest(classes = Application.class)
46 public class LoopLogServiceTestItCase {
47
48     private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest";
49     private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}";
50     private static final String CLAMP_COMPONENT = "CLAMP";
51     private static final String SAMPLE_LOG_MESSAGE = "Sample log";
52     private static final String BLUEPRINT = "blueprint";
53     private static final String SVG_REPRESENTATION = "representation";
54
55     @Autowired
56     LoopService loopService;
57
58     @Autowired
59     LoopsRepository loopsRepository;
60
61     @Autowired
62     LoopLogService loopLogService;
63
64     private void saveTestLoopToDb() {
65         Loop testLoop = new Loop(EXAMPLE_LOOP_NAME, SVG_REPRESENTATION);
66         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
67         loopService.saveOrUpdateLoop(testLoop);
68     }
69
70     @Test
71     @Transactional
72     public void testAddLog() {
73         saveTestLoopToDb();
74         Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
75         loopLogService.addLog(SAMPLE_LOG_MESSAGE, "INFO", loop);
76         Set<LoopLog> loopLogs = loop.getLoopLogs();
77         assertThat(loopLogs).hasSize(1);
78         LoopLog loopLog = loopLogs.iterator().next();
79         assertThat(loopLog.getMessage()).isEqualTo(SAMPLE_LOG_MESSAGE);
80     }
81
82     @Test
83     @Transactional
84     public void testLoopLog() {
85         LoopLog log = new LoopLog();
86         Long id = Long.valueOf(100);
87         log.setId(id);
88         log.setLogComponent(CLAMP_COMPONENT);
89         log.setLogType(LogType.INFO);
90         log.setMessage(SAMPLE_LOG_MESSAGE);
91         Loop testLoop = new Loop(EXAMPLE_LOOP_NAME, SVG_REPRESENTATION);
92         log.setLoop(testLoop);
93         assertThat(log.getMessage()).isEqualTo(SAMPLE_LOG_MESSAGE);
94         assertThat(log.getLogType()).isEqualTo(LogType.INFO);
95         assertThat(log.getLogComponent()).isEqualTo(CLAMP_COMPONENT);
96         assertThat(log.getId()).isEqualTo(id);
97         assertThat(log.getLoop()).isEqualTo(testLoop);
98     }
99 }