diff --git a/common/azure-spring-boot-starter-parent/pom.xml b/common/azure-spring-boot-starter-parent/pom.xml index 71b9eb3c8..99ddf3cfc 100644 --- a/common/azure-spring-boot-starter-parent/pom.xml +++ b/common/azure-spring-boot-starter-parent/pom.xml @@ -128,7 +128,9 @@ private false - com/microsoft/azure/spring/data/documentdb/core/mapping/BasicDocumentDbPersistentProperty.java + + com/microsoft/azure/spring/data/documentdb/core/mapping/BasicDocumentDbPersistentProperty.java + diff --git a/common/config/findbugs-exclude.xml b/common/config/findbugs-exclude.xml index 5a48d6930..4436a900e 100644 --- a/common/config/findbugs-exclude.xml +++ b/common/config/findbugs-exclude.xml @@ -5,4 +5,5 @@ + \ No newline at end of file diff --git a/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/pom.xml b/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/pom.xml index 8d2ee4aa5..a2bc30691 100644 --- a/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/pom.xml +++ b/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/pom.xml @@ -54,11 +54,6 @@ org.springframework.social spring-social-config - - org.springframework.boot - spring-boot-starter-test - test - org.springframework.boot spring-boot-starter-validation @@ -67,5 +62,15 @@ com.microsoft.azure spring-social-microsoft-graph + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-web + test + \ No newline at end of file diff --git a/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/src/test/java/com/microsoft/azure/autoconfigure/msgraph/Constants.java b/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/src/test/java/com/microsoft/azure/autoconfigure/msgraph/Constants.java new file mode 100644 index 000000000..615f6e384 --- /dev/null +++ b/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/src/test/java/com/microsoft/azure/autoconfigure/msgraph/Constants.java @@ -0,0 +1,13 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for + * license information. + */ +package com.microsoft.azure.autoconfigure.msgraph; + +public class Constants { + public static final String APP_ID_PROPERTY = "spring.social.microsoft.app-id"; + public static final String APP_ID = "123456789-acb1-4d0b-a13b-1a70ac85d8bf"; + public static final String APP_SECRET_PROPERTY = "spring.social.microsoft.app-secret"; + public static final String APP_SECRET = "1234mAocWmbvawgg4hyRTZ8"; +} diff --git a/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/src/test/java/com/microsoft/azure/autoconfigure/msgraph/MicrosoftAutoConfigurationTest.java b/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/src/test/java/com/microsoft/azure/autoconfigure/msgraph/MicrosoftAutoConfigurationTest.java new file mode 100644 index 000000000..67bfea80b --- /dev/null +++ b/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/src/test/java/com/microsoft/azure/autoconfigure/msgraph/MicrosoftAutoConfigurationTest.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for + * license information. + */ + +package com.microsoft.azure.autoconfigure.msgraph; + +import com.microsoft.azure.msgraph.api.Microsoft; +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class MicrosoftAutoConfigurationTest { + @Test + public void canAutowire() { + System.setProperty(Constants.APP_ID_PROPERTY, Constants.APP_ID); + System.setProperty(Constants.APP_SECRET_PROPERTY, Constants.APP_SECRET); + + try (AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext()) { + context.register(MicrosoftAutoConfiguration.class); + context.register(SocialWebAutoConfiguration.class); + context.refresh(); + Assertions.assertThat(context.getBean(Microsoft.class)).isNotNull(); + } + + System.clearProperty(Constants.APP_ID_PROPERTY); + System.clearProperty(Constants.APP_SECRET_PROPERTY); + } + + @Test + public void cannotAutowire() { + try (AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext()) { + context.register(MicrosoftAutoConfiguration.class); + context.register(SocialWebAutoConfiguration.class); + context.refresh(); + + Microsoft microsoft = null; + try { + microsoft = context.getBean(Microsoft.class); + } catch (Exception e) { + assertThat(e.getMessage()).contains("No qualifying bean of type 'com.microsoft.azure." + + "msgraph.api.Microsoft' available"); + } + assertThat(microsoft).isNull(); + } + } +} diff --git a/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/src/test/java/com/microsoft/azure/autoconfigure/msgraph/MicrosoftPropertiesTest.java b/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/src/test/java/com/microsoft/azure/autoconfigure/msgraph/MicrosoftPropertiesTest.java new file mode 100644 index 000000000..085da0394 --- /dev/null +++ b/microsoft-graph/microsoft-graph-spring-boot-autoconfigure/src/test/java/com/microsoft/azure/autoconfigure/msgraph/MicrosoftPropertiesTest.java @@ -0,0 +1,39 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for + * license information. + */ + +package com.microsoft.azure.autoconfigure.msgraph; + +import org.junit.Test; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MicrosoftPropertiesTest { + @Test + public void canSetProperties() { + System.setProperty(Constants.APP_ID_PROPERTY, Constants.APP_ID); + System.setProperty(Constants.APP_SECRET_PROPERTY, Constants.APP_SECRET); + + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { + context.register(Config.class); + context.refresh(); + final MicrosoftProperties properties = context.getBean(MicrosoftProperties.class); + + assertThat(properties.getAppId()).isEqualTo(Constants.APP_ID); + assertThat(properties.getAppSecret()).isEqualTo(Constants.APP_SECRET); + } + + System.clearProperty(Constants.APP_ID_PROPERTY); + System.clearProperty(Constants.APP_SECRET_PROPERTY); + } + + @Configuration + @EnableConfigurationProperties(MicrosoftProperties.class) + static class Config { + } +} diff --git a/microsoft-graph/microsoft-graph-spring-social-starter-sample/README.md b/microsoft-graph/microsoft-graph-spring-social-starter-sample/README.md index f78e6cfa5..941b97f3c 100644 --- a/microsoft-graph/microsoft-graph-spring-social-starter-sample/README.md +++ b/microsoft-graph/microsoft-graph-spring-social-starter-sample/README.md @@ -34,4 +34,9 @@ Go to [Application Registration Portal](https://apps.dev.microsoft.com/#/appList ``` mvn package java -jar target/microsoft-graph-spring-social-starter-sample-0.0.1-SNAPSHOT.jar -``` \ No newline at end of file +``` + +### Make your own REST API call + +This starter implements a small subset of Objects/APIs available via Microsoft Graph (GET /me, POST /me/sendMail, GET /me/messages). In addition, it demonstrates how to make custom REST API calls (see function `getContacts` in `HelloController.java` as an example). Please check the detailed [document](https://developer.microsoft.com/en-us/graph/docs/concepts/overview) for more details about Microsoft Graph. +After reading the document, you should be aware of the exact REST API and what objects you should prepare or expect from the REST API call. diff --git a/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/java/sample/microsoft/graph/HelloController.java b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/java/sample/microsoft/graph/HelloController.java index dd2633688..14f3e3db8 100644 --- a/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/java/sample/microsoft/graph/HelloController.java +++ b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/java/sample/microsoft/graph/HelloController.java @@ -10,13 +10,14 @@ import org.springframework.social.connect.ConnectionRepository; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.client.RestTemplate; +import sample.microsoft.graph.custom.Contacts; + +import java.net.URI; @Controller -@RequestMapping("/") public class HelloController { - private Microsoft microsoft; private ConnectionRepository connectionRepository; @@ -25,8 +26,8 @@ public HelloController(Microsoft microsoft, ConnectionRepository connectionRepos this.connectionRepository = connectionRepository; } - @GetMapping - public String helloFacebook(Model model) { + @RequestMapping("/") + public String helloMicrosoft(Model model) { if (connectionRepository.findPrimaryConnection(Microsoft.class) == null) { return "redirect:/connect/microsoft"; } @@ -36,4 +37,17 @@ public String helloFacebook(Model model) { return "hello"; } + @RequestMapping("/contacts") + public String getContacts(Model model) { + if (connectionRepository.findPrimaryConnection(Microsoft.class) == null) { + return "redirect:/connect/microsoft"; + } + + final RestTemplate restTemplate = microsoft.customOperations().getRestTemplate(); + final URI uri = microsoft.customOperations().getGraphAPIURI("me/contacts"); + final Contacts contacts = restTemplate.getForObject(uri, Contacts.class); + model.addAttribute("contacts", contacts.getContacts()); + + return "contacts"; + } } diff --git a/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/java/sample/microsoft/graph/custom/Contact.java b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/java/sample/microsoft/graph/custom/Contact.java new file mode 100644 index 000000000..3ee043c76 --- /dev/null +++ b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/java/sample/microsoft/graph/custom/Contact.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for + * license information. + */ + +package sample.microsoft.graph.custom; + +// Please be noted this is not a complete representation of the JSON object returned by Microsoft Graph. +public class Contact { + + private String displayName; + private String mobilePhone; + + /** + * The Display Name. + */ + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + /** + * The Mobile Phone. + */ + public String getMobilePhone() { + return mobilePhone; + } + + public void setMobilePhone(String mobilePhone) { + this.mobilePhone = mobilePhone; + } +} diff --git a/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/java/sample/microsoft/graph/custom/Contacts.java b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/java/sample/microsoft/graph/custom/Contacts.java new file mode 100644 index 000000000..5e83db04d --- /dev/null +++ b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/java/sample/microsoft/graph/custom/Contacts.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE in the project root for + * license information. + */ + +package sample.microsoft.graph.custom; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Contacts { + @JsonProperty("value") + private java.util.List contacts; + + @JsonProperty("@odata.nextLink") + private String nextLink; + + public java.util.List getContacts() { + return contacts; + } + + public void setContacts(java.util.List contacts) { + this.contacts = contacts; + } + + /** + * The url to the next page of this collection, or null + */ + public String getNextLink() { + return nextLink; + } + + public void setNextLink(String nextLink) { + this.nextLink = nextLink; + } +} diff --git a/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/connect/microsoftConnect.html b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/connect/microsoftConnect.html index ee863d4ec..27fce0f4a 100644 --- a/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/connect/microsoftConnect.html +++ b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/connect/microsoftConnect.html @@ -6,7 +6,7 @@ Connect to Microsoft Graph - + You aren't connected to Microsoft yet. Click the button to connect this application with your Microsoft account. diff --git a/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/contacts.html b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/contacts.html new file mode 100644 index 000000000..2e8767d28 --- /dev/null +++ b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/contacts.html @@ -0,0 +1,17 @@ + + + Hello Microsoft Graph + + + + + Name + Phone + + + + + + + + \ No newline at end of file diff --git a/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/hello.html b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/hello.html index c38603b65..a95963c2a 100644 --- a/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/hello.html +++ b/microsoft-graph/microsoft-graph-spring-social-starter-sample/src/main/resources/templates/hello.html @@ -48,6 +48,10 @@ User Principal Name + + Contacts + click here +
You aren't connected to Microsoft yet. Click the button to connect this application with your Microsoft account.