com.formkiq.web.LicenseSeleniumTest.java Source code

Java tutorial

Introduction

Here is the source code for com.formkiq.web.LicenseSeleniumTest.java

Source

/*
 * Copyright (C) 2017 FormKiQ Inc.
 *
 * 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
 *
 * http://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.formkiq.web;

import static com.formkiq.core.api.ClientsController.API_CLIENT_LIST;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.formkiq.core.domain.type.ClientDTO;
import com.formkiq.core.domain.type.ClientListDTO;
import com.formkiq.core.form.JSONService;
import com.formkiq.core.util.Resources;

/**
 * Users Add / Edit Integration Test.
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = DEFINED_PORT)
@ActiveProfiles("dev")
public class LicenseSeleniumTest extends SeleniumTestBase {

    /** {@link JSONService}. */
    @Autowired
    private JSONService jsonService;

    @Override
    @Before
    public void before() throws Exception {
        super.before();

        login(getDefaultEmail());

        click(By.id("lilicensehref"));
        assertEquals("FormKiQ Server - License", getTitle());
    }

    /**
     * invalid license.
     */
    @Test
    public void testLicense01() {
        // given
        String license = "adsaadad";

        // when
        findElementBy("textarea", "data-valuekey", "license").sendKeys(license);
        click(By.name("_eventId_next"));

        // then
        assertEquals("Unregistered Server", findElementBy(By.className("form-section-label")).getText());
        assertTrue(getPageSource().contains("Invalid License Key"));
    }

    /**
     * valid license.
     * @throws Exception Exception
     */
    @Test
    public void testLicense02() throws Exception {
        // given
        String license = Resources.getResourceAsString("/TestCompany.lic");

        // when
        findElementBy("textarea", "data-valuekey", "license").sendKeys(license);
        click(By.name("_eventId_next"));

        // then
        assertEquals("Registered Server", findElementBy(By.className("form-section-label")).getText());
        assertEquals(license, findElementBy(getBy("textarea", "data-valuekey", "license")).getText());

        String info = findElementBy(getBy("div", "data-valuekey", "info")).getText();
        assertTrue(info.contains("Company: Test Company"));
        assertTrue(info.contains("Issue Date: Sun Jun 25 21:27:33 CDT 2017"));
        assertTrue(info.contains("Expiry Date: Sun Jun 25 21:27:33 CDT 2017"));
        assertTrue(info.contains("Issued By: FormKiQ Inc."));

        String token = login("testapp", "testapp", getDefaultEmail(), getDefaultPass());

        String url = getDefaultHostAndPort() + API_CLIENT_LIST + "?access_token=" + token;

        final int clientCount = 3;
        ResponseEntity<String> entity = exchangeRest(HttpMethod.GET, url);
        assertEquals(SC_OK, entity.getStatusCode().value());
        ClientListDTO dto = this.jsonService.readValue(entity.getBody(), ClientListDTO.class);

        List<ClientDTO> clients = dto.getClients();
        Collections.sort(clients, new Comparator<ClientDTO>() {

            @Override
            public int compare(final ClientDTO o1, final ClientDTO o2) {
                return o1.getClientname().compareTo(o2.getClientname());
            }
        });

        assertEquals(clientCount, dto.getClients().size());
        assertEquals("testapp", dto.getClients().get(0).getClient());
        assertEquals("Test App", dto.getClients().get(0).getClientname());
        assertEquals("password", dto.getClients().get(0).getGranttypesasstring());

        assertEquals("TestCompany", dto.getClients().get(1).getClient());
        assertEquals("Test Company", dto.getClients().get(1).getClientname());
        assertEquals("password", dto.getClients().get(1).getGranttypesasstring());

        assertEquals(getClientId(), dto.getClients().get(2).getClient());
        assertEquals(getClientId(), dto.getClients().get(2).getClientname());
        assertEquals("authorization_code,password,refresh_token", dto.getClients().get(2).getGranttypesasstring());
    }
}