Added test cases for snmpmapper in Mapper 53/40153/1
authorBharathS24 <BS00493532@techmahindra.com>
Thu, 29 Mar 2018 12:37:27 +0000 (18:07 +0530)
committerBharathS24 <BS00493532@techmahindra.com>
Thu, 29 Mar 2018 12:38:01 +0000 (18:08 +0530)
Commiting test cases in Mapper

Change-Id: I429d66260151b5a26aa432b729c57bd6de9f0183
Issue-ID: DCAEGEN2-338
Signed-off-by: BharathS24 <BS00493532@techmahindra.com>
snmpmapper/src/test/java/org/onap/dcae/mapper/FileUploadControllerTest.java
snmpmapper/src/test/java/org/onap/dcae/mapper/storage/FileSystemStorageServiceTest.java

index 74b5826..d970724 100644 (file)
@@ -67,7 +67,7 @@ public class FileUploadControllerTest {
     
     
     @Test
-    public void test() throws Exception {
+    public void listUploadedFilesTest() throws Exception {
         
         Model map = new ExtendedModelMap();
         
@@ -79,7 +79,7 @@ public class FileUploadControllerTest {
         try {
             Mockito.when(storageService.loadAll()).thenReturn(list.stream());
         } catch (Exception e) {
-//            eLOGGER.error("Error occurred : " + e.getMessage());
+
         }
        
         try {
@@ -87,7 +87,7 @@ public class FileUploadControllerTest {
             
             assertEquals("uploadForm", listUploadedFiles);
         } catch (IOException e) {
-            // TODO Auto-generated catch block
+            
             e.printStackTrace();
         }
       
index f80ecf7..56fc518 100644 (file)
@@ -21,8 +21,16 @@ package org.onap.dcae.mapper.storage;
 
 import static org.junit.Assert.*;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
 import java.nio.file.Path;
-
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -35,59 +43,102 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.core.io.Resource;
+import org.springframework.mock.web.MockMultipartFile;
 import org.springframework.test.context.junit4.SpringRunner;
 import org.springframework.test.web.servlet.MockMvc;
 import org.springframework.web.multipart.MultipartFile;
 
 
+
 @RunWith(SpringRunner.class)
-@WebMvcTest(value = FileUploadController.class, secure = false)
+@SpringBootTest(classes=SnmpmapperApplication.class)
 public class FileSystemStorageServiceTest {
     
     
   
     StorageProperties sp = new StorageProperties();
-     
-    //FileSystemStorageService service = new FileSystemStorageService(sp);
+     @Autowired
+    FileSystemStorageService fileSystemStorageService ;
 
     
-//    
-//    @Before
-//    public void init() {
-//        MockitoAnnotations.initMocks(this);
-//    }
-//    
     
     @Value("${fileService.rootPath}")
     private String location;
+   
     
     
-    @Autowired
-    private MockMvc mockMvc;
+    byte[] content = null;
     
-    @Mock
-    MultipartFile file;
+   
     
-    @MockBean
-    private FileSystemStorageService fileSystemStorageService;
+    
+   
     String filename;
     
     
     @Test
-    public void testFileSystemStorageService() {
-        
-        fileSystemStorageService.init();
-        fileSystemStorageService.load(filename);
+    public void testFileSystemStorageServiceLoadMethod() throws IOException {
+       fileSystemStorageService.init();
+       saveFile();
         
+        Path filePath = fileSystemStorageService.load("TestFile.txt");
+      
+        assertEquals(Paths.get(location), filePath.getParent());
+       
        
     }
     
+   
+    
     @Test
-    public void test() {
-         Path rootLocation = null;;
-        sp.setLocation(location);
-        FileSystemStorageService fss=new FileSystemStorageService(sp);
+    public void storeTest() throws IOException
+    {
+       fileSystemStorageService.init();
+       saveFile();
+        
+        assertEquals(true, Files.exists(Paths.get(location, "TestFile.txt")));
+        
+       
+       
+       
+       
+    }
+
+       private void saveFile() throws IOException {
+               Files.deleteIfExists(Paths.get(location, "TestFile.txt"));
+       content = "randomString".getBytes();
+       MultipartFile file=new MockMultipartFile("TestFile.txt","TestFile.txt", "text/plain", content);
+       
+       
+        fileSystemStorageService.store(file);
+       }
+       @Test
+    public void testFileSystemStorageServiceLoadAllMethod() throws IOException {
+       fileSystemStorageService.init();
+       saveFile();
+        
+        Stream<Path> filePath = fileSystemStorageService.loadAll();
+      List<Path> listOfPaths =filePath.collect(Collectors.toList()); 
+        
+       assertEquals(true, listOfPaths.contains(Paths.get("TestFile.txt")));
+       
+       
+    }
+       
+        @Test
+           public void testFileSystemStorageServiceLoadResourceMethod() throws IOException {
+               fileSystemStorageService.init();
+               saveFile();
+               
+             
+               
+               Resource fileResource = fileSystemStorageService.loadAsResource("TestFile.txt");
 
-}
+             
+               assertEquals(Paths.get(location,"TestFile.txt").getFileName().toString(), fileResource.getFilename());
+              
+              
+           }
     
 }
\ No newline at end of file