package cokr.xit.ens.modules.junittest.sample;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasValue;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;

import cokr.xit.ens.modules.common.domain.SendMast;

/**
 * 
 * <ul>
 * <li>업무 그룹명: Junit 기본메소드 테스트</li>
 * <li>설 명: </li>
 * <li>작성일: 2021. 10. 25. 오후 5:49:07
 * </ul>
 *
 * @author 박민규
 *
 */
class JunitBasicMethodTestSample {


	@Test
	void testEquals() {
		Map<String, String> param1 = new HashMap<String, String>();
		Map<String, String> param2 = param1;
		assertEquals(param1, param2);	
		

		
		
		int hour = 24*30;
		Long extTime = 60L * 60L * hour;
		System.out.println("만료시간(단위: sec) => "+extTime);
		
		
		Date frstDate = new Date (1970, 1, 1, 0, 0, 0);	//2030-11-28 11:22:33 까지
		Date curDate = new Date (2021, 4, 2, 15, 0, 0);	//2030-11-28 11:22:33 까지
		Long extTime2 = curDate.getTime()-frstDate.getTime();
		Long extTime3 = curDate.UTC(2021, 4, 2, 15, 0, 0)-frstDate.UTC(1970, 1, 1, 0, 0, 0);
		System.out.println("만료시간(단위: sec) => "+(extTime2/1000));	//2021-04-01 0시0분0초 는 "1617202800"이 나와야 함.
		System.out.println("만료시간(단위: sec) => "+(extTime3/1000));
		Date date = new Date(1637283712000L);
//		date.setTime(1637283712);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		System.out.println("변환 =>"+ format.format(date));
//		1617202800
//		1624340958
	}
	@Test
	void testNotEquals() {
		assertNotEquals("haha", "abc");	
		System.out.println(String.format("%s", HttpStatus.INTERNAL_SERVER_ERROR.toString()));
		System.out.println(String.format("%s", HttpStatus.INTERNAL_SERVER_ERROR.name()));
		System.out.println(String.format("%s", HttpStatus.INTERNAL_SERVER_ERROR.value()));
	}
	@Test
	void testNull() {
		assertNull(null);
	}
	@Test
	void testNotNull() {
		String str = "abcd";
		assertNotNull(str);
	}
	@Test
	void testTrue() {
		Map<String, String> map = null;
		assertTrue(map == null);
	}
	@Test
	void testFalse() {
		Map<String, String> map = null;
		assertFalse(map != null);
	}
	@Test
	void testArrayEquals() {
		String names[] = {"test","haha"};
		String names2[] = {"test","haha"};
		assertArrayEquals(names, names2);
	}
	@Test
	void testTimeout() {
		
		assertTimeout(Duration.ofMillis(1000), ()->Thread.sleep(500), "제한시간 내 수행실패");
	}
	@Test
	void testAssertThat() {
		String names[] = {"test","haha"};
		String names2[] = {"test","haha"};
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("param1", "value1");
		
		//Core(anything/describedAs/is/...)
		assertThat(2 + 1, is(3));
		assertThat(names, is(names2));	//names is names2
		
		
		//Logical(allOf/anyOf/not)
		assertThat("Sample string.", is(not(startsWith("Test"))));	//"Sample string." is not startsWith "Test"
		assertThat("myValue", allOf(startsWith("my"), containsString("Val")));	//allOf 모두 성공해야 통과. "myValue" 문자열에 "my"로 시작하고 "Val"이 포함되어 있어야 성공
		assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")));	//anyOf 하나 이상 성공해야 통과. "myValue" 문자열에 "foo"로 시작하거나 "Val"이 포함되어 있어야 성공
		
		
		//Object(equalTo/hasToString/instanceOf/isCompatibleType/notNullValue/nullValue/sameInstance/...)
		assertThat(2 + 1, equalTo(3));
		assertThat(names, equalTo(names2));
		assertThat("aaa", instanceOf(String.class));
		assertThat("aaa", sameInstance("hahaha"));
		
		
		//Beans(hasProperty)
		SendMast sendMast = SendMast.builder().sendMastId(1234L).build();
		assertThat(sendMast, hasProperty("sendMastId"));
		assertThat(sendMast, hasProperty("sendMastId", is(1234L)));
		
		
		//Collections(array/hasEntry/hashKey/hasValue/hasItem/hasItems/hasItemInArray/...)
		assertThat(map, hasEntry("param1", "value1"));
		assertThat(map, hasKey("param1"));
		assertThat(map, hasValue("value1"));
		
		
		//Number(closeTo/greaterThan/greaterThanOrEqualTo/lessThan/lessThanOrEqualTo/...)
		assertThat(10, greaterThan(5));
		assertThat(10, greaterThanOrEqualTo(10));
		assertThat(10, lessThan(15));
		assertThat(10, lessThanOrEqualTo(10));
		
		
		//Text(equalToIgnoringCase/containsString/endsWith/startsWith/...)
		assertThat("ABcdEF", equalToIgnoringCase("abcdef"));	
		assertThat("ABcdEF", containsString("cdE"));	//"cdE"를 포함하면..
		assertThat("ABcdEF", endsWith("dEF"));	//"dEF"로 끝나면..
		assertThat("ABcdEF", startsWith("AB"));	//"AB"로 시작하면..
		
		
	}


}
