/*
 * Copyright 2017-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example;

import java.util.List;
import java.util.Map;

import com.google.cloud.spring.core.util.MapBuilder;
import org.awaitility.Duration;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assume.assumeThat;

/**
 * Tests the Pub/Sub Json payload app.
 *
 * @author Daniel Zou
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = { PubSubJsonPayloadApplication.class })
public class PubSubJsonPayloadSampleApplicationIntegrationTests {

	@Autowired
	private TestRestTemplate testRestTemplate;

	@BeforeClass
	public static void prepare() {
		assumeThat(
				"PUB/SUB-sample integration tests are disabled. Please use '-Dit.pubsub=true' "
						+ "to enable them. ",
				System.getProperty("it.pubsub"), is("true"));
	}

	@Test
	public void testReceivesJsonPayload() {
		Map<String, String> params = new MapBuilder<String, String>()
				.put("name", "Bob")
				.put("age", "25")
				.build();

		this.testRestTemplate.postForObject(
				"/createPerson?name={name}&age={age}", null, String.class, params);

		await().atMost(Duration.TEN_SECONDS).untilAsserted(() -> {
			ResponseEntity<List<Person>> response = this.testRestTemplate.exchange(
					"/listPersons",
					HttpMethod.GET,
					null,
					new ParameterizedTypeReference<List<Person>>() {
					});

			assertThat(response.getBody()).containsExactly(new Person("Bob", 25));
		});
	}
}

@@ -682,16 +682,41 @@
 til.Map;
+%0Aimport java.util.Random;
 %0A%0Aimport
@@ -2164,16 +2164,82 @@
 oad() %7B%0A
+%09%09Random random = new Random();%0A%09%09int age = random.nextInt(200);%0A%0A
 %09%09Map%3CSt
@@ -2338,12 +2338,27 @@
 e%22, 
-%2225%22
+String.valueOf(age)
 )%0A%09%09
@@ -2781,15 +2781,16 @@
 ains
-Exactly
+OnlyOnce
 (new
@@ -2808,10 +2808,11 @@
 b%22, 
-25
+age
 ));%0A

Make Pub/Sub JSON payload test more robust (#124)