You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2019/08/01 11:42:20 UTC

[isis] branch v2 updated: ISIS-2156 adds smoketest: complete Restful Client/Server use-case

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch v2
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/v2 by this push:
     new 7205f6f  ISIS-2156 adds smoketest: complete Restful Client/Server use-case
7205f6f is described below

commit 7205f6fbc58133f6283519a370174e3cdf24d3a7
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Aug 1 13:42:11 2019 +0200

    ISIS-2156 adds smoketest: complete Restful Client/Server use-case
---
 examples/smoketest/pom.xml                         | 24 ++++++
 .../isis/testdomain/jdo/InventoryManager.java      | 16 ++++
 .../isis/testdomain/jdo/InventoryRepository.java   | 36 ++++++++
 .../testdomain/rest/server/RestServerService.java  | 76 +++++++++++++++++
 .../rest/server/RestServerServiceTest.java         | 98 ++++++++++++++++++++++
 .../smoketest/src/test/resources/shiro-ldap.ini    |  4 +-
 6 files changed, 252 insertions(+), 2 deletions(-)

diff --git a/examples/smoketest/pom.xml b/examples/smoketest/pom.xml
index 1d7380f..0e52a9f 100644
--- a/examples/smoketest/pom.xml
+++ b/examples/smoketest/pom.xml
@@ -165,6 +165,30 @@
 		<!-- TEST DEPENDENCIES -->
 		
 		<dependency>
+			<groupId>org.apache.isis.core</groupId>
+			<artifactId>isis-plugins-jaxrs-resteasy-4</artifactId>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+		    <groupId>javax.ws.rs</groupId>
+		    <artifactId>javax.ws.rs-api</artifactId>
+		    <version>2.1.1</version>
+		    <scope>test</scope>
+		</dependency>
+		<dependency>
+		    <groupId>org.glassfish.jersey.core</groupId>
+		    <artifactId>jersey-client</artifactId>
+		    <version>2.25.1</version>
+		    <scope>test</scope>
+		</dependency>
+		<dependency>
+		    <groupId>org.eclipse.persistence</groupId>
+		    <artifactId>org.eclipse.persistence.moxy</artifactId>
+		    <version>2.6.0</version>
+		    <scope>test</scope>
+		</dependency>
+		
+		<dependency>
 			<groupId>org.apache.isis.mavendeps</groupId>
 			<artifactId>isis-mavendeps-testing</artifactId>
 			<scope>test</scope>
diff --git a/examples/smoketest/src/main/java/org/apache/isis/testdomain/jdo/InventoryManager.java b/examples/smoketest/src/main/java/org/apache/isis/testdomain/jdo/InventoryManager.java
index 40d044c..fbfc356 100644
--- a/examples/smoketest/src/main/java/org/apache/isis/testdomain/jdo/InventoryManager.java
+++ b/examples/smoketest/src/main/java/org/apache/isis/testdomain/jdo/InventoryManager.java
@@ -18,13 +18,18 @@
  */
 package org.apache.isis.testdomain.jdo;
 
+import javax.inject.Inject;
+
 import org.apache.isis.applib.annotation.Action;
 import org.apache.isis.applib.annotation.ViewModel;
 import org.apache.isis.applib.domain.DomainObjectList.ActionDomainEvent;
+import org.apache.isis.applib.services.repository.RepositoryService;
 
 @ViewModel
 public class InventoryManager {
 
+	// -- UPDATE PRODUCT PRICE
+	
 	public static class UpdateProductPriceEvent extends ActionDomainEvent {
 		private static final long serialVersionUID = 1L;}
 	
@@ -34,4 +39,15 @@ public class InventoryManager {
         return product;
     }
     
+	// -- COUNT PRODUCTS
+    
+    @Action
+    public int countProducts() {
+        return repository.allInstances(Product.class).size();
+    }
+    
+    // -- DEPENDENCIES
+    
+    @Inject RepositoryService repository;
+    
 }
diff --git a/examples/smoketest/src/main/java/org/apache/isis/testdomain/jdo/InventoryRepository.java b/examples/smoketest/src/main/java/org/apache/isis/testdomain/jdo/InventoryRepository.java
new file mode 100644
index 0000000..4701d6d
--- /dev/null
+++ b/examples/smoketest/src/main/java/org/apache/isis/testdomain/jdo/InventoryRepository.java
@@ -0,0 +1,36 @@
+package org.apache.isis.testdomain.jdo;
+
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.isis.applib.annotation.Action;
+import org.apache.isis.applib.annotation.DomainService;
+import org.apache.isis.applib.annotation.NatureOfService;
+import org.apache.isis.applib.services.repository.RepositoryService;
+
+@DomainService(
+		nature = NatureOfService.VIEW_REST_ONLY,
+		objectType = "testdomain.InventoryRepository")
+public class InventoryRepository {
+
+	@Action
+	public List<Product> listProducts() {
+		return repository.allInstances(Product.class);
+	}
+	
+	@Action
+	public List<Book> listBooks() {
+		return repository.allInstances(Book.class);
+	}
+	
+	@Action
+	public Book recommendedBookOfTheWeek() {
+		return Book.of("Book of the week", "An awesome Book", 12, "Author", "ISBN", "Publisher");
+	}
+	
+    // -- DEPENDENCIES
+    
+    @Inject RepositoryService repository;
+	
+}
diff --git a/examples/smoketest/src/test/java/org/apache/isis/testdomain/rest/server/RestServerService.java b/examples/smoketest/src/test/java/org/apache/isis/testdomain/rest/server/RestServerService.java
new file mode 100644
index 0000000..c508fe0
--- /dev/null
+++ b/examples/smoketest/src/test/java/org/apache/isis/testdomain/rest/server/RestServerService.java
@@ -0,0 +1,76 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF 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
+ *
+ *        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 org.apache.isis.testdomain.rest.server;
+
+import javax.inject.Inject;
+
+import org.apache.isis.applib.client.RestfulClient;
+import org.apache.isis.applib.client.RestfulClientConfig;
+import org.apache.isis.commons.internal.resources._Resources;
+import org.springframework.core.env.Environment;
+import org.springframework.stereotype.Service;
+
+import lombok.val;
+import lombok.extern.log4j.Log4j2;
+
+@Service @Log4j2
+public class RestServerService {
+
+	public int getPort() {
+		if(port==null) {
+			init();
+		}
+		return port;
+	}
+
+	public RestfulClient newClient() {
+		
+		val restRootPath = 
+				"http://localhost:" + getPort() + "/" + 
+				_Resources.prependContextPathIfPresent(_Resources.getRestfulPathOrThrow());
+
+		log.info("new restful client created for {}", restRootPath);
+		
+		RestfulClientConfig clientConfig = new RestfulClientConfig();
+		clientConfig.setRestfulBase(restRootPath);
+		// setup basic-auth
+		clientConfig.setUseBasicAuth(true); // default = false
+		clientConfig.setRestfulAuthUser("sven");
+		clientConfig.setRestfulAuthPassword("pass");
+		// setup request/response debug logging
+		clientConfig.setUseRequestDebugLogging(true); // default = false
+
+		RestfulClient client = RestfulClient.ofConfig(clientConfig);
+
+		return client;
+	}	
+
+	// -- HELPER
+
+	private Integer port;
+
+	private void init() {
+		port = Integer.parseInt(environment.getProperty("local.server.port"));
+	}
+
+	// -- DEPENDENCIES
+
+	@Inject Environment environment;
+
+}
diff --git a/examples/smoketest/src/test/java/org/apache/isis/testdomain/rest/server/RestServerServiceTest.java b/examples/smoketest/src/test/java/org/apache/isis/testdomain/rest/server/RestServerServiceTest.java
new file mode 100644
index 0000000..10b46d4
--- /dev/null
+++ b/examples/smoketest/src/test/java/org/apache/isis/testdomain/rest/server/RestServerServiceTest.java
@@ -0,0 +1,98 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF 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
+ *
+ *        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 org.apache.isis.testdomain.rest.server;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import javax.inject.Inject;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation.Builder;
+import javax.ws.rs.core.Response;
+
+import org.apache.isis.applib.client.ResponseDigest;
+import org.apache.isis.applib.client.RestfulClient;
+import org.apache.isis.applib.client.SuppressionType;
+import org.apache.isis.testdomain.jdo.Book;
+import org.apache.isis.testdomain.jdo.JdoTestDomainModule;
+import org.apache.isis.viewer.restfulobjects.IsisBootWebRestfulObjects;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.context.annotation.Import;
+
+import lombok.val;
+
+@SpringBootTest(
+		classes = {RestServerService.class},
+		properties = {
+				"logging.config=log4j2-test.xml",
+		},
+		webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Import({
+	JdoTestDomainModule.class,
+	IsisBootWebRestfulObjects.class
+})
+class RestServerServiceTest {
+
+	@LocalServerPort int port;
+	@Inject RestServerService restServerService;
+
+	@Test
+	void test() throws InterruptedException {
+		
+		//Thread.sleep(10000000000L);
+		
+		assertNotNull(restServerService.getPort());
+		assertTrue(restServerService.getPort()>0);
+
+		RestfulClient client = restServerService.newClient();
+
+		Builder request = client.request(
+				"services/testdomain.InventoryRepository/actions/recommendedBookOfTheWeek/invoke", 
+				SuppressionType.ALL);
+
+		Entity<String> args = client.arguments()
+				.build();
+
+		Response response = request.post(args);
+
+		ResponseDigest<Book> digest = client.digest(response, Book.class);
+
+		if(digest.isSuccess()) {
+		
+			val bookOfTheWeek = digest.get();
+			System.out.println("result: "+ bookOfTheWeek);
+			
+			
+			assertNotNull(bookOfTheWeek);
+			assertEquals("Book of the week", bookOfTheWeek.getName());
+			
+
+		} else {
+			
+			fail(digest.getFailureCause());
+			
+		}
+
+	}
+
+}
diff --git a/examples/smoketest/src/test/resources/shiro-ldap.ini b/examples/smoketest/src/test/resources/shiro-ldap.ini
index 02d1bb8..2340e4b 100644
--- a/examples/smoketest/src/test/resources/shiro-ldap.ini
+++ b/examples/smoketest/src/test/resources/shiro-ldap.ini
@@ -43,8 +43,8 @@ ldapRealm.rolesByGroup = \
     DEMOS: self-install_role
 
 ldapRealm.permissionsByRole=\
-   user_role = *:SimpleObjectMenu:*:*,\
-               *:SimpleObject:*:*; \
+   user_role = *:InventoryManager:*:*,\
+               *:Product:*:*; \
    self-install_role = *:FixtureScriptsDefault:*:* ; \
    admin_role = *