// Copyright 2019 Google LLC
//
// 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.google.sps.servlets;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import org.junit.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

/*
 * Tests the doGet() function in KeywordCampaignIdServlet.java.
 */
@RunWith(JUnit4.class)
public final class KeywordCampaignIdServletTest {
    
    @Mock
    HttpServletRequest request;
 
    @Mock
    HttpServletResponse response;
    
    private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        helper.setUp();
    }

    @After
    public void tearDown() {
        helper.tearDown();
    }

    @Test
    public void keywordCampaignIdServletDoGet() throws IOException, ServletException {
        when(request.getParameter("keywordCampaignId")).thenReturn("1");

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        when(response.getWriter()).thenReturn(pw);

        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        Entity keywordCampaignEntity = new Entity("keywordCampaign");
        keywordCampaignEntity.setProperty("keywordCampaignId", 1);
        keywordCampaignEntity.setProperty("userId", 2);
        keywordCampaignEntity.setProperty("name", "entity 1");
        keywordCampaignEntity.setProperty("fromDate", "1/1/1");
        keywordCampaignEntity.setProperty("toDate", "2/2/2");
        keywordCampaignEntity.setProperty("dailyBudget", 123.2);
        keywordCampaignEntity.setProperty("location", "CA");
        keywordCampaignEntity.setProperty("domain", "google.com");
        keywordCampaignEntity.setProperty("target", "google.com");
        keywordCampaignEntity.setProperty("impressions", 432);
        keywordCampaignEntity.setProperty("clicks", 123);
        keywordCampaignEntity.setProperty("cost", 42.51);
        ArrayList<Integer> DSACampaignIds = new ArrayList<Integer>();
        DSACampaignIds.add(4);
        DSACampaignIds.add(2);
        DSACampaignIds.add(5);
        keywordCampaignEntity.setProperty("DSACampaignIds", DSACampaignIds);
        ds.put(keywordCampaignEntity);

        KeywordCampaignIdServlet servlet = new KeywordCampaignIdServlet();
        servlet.doGet(request, response);
        String result = sw.getBuffer().toString().trim();
        String expectedStr = "{\"keywordCampaignId\":1,\"userId\":2,\"DSACampaignIds\":[4,2,5],\"name\":\"entity 1\",\"fromDate\":\"1/1/1\",\"toDate\":\"2/2/2\",";
        expectedStr += "\"dailyBudget\":123.2,\"location\":\"CA\",\"domain\":\"google.com\",\"target\":\"google.com\",\"impressions\":432,\"clicks\":123,\"cost\":42.51}";
        assertEquals(new String(expectedStr), result);
    }
}
@@ -2586,1097 +2586,129 @@
     
-Entity keywordCampaignEntity = new Entity(%22keywordCampaign%22);%0A        keywordCampaignEntity.setProperty(%22keywordCampaignId%22, 1);%0A        keywordCampaignEntity.setProperty(%22userId%22, 2);%0A        keywordCampaignEntity.setProperty(%22name%22, %22entity 1%22);%0A        keywordCampaignEntity.setProperty(%22fromDate%22, %221/1/1%22);%0A        keywordCampaignEntity.setProperty(%22toDate%22, %222/2/2%22);%0A        keywordCampaignEntity.setProperty(%22dailyBudget%22, 123.2);%0A        keywordCampaignEntity.setProperty(%22location%22, %22CA%22);%0A        keywordCampaignEntity.setProperty(%22domain%22, %22google.com%22);%0A        keywordCampaignEntity.setProperty(%22target%22, %22google.com%22);%0A        keywordCampaignEntity.setProperty(%22impressions%22, 432);%0A        keywordCampaignEntity.setProperty(%22clicks%22, 123);%0A        keywordCampaignEntity.setProperty(%22cost%22, 42.51);%0A        ArrayList%3CInteger%3E DSACampaignIds = new ArrayList%3CInteger%3E();%0A        DSACampaignIds.add(4);%0A        DSACampaignIds.add(2);%0A        DSACampaignIds.add(5);%0A        keywordCampaignEntity.setProperty(%22DSACampaignIds%22, DSACampaignIds);%0A        ds.put(keywordCampaignEntity
+ds.put(KeywordCampaignsServlet.createEntityFromKeywordCampaign(%221%22, %222%22, %224 2 5%22.split(%22 %22), %22entity 1%22, 432, 123, 42.51)
 );%0A%0A
@@ -2935,17 +2935,21 @@
 ignId%5C%22:
-1
+%5C%221%5C%22
 ,%5C%22userI
@@ -2952,17 +2952,21 @@
 serId%5C%22:
-2
+%5C%222%5C%22
 ,%5C%22DSACa
@@ -2982,13 +2982,25 @@
 %5C%22:%5B
-4,2,5
+%5C%224%5C%22,%5C%222%5C%22,%5C%225%5C%22
 %5D,%5C%22
@@ -3023,173 +3023,35 @@
 1%5C%22,
-%5C%22fromDate%5C%22:%5C%221/1/1%5C%22,%5C%22toDate%5C%22:%5C%222/2/2%5C%22,%22;%0A        expectedStr += %22%5C%22dailyBudget%5C%22:123.2,%5C%22location%5C%22:%5C%22CA%5C%22,%5C%22domain%5C%22:%5C%22google.com%5C%22,%5C%22target%5C%22:%5C%22google.com%5C%22,
+%22;%0A        expectedStr += %22
 %5C%22im

Update keyword campaign id test.