/**
 * Licensed to the Rhiot under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The licenses this file to You 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 io.rhiot.cloudplatform.adapter.rest;

import com.google.common.collect.ImmutableMap;
import com.google.common.truth.Truth;
import io.rhiot.cloudplatform.encoding.spi.PayloadEncoding;
import io.rhiot.cloudplatform.service.binding.ServiceBinding;
import io.rhiot.cloudplatform.runtime.spring.test.CloudPlatformTest;
import org.junit.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Set;

@Configuration
public class RestProtocolAdapterTest extends CloudPlatformTest {

    RestTemplate rest = new RestTemplate();

    @Test
    public void shouldInvokeGetOperation() throws IOException {
        Map response = json.readValue(new URL("http://localhost:8080/test/count/1"), Map.class);
        Truth.assertThat(response.get("payload")).isEqualTo(1);
    }

    @Test
    public void shouldInvokePostOperation() {
        byte[] request = payloadEncoding.encode(ImmutableMap.of("foo", "bar"));
        Object payload = rest.postForObject("http://localhost:8080/test/sizeOf", request, Map.class).get("payload");
        Truth.assertThat(payload).isEqualTo(1);
    }

    @Test
    public void shouldPassUriAndBody() {
        byte[] request = payloadEncoding.encode(ImmutableMap.of("foo", "bar"));
        Object payload = rest.postForObject("http://localhost:8080/test/numberPlusSizeOf/1", request, Map.class).get("payload");
        Truth.assertThat(payload).isEqualTo(2);
    }

    @Test
    public void shouldHandleOptions() {
        Set<HttpMethod> options = rest.optionsForAllow("http://localhost:8080/test/count/1");
        Truth.assertThat(options).isEmpty();
    }

    // Beans fixtures

    public static interface TestService {

        int count(int number);

        int sizeOf(Map map);

        int numberPlusSizeOf(int number, Map map);

    }

    @Component("test")
    public static class TestInterfaceImpl implements TestService {

        @Override
        public int count(int number) {
            return number;
        }

        @Override
        public int sizeOf(Map map) {
            return map.size();
        }

        @Override
        public int numberPlusSizeOf(int number, Map map) {
            return number + map.size();
        }

    }

    @Bean
    ServiceBinding testServiceBinding(PayloadEncoding payloadEncoding) {
        return new ServiceBinding(payloadEncoding, "test");
    }

}
@@ -1320,16 +1320,61 @@
 ponent;%0A
+import org.springframework.util.SocketUtils;%0A
 import o
@@ -1512,16 +1512,134 @@
 l.Set;%0A%0A
+import static java.lang.System.setProperty;%0Aimport static org.springframework.util.SocketUtils.findAvailableTcpPort;%0A%0A
 @Configu
@@ -1752,24 +1752,274 @@
 emplate();%0A%0A
+    static int restPort = findAvailableTcpPort();%0A%0A    String baseURL = %22http://localhost:%22 + restPort + %22/test/%22;%0A%0A    @Override%0A    protected void beforeCloudPlatformStarted() %7B%0A        setProperty(%22rest.port%22, restPort + %22%22);%0A    %7D%0A%0A    // Tests%0A%0A
     @Test%0A  
@@ -2130,36 +2130,19 @@
 URL(
-%22http://localhost:8080/test/
+baseURL + %22
 coun
@@ -2415,36 +2415,19 @@
 ect(
-%22http://localhost:8080/test/
+baseURL + %22
 size
@@ -2701,36 +2701,19 @@
 ect(
-%22http://localhost:8080/test/
+baseURL + %22
 numb
@@ -2929,36 +2929,19 @@
 low(
-%22http://localhost:8080/test/
+baseURL + %22
 coun
@@ -3033,23 +3033,16 @@
  public 
-static 
 interfac

Test REST port should be random.