Fixed Sonar Bug and Code Smell Blockers
[cli.git] / framework / src / test / java / org / onap / cli / fw / store / OnapCommandProfileStoreTest.java
1 /*
2  * Copyright 2019 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.cli.fw.store;
17
18 import org.junit.AfterClass;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.onap.cli.fw.cmd.execution.OnapCommandExceutionListCommandTest;
22 import org.onap.cli.fw.error.OnapCommandException;
23 import org.onap.cli.fw.error.OnapCommandPersistProfileFailed;
24 import org.onap.cli.fw.input.cache.OnapCommandParamEntity;
25
26 import java.io.File;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import static org.junit.Assert.*;
31 import java.io.IOException;
32 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
33
34 public class OnapCommandProfileStoreTest {
35     OnapCommandProfileStore onapCommandProfileStore;
36
37     @Before
38     public void setUp() throws Exception {
39         onapCommandProfileStore = OnapCommandProfileStore.getInstance();
40     }
41
42     @Test
43     public void includeProfileTest() throws OnapCommandException {
44         onapCommandProfileStore.includeProfile("profiles");
45         assertTrue(new File(System.getProperty("user.dir") + File.separator + "data/profiles").exists());
46     }
47
48     @Test
49     public void persistProfileAndgetProfilesTest() throws OnapCommandPersistProfileFailed {
50         OnapCommandParamEntity onapCommandParamEntity = new OnapCommandParamEntity();
51         onapCommandParamEntity.setName("schema-list");
52         onapCommandParamEntity.setProduct("open-cli");
53         onapCommandParamEntity.setValue("value");
54         List<OnapCommandParamEntity> paramEntityList = new ArrayList<>();
55         paramEntityList.add(onapCommandParamEntity);
56         onapCommandProfileStore.persistProfile(paramEntityList, "abc");
57         assertTrue(new File(System.getProperty("user.dir") + File.separator + "data/profiles/abc-profile.json").exists());
58         assertNotNull(onapCommandProfileStore.getProfiles());
59     }
60
61     @Test
62     public void removeProfileTest() {
63         onapCommandProfileStore.removeProfile("abc");
64         assertFalse(new File(System.getProperty("user.dir") + File.separator + "data/profiles/abc-profile.json").exists());
65     }
66     @Test
67     public void removeProfileDeleteTest() throws IOException {
68         new File(System.getProperty("user.dir") + File.separator + "data/profiles/abc-profile.json").createNewFile();
69         onapCommandProfileStore.removeProfile("abc");
70         assertFalse(new File(System.getProperty("user.dir") + File.separator + "data/profiles/abc-profile.json").exists());
71     }
72
73     @Test
74     public void addTest() {
75         onapCommandProfileStore.add("abc", "abc", "abc");
76         assertNotNull(onapCommandProfileStore. getParams("abc"));
77     }
78
79     @Test
80     public void getParamsTest() {
81         assertNotNull(onapCommandProfileStore.getParams("abc"));
82     }
83
84     @Test
85     public void removeTest() {
86         assertDoesNotThrow(() -> onapCommandProfileStore.add("abc", "abc", "abc"));
87         assertDoesNotThrow(() -> onapCommandProfileStore.remove("abc","abc"));
88     }
89
90     @AfterClass
91     public static void tearDown() throws Exception {
92         String dirPathForData = System.getProperty("user.dir") + File.separator + "data";
93         File dataDir = new File(dirPathForData);
94         assertTrue(OnapCommandExceutionListCommandTest.deleteDirectory(dataDir));
95     }
96 }