@Test
public void createTest() throws Exception {

	// perform check
	MvcResult andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL + "/search")
					.content(mapper.writeValueAsString(DemandResponseEventFilter.builder()
							.addVenId(DemandResponseControllerTest.VEN1).build()))
					.header("Content-Type", "application/json").with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	List<DemandResponseEventReadDto> readValue = convertMvcResultToDemandResponseDtoList(andReturn);
	assertNotNull(readValue);
	assertEquals(2, readValue.size());
	for (DemandResponseEventReadDto dto : readValue) {
		assertEquals(DemandResponseControllerTest.MODIFICATION, dto.getDescriptor().getModificationNumber());
		assertNotNull(dto.getCreatedTimestamp());
		assertEquals(DemandResponseControllerTest.DURATION, dto.getActivePeriod().getDuration());
		assertEquals(DemandResponseControllerTest.MARKETCONTEXT_NAME, dto.getDescriptor().getMarketContext());
		assertEquals(DemandResponseControllerTest.START, dto.getActivePeriod().getStart());
		assertEquals(DemandResponseEventSimpleValueEnum.SIMPLE_SIGNAL_PAYLOAD_HIGH.getValue(),
				dto.getSignals().get(0).getCurrentValue());
	}
	// create and don't publish
	DemandResponseEventCreateDto toCreate = createValidEvent();
	byte[] content = mapper.writeValueAsBytes(toCreate);
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(content)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.CREATED_201)).andReturn();
	DemandResponseEventReadDto res = convertMvcResultToDemandResponseDto(andReturn);
	assertNotNull(res);
	assertNotNull(res.getId());
	Long toDeleteId = res.getId();
	assertEquals(DemandResponseControllerTest.MODIFICATION, res.getDescriptor().getModificationNumber());
	assertNotNull(res.getCreatedTimestamp());
	assertNotNull(res.getLastUpdateTimestamp());
	assertEquals(DemandResponseControllerTest.DURATION, res.getActivePeriod().getDuration());
	assertEquals(DemandResponseControllerTest.MARKETCONTEXT_NAME, res.getDescriptor().getMarketContext());
	assertEquals(DemandResponseControllerTest.START, res.getActivePeriod().getStart());
	assertEquals(DemandResponseEventSimpleValueEnum.SIMPLE_SIGNAL_PAYLOAD_HIGH.getValue(),
			res.getSignals().get(0).getCurrentValue());
	assertTrue(DemandResponseEventStateEnum.ACTIVE.equals(res.getDescriptor().getState()));
	// perform check
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL + "/search")
					.content(mapper.writeValueAsString(DemandResponseEventFilter.builder()
							.addVenId(DemandResponseControllerTest.VEN1).build()))
					.header("Content-Type", "application/json").with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	readValue = convertMvcResultToDemandResponseDtoList(andReturn);
	assertNotNull(readValue);
	assertEquals(3, readValue.size());
	for (DemandResponseEventReadDto dto : readValue) {
		assertEquals(DemandResponseControllerTest.MODIFICATION, dto.getDescriptor().getModificationNumber());
		assertNotNull(dto.getCreatedTimestamp());
		assertEquals(DemandResponseControllerTest.DURATION, dto.getActivePeriod().getDuration());
		assertEquals(DemandResponseControllerTest.MARKETCONTEXT_NAME, dto.getDescriptor().getMarketContext());
		assertEquals(DemandResponseControllerTest.START, dto.getActivePeriod().getStart());
		assertEquals(DemandResponseEventSimpleValueEnum.SIMPLE_SIGNAL_PAYLOAD_HIGH.getValue(),
				dto.getSignals().get(0).getCurrentValue());
	}
	// perform invalid create: null market context
	toCreate = createValidEvent();
	toCreate.getDescriptor().setMarketContext(null);
	String contentStr = mapper.writeValueAsString(toCreate);
	this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(contentStr)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// perform invalid create: null profile
	toCreate = createValidEvent();
	toCreate.getDescriptor().setOadrProfile(null);
	contentStr = mapper.writeValueAsString(toCreate);
	this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(contentStr)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// perform invalid create: null response required
	toCreate = createValidEvent();
	toCreate.getDescriptor().setResponseRequired(null);
	contentStr = mapper.writeValueAsString(toCreate);
	this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(contentStr)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// perform invalid create: unknown market context
	toCreate = createValidEvent();
	toCreate.getDescriptor().setMarketContext(UNKNOWN_MARKETCONTEXT_NAME);
	contentStr = mapper.writeValueAsString(toCreate);
	this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(contentStr)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// perform invalid create: modification number present
	toCreate = createValidEvent();
	toCreate.getDescriptor().setMarketContext(UNKNOWN_MARKETCONTEXT_NAME);
	toCreate.getDescriptor().setModificationNumber(0L);
	contentStr = mapper.writeValueAsString(toCreate);
	this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(contentStr)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// perform invalid create: invalid priority
	toCreate = createValidEvent();
	toCreate.getDescriptor().setPriority(-1L);
	contentStr = mapper.writeValueAsString(toCreate);
	this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(contentStr)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// perform invalid create: no signal configured
	toCreate = createValidEvent();
	toCreate.getSignals().clear();
	contentStr = mapper.writeValueAsString(toCreate);
	this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(contentStr)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// perform invalid create: start MUST be set
	toCreate = createValidEvent();
	toCreate.getActivePeriod().setStart(null);
	contentStr = mapper.writeValueAsString(toCreate);
	this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(contentStr)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// perform invalid create: duration MUST be set
	toCreate = createValidEvent();
	toCreate.getActivePeriod().setDuration(null);
	contentStr = mapper.writeValueAsString(toCreate);
	this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(contentStr)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// perform invalid create: duration MUST be a valid xml duration
	toCreate = createValidEvent();
	toCreate.getActivePeriod().setDuration("1h");
	contentStr = mapper.writeValueAsString(toCreate);
	this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(contentStr)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// clean
	this.mockMvc.perform(MockMvcRequestBuilders.delete(DEMAND_RESPONSE_EVENT_URL + toDeleteId).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200));
	// create and publish
	// expect modificationNumber = 0 because event published
	toCreate = createValidEvent();
	toCreate.setPublished(true);
	content = mapper.writeValueAsBytes(toCreate);
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL).content(content)
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.CREATED_201)).andReturn();
	res = convertMvcResultToDemandResponseDto(andReturn);
	assertNotNull(res);
	assertNotNull(res.getId());
	toDeleteId = res.getId();
	assertNotNull(res.getCreatedTimestamp());
	assertNotNull(res.getLastUpdateTimestamp());
	assertEquals(DemandResponseControllerTest.DURATION, res.getActivePeriod().getDuration());
	assertEquals(DemandResponseControllerTest.MARKETCONTEXT_NAME, res.getDescriptor().getMarketContext());
	assertEquals(DemandResponseControllerTest.START, res.getActivePeriod().getStart());
	assertEquals(DemandResponseEventSimpleValueEnum.SIMPLE_SIGNAL_PAYLOAD_HIGH.getValue(),
			res.getSignals().get(0).getCurrentValue());
	assertTrue(DemandResponseEventStateEnum.ACTIVE.equals(res.getDescriptor().getState()));
	assertTrue(res.getDescriptor().getModificationNumber().equals(0L));
	// clean
	this.mockMvc.perform(MockMvcRequestBuilders.delete(DEMAND_RESPONSE_EVENT_URL + toDeleteId).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200));
}