#297 Move API Gateway code into a new submodule: api-gateway-service. Change api-gateway packaging type to "pom". Update price-microservice and image-microservice to be true submodules of api-gateway
This commit is contained in:
		| @@ -0,0 +1,64 @@ | ||||
| /** | ||||
|  * The MIT License | ||||
|  * Copyright (c) 2014 Ilkka Seppälä | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| package com.iluwatar.api.gateway; | ||||
|  | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
|  | ||||
| /** | ||||
|  * The ApiGateway aggregates calls to microservices based on the needs of the individual clients. | ||||
|  */ | ||||
| @RestController | ||||
| public class ApiGateway { | ||||
|  | ||||
|   @Resource | ||||
|   private ImageClient imageClient; | ||||
|  | ||||
|   @Resource | ||||
|   private PriceClient priceClient; | ||||
|  | ||||
|   /** | ||||
|    * Retrieves product information that desktop clients need | ||||
|    * @return Product information for clients on a desktop | ||||
|    */ | ||||
|   @RequestMapping("/desktop") | ||||
|   public DesktopProduct getProductDesktop() { | ||||
|     DesktopProduct desktopProduct = new DesktopProduct(); | ||||
|     desktopProduct.setImagePath(imageClient.getImagePath()); | ||||
|     desktopProduct.setPrice(priceClient.getPrice()); | ||||
|     return desktopProduct; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * Retrieves product information that mobile clients need | ||||
|    * @return Product information for clients on a mobile device | ||||
|    */ | ||||
|   @RequestMapping("/mobile") | ||||
|   public MobileProduct getProductMobile() { | ||||
|     MobileProduct mobileProduct = new MobileProduct(); | ||||
|     mobileProduct.setPrice(priceClient.getPrice()); | ||||
|     return mobileProduct; | ||||
|   } | ||||
| } | ||||
| @@ -0,0 +1,66 @@ | ||||
| /** | ||||
|  * The MIT License | ||||
|  * Copyright (c) 2014 Ilkka Seppälä | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| package com.iluwatar.api.gateway; | ||||
|  | ||||
| import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
|  | ||||
| /** | ||||
|  * With the Microservices pattern, a client may need data from multiple different microservices. | ||||
|  * If the client called each microservice directly, that could contribute to longer load times, | ||||
|  * since the client would have to make a network request for each microservice called. Moreover, | ||||
|  * having the client call each microservice directly ties the client to that microservice - if the | ||||
|  * internal implementations of the microservices change (for example, if two microservices are | ||||
|  * combined sometime in the future) or if the location (host and port) of a microservice changes, | ||||
|  * then every client that makes use of those microservices must be updated. | ||||
|  * | ||||
|  * <p> | ||||
|  * The intent of the API Gateway pattern is to alleviate some of these issues. In the API Gateway | ||||
|  * pattern, an additional entity (the API Gateway) is placed between the client and the | ||||
|  * microservices. The job of the API Gateway is to aggregate the calls to the microservices. | ||||
|  * Rather than the client calling each microservice individually, the client calls the API Gateway | ||||
|  * a single time. The API Gateway then calls each of the microservices that the client needs. | ||||
|  * | ||||
|  * <p> | ||||
|  * This implementation shows what the API Gateway pattern could look like for an e-commerce site. | ||||
|  * The {@link ApiGateway} makes calls to the Image and Price microservices using the | ||||
|  * {@link ImageClientImpl} and {@link PriceClientImpl} respectively. Customers viewing the site on a | ||||
|  * desktop device can see both price information and an image of a product, so the {@link ApiGateway} | ||||
|  * calls both of the microservices and aggregates the data in the {@link DesktopProduct} model. | ||||
|  * However, mobile users only see price information; they do not see a product image. For mobile | ||||
|  * users, the {@link ApiGateway} only retrieves price information, which it uses to populate the | ||||
|  * {@link MobileProduct}. | ||||
|  */ | ||||
| @SpringBootApplication | ||||
| public class App { | ||||
|  | ||||
|   /** | ||||
|    * Program entry point | ||||
|    * | ||||
|    * @param args | ||||
|    *          command line args | ||||
|    */ | ||||
|   public static void main(String[] args) { | ||||
|     SpringApplication.run(App.class, args); | ||||
|   } | ||||
| } | ||||
| @@ -0,0 +1,54 @@ | ||||
| /** | ||||
|  * The MIT License | ||||
|  * Copyright (c) 2014 Ilkka Seppälä | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| package com.iluwatar.api.gateway; | ||||
|  | ||||
| /** | ||||
|  * Encapsulates all of the information that a desktop client needs to display a product. | ||||
|  */ | ||||
| public class DesktopProduct { | ||||
|   /** | ||||
|    * The price of the product | ||||
|    */ | ||||
|   private String price; | ||||
|  | ||||
|   /** | ||||
|    * The path to the image of the product | ||||
|    */ | ||||
|   private String imagePath; | ||||
|  | ||||
|   public String getPrice() { | ||||
|     return price; | ||||
|   } | ||||
|  | ||||
|   public void setPrice(String price) { | ||||
|     this.price = price; | ||||
|   } | ||||
|  | ||||
|   public String getImagePath() { | ||||
|     return imagePath; | ||||
|   } | ||||
|  | ||||
|   public void setImagePath(String imagePath) { | ||||
|     this.imagePath = imagePath; | ||||
|   } | ||||
| } | ||||
| @@ -0,0 +1,30 @@ | ||||
| /** | ||||
|  * The MIT License | ||||
|  * Copyright (c) 2014 Ilkka Seppälä | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| package com.iluwatar.api.gateway; | ||||
|  | ||||
| /** | ||||
|  * An interface used to communicate with the Image microservice | ||||
|  */ | ||||
| public interface ImageClient { | ||||
|   String getImagePath(); | ||||
| } | ||||
| @@ -0,0 +1,56 @@ | ||||
| /** | ||||
|  * The MIT License | ||||
|  * Copyright (c) 2014 Ilkka Seppälä | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| package com.iluwatar.api.gateway; | ||||
|  | ||||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||||
| import org.apache.http.client.methods.HttpGet; | ||||
| import org.apache.http.impl.client.CloseableHttpClient; | ||||
| import org.apache.http.impl.client.HttpClients; | ||||
| import org.apache.http.util.EntityUtils; | ||||
| import org.springframework.stereotype.Component; | ||||
|  | ||||
| import java.io.IOException; | ||||
|  | ||||
| /** | ||||
|  * An adapter to communicate with the Image microservice | ||||
|  */ | ||||
| @Component | ||||
| public class ImageClientImpl implements ImageClient{ | ||||
|   /** | ||||
|    * Makes a simple HTTP Get request to the Image microservice | ||||
|    * @return The path to the image | ||||
|    */ | ||||
|   @Override | ||||
|   public String getImagePath() { | ||||
|     String response = null; | ||||
|     try (CloseableHttpClient httpClient = HttpClients.createDefault()) { | ||||
|       HttpGet httpGet = new HttpGet("http://localhost:50005/image-path"); | ||||
|       try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { | ||||
|         response = EntityUtils.toString(httpResponse.getEntity()); | ||||
|       } | ||||
|     } catch (IOException e) { | ||||
|       e.printStackTrace(); | ||||
|     } | ||||
|     return response; | ||||
|   } | ||||
| } | ||||
| @@ -0,0 +1,41 @@ | ||||
| /** | ||||
|  * The MIT License | ||||
|  * Copyright (c) 2014 Ilkka Seppälä | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| package com.iluwatar.api.gateway; | ||||
|  | ||||
| /** | ||||
|  * Encapsulates all of the information that mobile client needs to display a product. | ||||
|  */ | ||||
| public class MobileProduct { | ||||
|   /** | ||||
|    * The price of the product | ||||
|    */ | ||||
|   private String price; | ||||
|  | ||||
|   public String getPrice() { | ||||
|     return price; | ||||
|   } | ||||
|  | ||||
|   public void setPrice(String price) { | ||||
|     this.price = price; | ||||
|   } | ||||
| } | ||||
| @@ -0,0 +1,30 @@ | ||||
| /** | ||||
|  * The MIT License | ||||
|  * Copyright (c) 2014 Ilkka Seppälä | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| package com.iluwatar.api.gateway; | ||||
|  | ||||
| /** | ||||
|  * An interface used to communicate with the Price microservice | ||||
|  */ | ||||
| public interface PriceClient { | ||||
|   String getPrice(); | ||||
| } | ||||
| @@ -0,0 +1,56 @@ | ||||
| /** | ||||
|  * The MIT License | ||||
|  * Copyright (c) 2014 Ilkka Seppälä | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| package com.iluwatar.api.gateway; | ||||
|  | ||||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||||
| import org.apache.http.client.methods.HttpGet; | ||||
| import org.apache.http.impl.client.CloseableHttpClient; | ||||
| import org.apache.http.impl.client.HttpClients; | ||||
| import org.apache.http.util.EntityUtils; | ||||
| import org.springframework.stereotype.Component; | ||||
|  | ||||
| import java.io.IOException; | ||||
|  | ||||
| /** | ||||
|  * An adapter to communicate with the Price microservice | ||||
|  */ | ||||
| @Component | ||||
| public class PriceClientImpl implements PriceClient{ | ||||
|   /** | ||||
|    * Makes a simple HTTP Get request to the Price microservice | ||||
|    * @return The price of the product | ||||
|    */ | ||||
|   @Override | ||||
|   public String getPrice() { | ||||
|     String response = null; | ||||
|     try (CloseableHttpClient httpClient = HttpClients.createDefault()) { | ||||
|       HttpGet httpGet = new HttpGet("http://localhost:50006/price"); | ||||
|       try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { | ||||
|         response = EntityUtils.toString(httpResponse.getEntity()); | ||||
|       } | ||||
|     } catch (IOException e) { | ||||
|       e.printStackTrace(); | ||||
|     } | ||||
|     return response; | ||||
|   } | ||||
| } | ||||
| @@ -0,0 +1,24 @@ | ||||
| # | ||||
| # The MIT License | ||||
| # Copyright (c) 2014 Ilkka Seppälä | ||||
| # | ||||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
| # of this software and associated documentation files (the "Software"), to deal | ||||
| # in the Software without restriction, including without limitation the rights | ||||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
| # copies of the Software, and to permit persons to whom the Software is | ||||
| # furnished to do so, subject to the following conditions: | ||||
| # | ||||
| # The above copyright notice and this permission notice shall be included in | ||||
| # all copies or substantial portions of the Software. | ||||
| # | ||||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
| # THE SOFTWARE. | ||||
| # | ||||
|  | ||||
| server.port=50004 | ||||
| @@ -0,0 +1,78 @@ | ||||
| /** | ||||
|  * The MIT License | ||||
|  * Copyright (c) 2014 Ilkka Seppälä | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  * THE SOFTWARE. | ||||
|  */ | ||||
| package com.iluwatar.api.gateway; | ||||
|  | ||||
| import org.junit.Before; | ||||
| import org.junit.Test; | ||||
| import org.mockito.InjectMocks; | ||||
| import org.mockito.Mock; | ||||
| import org.mockito.MockitoAnnotations; | ||||
|  | ||||
| import static org.junit.Assert.assertEquals; | ||||
| import static org.mockito.Mockito.when; | ||||
|  | ||||
| public class ApiGatewayTest { | ||||
|  | ||||
|   @InjectMocks | ||||
|   private ApiGateway apiGateway; | ||||
|  | ||||
|   @Mock | ||||
|   private ImageClient imageClient; | ||||
|  | ||||
|   @Mock | ||||
|   private PriceClient priceClient; | ||||
|  | ||||
|   @Before | ||||
|   public void setup() { | ||||
|     MockitoAnnotations.initMocks(this); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * Tests getting the data for a desktop client | ||||
|    */ | ||||
|   @Test | ||||
|   public void testGetProductDesktop() { | ||||
|     String imagePath = "/product-image.png"; | ||||
|     String price = "20"; | ||||
|     when(imageClient.getImagePath()).thenReturn(imagePath); | ||||
|     when(priceClient.getPrice()).thenReturn(price); | ||||
|  | ||||
|     DesktopProduct desktopProduct = apiGateway.getProductDesktop(); | ||||
|  | ||||
|     assertEquals(price, desktopProduct.getPrice()); | ||||
|     assertEquals(imagePath, desktopProduct.getImagePath()); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * Tests getting the data for a mobile client | ||||
|    */ | ||||
|   @Test | ||||
|   public void testGetProductMobile() { | ||||
|     String price = "20"; | ||||
|     when(priceClient.getPrice()).thenReturn(price); | ||||
|  | ||||
|     MobileProduct mobileProduct = apiGateway.getProductMobile(); | ||||
|  | ||||
|     assertEquals(price, mobileProduct.getPrice()); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user