m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/pl/edu/mimuw/cloudatlas/client/ClientTest.java
blob: aba2fc78c6eaf7359cfa57fca7cc4476b9826fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package pl.edu.mimuw.cloudatlas.client;

import org.hamcrest.CoreMatchers;
import org.junit.AfterClass;
import org.junit.Ignore;
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import pl.edu.mimuw.cloudatlas.api.Api;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import static org.junit.Assert.assertThat;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;

@Ignore
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ClientTest {
    private static Process registryProcess;
    private static Process agentProcess;
    private static Process querySignerProcess;
    private static Registry registry;
    private static Api api;

    @BeforeClass
    public static void bindApi() throws Exception {
        registryProcess = Runtime.getRuntime().exec("./scripts/registry");
        Thread.sleep(10000);
        agentProcess = Runtime.getRuntime().exec("./gradlew runAgent -Dhostname=localhost -DfreshnessPeriod=10000000");
        Thread.sleep(10000);

        registry = LocateRegistry.getRegistry("localhost");
        api = (Api) registry.lookup("Api");
    }

    @AfterClass
    public static void killProcesses() throws Exception {
        try {
            registryProcess.destroy();
            agentProcess.destroy();
            querySignerProcess.destroy();
        } catch (Exception e) {
            System.out.println("Caught exception: " + e);
        }
    }

    @LocalServerPort
    private int port;

    @Autowired
    private MockMvc mvc;

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void homeMessageCheck() throws Exception {
        String response = this.restTemplate.getForObject("http://localhost:" + port + "/", String.class);
        assertThat(response, CoreMatchers.containsString("Welcome to CloudaAtlas client interface"));
    }

    @Test
    public void attributeValuesMessageCheck() throws Exception {
        String response = this.restTemplate.getForObject("http://localhost:" + port + "/values", String.class);
        assertThat(response, CoreMatchers.containsString("Attribute values"));
    }

    @Test
    @Ignore
    public void queryInstallationCheck() throws Exception {
        this.mvc.perform(post("/installQuery")
                        .param("name", "&sampleQuery")
                        .param("value", "SELECT 1 AS one"))
                .andExpect(status().isOk()).andExpect(content()
                .contentType("text/html;charset=UTF-8"))
                .andExpect(content().string(CoreMatchers.containsString("Query installed successfully")));
    }

    @Test
    @Ignore
    public void queryUninstallationCheck() throws Exception {
        this.mvc.perform(post("/installQuery")
                .param("name", "&sampleQuery")
                .param("value", "SELECT 1 AS one"))
                .andExpect(status().isOk()).andExpect(content()
                .contentType("text/html;charset=UTF-8"))
                .andExpect(content().string(CoreMatchers.containsString("Query installed successfully")));

        this.mvc.perform(post("/uninstallQuery")
                .param("name", "&sampleQuery"))
                .andExpect(status().isOk()).andExpect(content()
                .contentType("text/html;charset=UTF-8"))
                .andExpect(content().string(CoreMatchers.containsString("Query uninstalled successfully")));
    }

    @Test
    public void attributeInstallationCheck() throws Exception {
        this.mvc.perform(post("/attribs")
                .param("zoneName", "/")
                .param("attributeName", "a")
                .param("attributeType", "Int")
                .param("valueString", "1"))
                .andExpect(status().isOk()).andExpect(content()
                .contentType("text/html;charset=UTF-8"))
                .andExpect(content().string(CoreMatchers.containsString("Attribute submitted successfully")));
    }

    @Test
    public void complexAttributeInstallationCheck() throws Exception {
        this.mvc.perform(post("/attribs")
                .param("zoneName", "/")
                .param("attributeName", "a")
                .param("attributeType", "List")
                .param("attributeComplexType", "List, Set, Int")
                .param("valueString", "[[1]]"))
                .andExpect(status().isOk())
                .andExpect(content().contentType("text/html;charset=UTF-8"))
                .andExpect(content().string(CoreMatchers.containsString("Attribute submitted successfully")));
    }

    @Ignore
    @Test
    public void numericalRESTApiCheck() throws Exception {
        Thread.sleep(10000);
        this.mvc.perform(get("/attribNumValues")
                .accept(MediaType.TEXT_PLAIN))
                .andExpect(status().isOk())
                .andExpect(content().string(CoreMatchers.containsString("num_processes")));
    }

    @Ignore
    @Test
    public void allValuesRESTApiCheck() throws Exception {
        Thread.sleep(10000);
        mvc.perform(get("/attribAllValues")
                .accept(MediaType.TEXT_PLAIN))
                .andExpect(status().isOk())
                .andExpect(content().string(CoreMatchers.containsString("contacts")));
    }
}