반응형

 

 

 

package kr.co.xxx;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;  
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
 
import org.junit.Before;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult; 
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 

import org.json.simple.JSONObject;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DbserverApplication.class)
@WebAppConfiguration
public class DbserverApplicationTests {

Logger log = LoggerFactory.getLogger(DbserverApplication.class);
 
    @Autowired
    private WebApplicationContext webApplicationContext;
    private MockMvc mockMvc;

  

public void logPrint(String Name) {
log.info(" ");
log.info("=======================================================");
log.info(" ");

log.info(Name);

log.info(" ");
log.info("=======================================================");
log.info(" ");
}

public void endLogPrint() {
log.info(" ");
log.info("=======================================================");
log.info(" ");
}



    @Before 
    public void setUp() throws Exception {  
     logPrint("Set Up");
    
     // 이곳에서 DbserverController를 MockMvc 객체로 만듭니다.   
        
     mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();      
        
        endLogPrint();
         
        
         
    } 

    /*
    @Test
    public void testIsRunning() throws Exception { 
     logPrint("ISRunning");
    
     mockMvc.perform(get("/ping")
            )
     .andDo(print())                // 출력
     .andExpect(status().isOk());   // 정상 결과 예상
    
    
    
     endLogPrint();
    }
      
    

    
    @SuppressWarnings("unchecked")
@Test
    public void testGetVideoInfo() throws Exception { 
     log.info("GetVideoInfo");
    
     // 전체 JSON Object 담을 정보
     //JSONArray mapArray = new JSONArray();
    
     // 한개의 정보가 들어갈 JSONObject 선언
     JSONObject mapInfo = new JSONObject();
    
     // 정보 입력
     mapInfo.put("xmin", "-111.46727710962297");
     mapInfo.put("ymin", "20.462022433054965" );
     mapInfo.put("xmax", "-110.46727710962297");
     mapInfo.put("ymax", "21.96517113846002"  );
    
     // Array 값 추가
     //mapArray.add(mapInfo);  
        
     mockMvc.perform(post("/getVideoInfo") 
     .header("Accept", "application/json")
     .contentType("application/json")
     .characterEncoding("utf-8") 
     .content(mapInfo.toString()) 
     )
.andDo(print())                // 출력
.andExpect(status().isOk())    // 정상결과 예상 
                ;
         
    
     endLogPrint();
    }         
    
    */

    
    @Test
    public void testIsRunning() throws Exception { 
     logPrint("ISRunning");
    
     MvcResult result = this.mockMvc.perform(get("/ping")
                                )
                 //.andDo(print())                // 출력
        .andExpect(status().isOk())    // 정상 결과 예상
        .andReturn()
        ;   
    
    
     // 결과를 받는 String
     String content = result.getResponse().getContentAsString();
      
     // 예상 결과 
     // 첫문장 비교해서 오류 발생하면 문제가 있는 것임
     assertEquals("I'm Alive!", content ); 
    
     endLogPrint();
    }
      
    
    
    @SuppressWarnings({ "unchecked" })
@Test(timeout = 100000)   // Timeout 시간 설정 (단위 : milliseconds)
    public void testGetVideoInfo() throws Exception {
      
    
     log.info("GetVideoInfo");
    
     // 전체 JSON Object 담을 정보
     //JSONArray mapArray = new JSONArray();
    
     // 한개의 정보가 들어갈 JSONObject 선언
     JSONObject mapInfo = new JSONObject();
    
     // 정보 입력
     mapInfo.put("xmin", "-111.46727710962297");
     mapInfo.put("ymin", "20.462022433054965" );
     mapInfo.put("xmax", "-95.15257984399797" );
     mapInfo.put("ymax", "22.96517113846002"  );
    
     // Array 값 추가
     //mapArray.add(mapInfo);   
        
     MvcResult result = this.mockMvc.perform(post("/getVideoInfo")              
     .header("Accept", "application/json")
     .contentType("application/json")
     .characterEncoding("utf-8") 
     .content(mapInfo.toString()) 
     ) 
//.andDo(print())                // 출력 (결과가가 길면 출력을 생략하는 것이 좋음)
.andExpect(status().isOk())    // 정상결과 예상 
.andReturn()
                ;
    
     
     // 결과를 받는 String
     String content = result.getResponse().getContentAsString();
    
     // ,를 기준으로 첫번째 값만 받아옴
     String [] con_list = content.split(",");
    
     // 정상 반환 여부를 확인하기 위해 첫번째 값 별도로 재저장
     String [] first_con = con_list[0].split(":");
    
     //result.
     log.info("first_con[0]  : " + first_con[0]); 
    
     // 예상 결과 
     // 첫문장 비교해서 오류 발생하면 문제가 있는 것임
     assertEquals("[{\"video_sn\"", first_con[0] ); 
    
     endLogPrint();
    }         
 
    
} // end of Class

반응형

+ Recent posts