@Test
public void updateTest() throws Exception {

	List<DemandResponseEventFilter> filters = DemandResponseEventFilter.builder().addVenId(VEN1).build();
	MvcResult andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL + "search")
					.content(mapper.writeValueAsString(filters))
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	List<DemandResponseEventReadDto> list = convertMvcResultToDemandResponseDtoList(andReturn);
	assertEquals(2, list.size());
	filters = DemandResponseEventFilter.builder().addVenId(VEN2).build();
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL + "search")
					.content(mapper.writeValueAsString(filters))
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	list = convertMvcResultToDemandResponseDtoList(andReturn);
	assertEquals(2, list.size());
	// check event exists
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.get(DEMAND_RESPONSE_EVENT_URL + event1.getId()).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	DemandResponseEventReadDto dto = convertMvcResultToDemandResponseDto(andReturn);
	Long modificationNumber = dto.getDescriptor().getModificationNumber();
	assertNotNull(dto);
	assertNotNull(dto.getSignals());
	assertEquals(1, dto.getSignals().size());
	assertEquals(dto.getSignals().get(0).getCurrentValue(),
			DemandResponseEventSimpleValueEnum.SIMPLE_SIGNAL_PAYLOAD_HIGH.getValue());
	assertNotNull(dto.getSignals().get(0).getIntervals());
	assertEquals(1, dto.getSignals().get(0).getIntervals().size());
	assertEquals(INTERVAL_VALUE, dto.getSignals().get(0).getIntervals().get(0).getValue());
	assertEquals(INTERVAL_DURATION, dto.getSignals().get(0).getIntervals().get(0).getDuration());
	DemandResponseEventSignalDto signalModerate = new DemandResponseEventSignalDto();
	signalModerate.setCurrentValue(DemandResponseEventSimpleValueEnum.SIMPLE_SIGNAL_PAYLOAD_MODERATE.getValue());
	signalModerate.setSignalName(SIMPLE_SIGNAL_NAME);
	signalModerate.setSignalType(LEVEL_SIGNAL_TYPE);
	List<DemandResponseEventTargetDto> targets = new ArrayList<>();
	targets.add(new DemandResponseEventTargetDto(VEN_PARAM, VEN2));
	// invalid update: signals not set
	DemandResponseEventUpdateDto updateDto = new DemandResponseEventUpdateDto();
	updateDto.setPublished(false);
	updateDto.getSignals().clear();
	updateDto.setTargets(targets);
	String contentStr = mapper.writeValueAsString(updateDto);
	this.mockMvc.perform(MockMvcRequestBuilders.put(DEMAND_RESPONSE_EVENT_URL + event1.getId())
			.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).content(contentStr).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// invalid update: signals not set
	updateDto = new DemandResponseEventUpdateDto();
	updateDto.setPublished(false);
	updateDto.getSignals().add(signalModerate);
	updateDto.getTargets().clear();
	contentStr = mapper.writeValueAsString(updateDto);
	this.mockMvc.perform(MockMvcRequestBuilders.put(DEMAND_RESPONSE_EVENT_URL + event1.getId())
			.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).content(contentStr).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// invalid update: published null
	updateDto = new DemandResponseEventUpdateDto();
	updateDto.setPublished(null);
	updateDto.getSignals().add(signalModerate);
	updateDto.setTargets(targets);
	contentStr = mapper.writeValueAsString(updateDto);
	this.mockMvc.perform(MockMvcRequestBuilders.put(DEMAND_RESPONSE_EVENT_URL + event1.getId())
			.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).content(contentStr).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// update but don't publish
	updateDto = new DemandResponseEventUpdateDto();
	updateDto.setPublished(false);
	updateDto.getSignals().add(signalModerate);
	updateDto.setTargets(targets);
	contentStr = mapper.writeValueAsString(updateDto);
	this.mockMvc.perform(MockMvcRequestBuilders.put(DEMAND_RESPONSE_EVENT_URL + event1.getId())
			.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).content(contentStr).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200));
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.get(DEMAND_RESPONSE_EVENT_URL + event1.getId()).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	dto = convertMvcResultToDemandResponseDto(andReturn);
	assertNotNull(dto);
	assertNotNull(dto.getSignals());
	assertEquals(1, dto.getSignals().size());
	assertEquals(dto.getSignals().get(0).getCurrentValue(),
			DemandResponseEventSimpleValueEnum.SIMPLE_SIGNAL_PAYLOAD_MODERATE.getValue());
	assertEquals(modificationNumber, dto.getDescriptor().getModificationNumber());
	assertTrue(dto.getSignals().get(0).getIntervals().isEmpty());
	assertEquals(1, dto.getTargets().size());
	assertEquals(VEN2, dto.getTargets().get(0).getTargetId());
	assertEquals(VEN_PARAM, dto.getTargets().get(0).getTargetType());
	filters = DemandResponseEventFilter.builder().addVenId(VEN1).build();
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL + "search")
					.content(mapper.writeValueAsString(filters))
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	list = convertMvcResultToDemandResponseDtoList(andReturn);
	assertEquals(1, list.size());
	filters = DemandResponseEventFilter.builder().addVenId(VEN2).build();
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL + "search")
					.content(mapper.writeValueAsString(filters))
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	list = convertMvcResultToDemandResponseDtoList(andReturn);
	assertEquals(2, list.size());
	// update and publish
	DemandResponseEventSignalDto signalNormal = new DemandResponseEventSignalDto();
	signalNormal.setCurrentValue(DemandResponseEventSimpleValueEnum.SIMPLE_SIGNAL_PAYLOAD_NORMAL.getValue());
	signalNormal.setSignalName(SIMPLE_SIGNAL_NAME);
	signalNormal.setSignalType(LEVEL_SIGNAL_TYPE);
	updateDto = new DemandResponseEventUpdateDto();
	updateDto.setPublished(true);
	updateDto.getSignals().add(signalNormal);
	targets = new ArrayList<>();
	targets.add(new DemandResponseEventTargetDto(GROUP_PARAM, GROUP1));
	updateDto.setTargets(targets);
	contentStr = mapper.writeValueAsString(updateDto);
	this.mockMvc.perform(MockMvcRequestBuilders.put(DEMAND_RESPONSE_EVENT_URL + event1.getId())
			.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).content(contentStr).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200));
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.get(DEMAND_RESPONSE_EVENT_URL + event1.getId()).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	dto = convertMvcResultToDemandResponseDto(andReturn);
	assertNotNull(dto);
	assertNotNull(dto.getSignals());
	assertEquals(1, dto.getSignals().size());
	assertEquals(dto.getSignals().get(0).getCurrentValue(),
			DemandResponseEventSimpleValueEnum.SIMPLE_SIGNAL_PAYLOAD_NORMAL.getValue());
	modificationNumber++;
	assertEquals(modificationNumber, dto.getDescriptor().getModificationNumber());
	filters = DemandResponseEventFilter.builder().addVenId(VEN1).build();
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL + "search")
					.content(mapper.writeValueAsString(filters))
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	list = convertMvcResultToDemandResponseDtoList(andReturn);
	assertEquals(2, list.size());
	filters = DemandResponseEventFilter.builder().addVenId(VEN2).build();
	andReturn = this.mockMvc
			.perform(MockMvcRequestBuilders.post(DEMAND_RESPONSE_EVENT_URL + "search")
					.content(mapper.writeValueAsString(filters))
					.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK_200)).andReturn();
	list = convertMvcResultToDemandResponseDtoList(andReturn);
	assertEquals(1, list.size());
	// update id not Long
	this.mockMvc.perform(MockMvcRequestBuilders.put(DEMAND_RESPONSE_EVENT_URL + UNKNOWN_MARKETCONTEXT_NAME)
			.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).content(contentStr).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST_400));
	// update unknown
	this.mockMvc.perform(MockMvcRequestBuilders.put(DEMAND_RESPONSE_EVENT_URL + "999")
			.header(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_HEADER_VALUE).content(contentStr).with(adminSession))
			.andExpect(MockMvcResultMatchers.status().is(HttpStatus.NOT_FOUND_404));
}