package com.dbs.consentServer;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Optional;

import com.dbs.consentServer.service.ConsentService;

import org.junit.jupiter.api.Test;
// import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;

// import static org.junit.Assert.assertThat;

@SpringBootTest
@AutoConfigureMockMvc
class ConsentApiApplicationTests {
  @Autowired
  private ConsentService consentService;

  /**
   * getOcrTag 요청 테스트
   * 해당 API 를 호출 하여 OCRTAG 가 유효한지 검사한다
   * @param mvc
   * @throws Exception
   */
  @Test
  void getOcrTagTest(@Autowired MockMvc mvc) throws Exception {
    mvc.perform(get("/ConsentSvc/getOcrTag"))
			.andExpect(status().isOk())
      .andExpect(content().contentType("text/plain;charset=UTF-8"))
      .andExpect(new ResultMatcher(){
        @Override
        public void match(MvcResult result) throws Exception {
          Optional.ofNullable(result.getResponse().getContentAsString())
            .filter((body) -> body.length() == 13)
            .orElseThrow(() -> new Exception("OCRTAG length is not same with pre-defined(length: 13)"));          
        }
      }
    );
	}
}
