Anonymous avatar Anonymous committed 7918bd8

First commit

Comments (0)

Files changed (14)

+Copyright (c) 2013, Atlassian
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+    Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+You can find the full instructions for this tutorial here:
+
+https://developer.atlassian.com/display/DOCS/Developing+a+REST+Service+Plugin
+
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.atlassian.plugins.tutorial</groupId>
+    <artifactId>Message</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <organization>
+        <name>Example Company</name>
+        <url>http://www.example.com/</url>
+    </organization>
+    <name>Message</name>
+    <description>This is the com.atlassian.plugins.tutorial:Message plugin for Atlassian Refapp.</description>
+    <packaging>atlassian-plugin</packaging>
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.6</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <version>2.4</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.ws.rs</groupId>
+            <artifactId>jsr311-api</artifactId>
+            <version>1.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.xml.bind</groupId>
+            <artifactId>jaxb-api</artifactId>
+            <version>2.1</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.plugins.rest</groupId>
+            <artifactId>atlassian-rest-common</artifactId>
+            <version>1.0.2</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.sal</groupId>
+            <artifactId>sal-api</artifactId>
+            <version>2.6.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.wink</groupId>
+            <artifactId>wink-client</artifactId>
+            <version>1.1.3-incubating</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.8.5</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>com.atlassian.refapp</groupId>
+                <artifactId>atlassian-platform</artifactId>
+                <version>${refapp.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>com.atlassian.maven.plugins</groupId>
+                <artifactId>maven-refapp-plugin</artifactId>
+                <version>${amps.version}</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <productVersion>${refapp.version}</productVersion>
+                </configuration>
+            </plugin>
+            <plugin>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.6</source>
+                    <target>1.6</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    <properties>
+        <refapp.version>2.20.0-m3</refapp.version>
+        <amps.version>4.0</amps.version>
+    </properties>
+</project>

src/main/java/com/atlassian/plugins/tutorial/ExampleServlet.java

+package com.atlassian.plugins.tutorial;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class ExampleServlet extends HttpServlet
+{
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
+    {
+        res.getWriter().write("Hello world");
+        res.getWriter().close();
+    }
+}

src/main/java/com/atlassian/plugins/tutorial/rest/MyRestResource.java

+package com.atlassian.plugins.tutorial.rest;
+
+import com.atlassian.plugins.rest.common.security.AnonymousAllowed;
+
+import javax.ws.rs.*;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+/**
+ * A resource of message.
+ */
+@Path("/message")
+public class MyRestResource {
+
+    @GET
+    @AnonymousAllowed
+    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+
+    public Response getMessage(@QueryParam("key") String key)
+    {
+        if(key!=null)
+            return Response.ok(new MyRestResourceModel(key, getMessageFromKey(key))).build();
+        else
+            return Response.ok(new MyRestResourceModel("default","Hello World")).build();
+    }
+
+    @GET
+    @AnonymousAllowed
+    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+    @Path("/{key}")
+    public Response getMessageFromPath(@PathParam("key") String key)
+    {
+        return Response.ok(new MyRestResourceModel(key, getMessageFromKey(key))).build();
+    }
+ 
+    private String getMessageFromKey(String key) {
+        // In reality, this data would come from a database or some component
+        // within the hosting application, for demonstration purpopses I will
+        // just return the key
+        return key;
+    }
+ 
+
+
+}

src/main/java/com/atlassian/plugins/tutorial/rest/MyRestResourceModel.java

+package com.atlassian.plugins.tutorial.rest;
+
+import javax.xml.bind.annotation.*;
+@XmlRootElement(name = "message")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class MyRestResourceModel {
+
+    @XmlAttribute
+    private String key;
+
+
+    @XmlElement(name = "value")
+    private String message;
+
+    public MyRestResourceModel() {
+    }
+
+    public MyRestResourceModel(String key, String message) {
+        this.key = key;
+        this.message = message;
+    }
+ 
+    public String getKey() {
+        return key;
+    }
+ 
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+}

src/main/resources/atlassian-plugin.xml

+<?xml version="1.0" encoding="UTF-8"?>
+
+<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.name}" plugins-version="2">
+  <plugin-info>
+    <description>${project.description}</description>
+    <version>${project.version}</version>
+    <vendor name="${project.organization.name}" url="${project.organization.url}"/>
+  </plugin-info>
+  <servlet name="Message" class="com.atlassian.plugins.tutorial.ExampleServlet" key="test">
+    <url-pattern>/test</url-pattern>
+  </servlet>
+  <resource type="i18n" name="i18n" location="com.atlassian.plugins.tutorial.Message"/>
+  <rest name="My Rest Resource" i18n-name-key="my-rest-resource.name" key="my-rest-resource" path="/myrestresource" version="1.0">
+    <description key="my-rest-resource.description">The My Rest Resource Plugin</description>
+  </rest>
+</atlassian-plugin>

src/main/resources/com/atlassian/plugins/tutorial/Message.properties

+
+my-rest-resource.name=My Rest Resource
+my-rest-resource.description=The My Rest Resource Plugin

src/test/java/com/atlassian/plugins/tutorial/ExampleServletTest.java

+package com.atlassian.plugins.tutorial;
+
+import org.junit.Test;
+
+/**
+ * Testing {@link com.atlassian.plugins.tutorial.ExampleServlet}
+ */
+public class ExampleServletTest
+{
+    @Test
+    public void basic()
+    {
+        // add test here...
+    }
+}

src/test/java/com/atlassian/plugins/tutorial/rest/MyRestResourceTest.java

+package com.atlassian.plugins.tutorial.rest;
+
+import org.junit.Test;
+import org.junit.After;
+import org.junit.Before;
+import org.mockito.Mockito;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+import com.atlassian.plugins.tutorial.rest.MyRestResource;
+import com.atlassian.plugins.tutorial.rest.MyRestResourceModel;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.GenericEntity;
+
+public class MyRestResourceTest {
+
+    @Before
+    public void setup() {
+
+    }
+
+    @After
+    public void tearDown() {
+
+    }
+
+    @Test
+    public void messageIsValid() {
+        MyRestResource resource = new MyRestResource();
+
+        Response response = resource.getMessage("Hello World");
+        final MyRestResourceModel message = (MyRestResourceModel) response.getEntity();
+
+        assertEquals("wrong message","Hello World",message.getMessage()); 
+
+    }
+}

src/test/java/com/atlassian/plugins/tutorial/rest/MyRestResourceTest.java.backup

+package com.atlassian.plugins.tutorial.rest;
+
+import org.junit.Test;
+import org.junit.After;
+import org.junit.Before;
+import org.mockito.Mockito;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+import com.atlassian.plugins.tutorial.rest.MyRestResource;
+import com.atlassian.plugins.tutorial.rest.MyRestResourceModel;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.GenericEntity;
+
+public class MyRestResourceTest {
+
+    @Before
+    public void setup() {
+
+    }
+
+    @After
+    public void tearDown() {
+
+    }
+
+    @Test
+    public void messageIsValid() {
+        MyRestResource resource = new MyRestResource();
+
+        Response response = resource.getMessage();
+        final MyRestResourceModel message = (MyRestResourceModel) response.getEntity();
+
+        assertEquals("wrong message","Hello World",message.getMessage());
+    }
+}

src/test/java/it/IntegrationTestMyPlugin.java

+package it;
+
+import junit.framework.TestCase;
+
+public class IntegrationTestMyPlugin extends TestCase
+{
+	public void testSomething()
+	{
+	}
+}

src/test/java/it/com/atlassian/plugins/tutorial/rest/MyRestResourceFuncTest.java

+package it.com.atlassian.plugins.tutorial.rest;
+
+import org.junit.Test;
+import org.junit.After;
+import org.junit.Before;
+import org.mockito.Mockito;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+import com.atlassian.plugins.tutorial.rest.MyRestResource;
+import com.atlassian.plugins.tutorial.rest.MyRestResourceModel;
+import org.apache.wink.client.Resource;
+import org.apache.wink.client.RestClient;
+
+public class MyRestResourceFuncTest {
+
+    @Before
+    public void setup() {
+
+    }
+
+    @After
+    public void tearDown() {
+
+    }
+
+    @Test
+    public void messageIsValid() {
+
+        String baseUrl = System.getProperty("baseurl");
+        String resourceUrl = baseUrl + "/rest/myrestresource/1.0/message";
+
+        RestClient client = new RestClient();
+        Resource resource = client.resource(resourceUrl);
+
+        MyRestResourceModel message = resource.get(MyRestResourceModel.class);
+
+        assertEquals("wrong message","Hello World",message.getMessage());
+    }
+}

src/test/resources/TEST_RESOURCES_README.txt

+Create any of the test resources you might need in this directory.
+
+Please remove this file before releasing your plugin.
+ 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.