Improve unit test coverage of loop/log 81/97181/1
authorVidyashree Rama <vidyashree.rama@huawei.com>
Wed, 16 Oct 2019 10:14:19 +0000 (15:44 +0530)
committerVidyashree Rama <vidyashree.rama@huawei.com>
Wed, 16 Oct 2019 10:15:34 +0000 (15:45 +0530)
Improve unit test coverage of loop/log

Change-Id: Id4deee79a3637396d210f14051fd345c9e7039e3
Issue-ID: CLAMP-510
Signed-off-by: Vidyashree Rama <vidyashree.rama@huawei.com>
src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java [new file with mode: 0644]

diff --git a/src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java b/src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java
new file mode 100644 (file)
index 0000000..57b2cef
--- /dev/null
@@ -0,0 +1,99 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP CLAMP
+ * ================================================================================
+ * Copyright (C) 2019 Huawei Technologies Co., Ltd.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END============================================
+ * ===================================================================
+ *
+ */
+
+package org.onap.clamp.loop;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import com.google.gson.JsonObject;
+
+import java.util.Set;
+import javax.transaction.Transactional;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.onap.clamp.clds.Application;
+import org.onap.clamp.clds.util.JsonUtils;
+import org.onap.clamp.loop.log.LogType;
+import org.onap.clamp.loop.log.LoopLog;
+import org.onap.clamp.loop.log.LoopLogService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = Application.class)
+public class LoopLogServiceTestItCase {
+
+    private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest";
+    private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}";
+    private static final String CLAMP_COMPONENT = "CLAMP";
+    private static final String SAMPLE_LOG_MESSAGE = "Sample log";
+    private static final String BLUEPRINT = "blueprint";
+    private static final String SVG_REPRESENTATION = "representation";
+
+    @Autowired
+    LoopService loopService;
+
+    @Autowired
+    LoopsRepository loopsRepository;
+
+    @Autowired
+    LoopLogService loopLogService;
+
+    private void saveTestLoopToDb() {
+        Loop testLoop = new Loop(EXAMPLE_LOOP_NAME, BLUEPRINT, SVG_REPRESENTATION);
+        testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
+        loopService.saveOrUpdateLoop(testLoop);
+    }
+
+    @Test
+    @Transactional
+    public void testAddLog() {
+        saveTestLoopToDb();
+        Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
+        loopLogService.addLog(SAMPLE_LOG_MESSAGE, "INFO", loop);
+        Set<LoopLog> loopLogs = loop.getLoopLogs();
+        assertThat(loopLogs).hasSize(1);
+        LoopLog loopLog = loopLogs.iterator().next();
+        assertThat(loopLog.getMessage()).isEqualTo(SAMPLE_LOG_MESSAGE);
+        loopsRepository.deleteAll();
+    }
+
+    @Test
+    public void testLoopLog() {
+        LoopLog log = new LoopLog();
+        Long id = Long.valueOf(100);
+        log.setId(id);
+        log.setLogComponent(CLAMP_COMPONENT);
+        log.setLogType(LogType.INFO);
+        log.setMessage(SAMPLE_LOG_MESSAGE);
+        Loop testLoop = new Loop(EXAMPLE_LOOP_NAME, BLUEPRINT, SVG_REPRESENTATION);
+        log.setLoop(testLoop);
+        assertThat(log.getMessage()).isEqualTo(SAMPLE_LOG_MESSAGE);
+        assertThat(log.getLogType()).isEqualTo(LogType.INFO);
+        assertThat(log.getLogComponent()).isEqualTo(CLAMP_COMPONENT);
+        assertThat(log.getId()).isEqualTo(id);
+        assertThat(log.getLoop()).isEqualTo(testLoop);
+    }
+}
\ No newline at end of file